Hey all,
Im trying to make a laser maze game and want to get 2 Uno's talking to each other through Serial.
I have them wired up accordingly:
Arduino #1 Arduino #2
Tx -----> Rx
Rx -----> Tx
Gnd -----> Gnd
I've gotten a message successfully sent & received from #1--->#2, but fail a get a message through from #2--->#1
Here's my code:
Arduino #1:
unsigned long tempus;
long score;
int laserBreakOn = 300;
int trigger = 1;
void setup() {
Serial.begin(9600);
for (int pin = 9; pin <= 13; pin++){ //initialize laser pins
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
}
}
void loop() {
tempus = millis();
score = 40000 - (tempus/5);
Serial.println(score);
if (analogRead(A0) < laserBreakOn){digitalWrite(9, LOW);}
if (analogRead(A1) < laserBreakOn){digitalWrite(10, LOW);}
if (analogRead(A2) < laserBreakOn){digitalWrite(11, LOW);}
if (analogRead(A3) < laserBreakOn){digitalWrite(12, LOW);}
if (analogRead(A4) < laserBreakOn){digitalWrite(13, LOW);}
if (digitalRead(7) == 1){ //end sequence
int laserBreakCount = 0;
for (int pin = 9; pin <= 13; pin++){
if (digitalRead(pin) == 0){
laserBreakCount++;
}
}
Serial.write(trigger);
delay(5);
int incoming = Serial.read();
laserBreakCount = laserBreakCount + incoming;
score = score - (1000*laserBreakCount);
Serial.print("Final Score:");
Serial.println(score);
Serial.end();
}
}
Arduino #2:
int laserBreakOn = 300;
void setup() {
Serial.begin(9600);
for (int pin = 8; pin <= 9; pin++){ //initialize laser pins
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
}
}
void loop() {
if (analogRead(A0) < laserBreakOn){digitalWrite(8, LOW);}
if (analogRead(A1) < laserBreakOn){digitalWrite(9, LOW);}
int incoming = Serial.read();
if (incoming == 1){ //end sequence
Serial.println("I READ ONE");
int breakCount = 0;
for (int pin = 8; pin <= 9; pin++){
if (digitalRead(pin) == 0){
breakCount++;
}
}
Serial.write(breakCount);
Serial.end();
}
}
When #1 goes through its end sequence, it successfully sends trigger to #2 and #2 begins its own end sequence, and i know breakCount successfully iterates to the number of broken lasers.
What i was then hoping that would happen after was that #2 writes breakCount to #1 and then #1 would add that to it's laserBreakCount.
BUT I can only get #1 to receive (or at least interpret what it's received) as a -1.
This is my first project involving Serial Comm between arduinos. I've looked at Robin2's great tutorial and numerous other places, but I just cant get these two talking for the life of me. The world of Serial just doesnt make sense to me yet.
Thanks ahead of time for all the help!
If there's any extra info you might need to know I'll do my best to provide.