I have two arduinos operating in tandem. each with its own photocell and a buzzer to enunciate when a laser beam is broken. I can't get them to communicate. I just need the first device (call it "A") to tell the second device ("B") when its sensor is tripped. B then keeps track of when either device's sensor is tripped, keeps count, and reads off its count every time each sensor is triggered.
I can't get device A to tell device B what's going on properly. I thought I could simply do that with a simple if-statement and setting an output pin on "A" to HIGH for B to read as part of its loop. For some reason, though, the signal from A *always* comes in as high, not just when an event is triggered, so the count goes up continuously. Are digital pins set to HIGH by default or something? Any idea what's going on? I really don't want to have to use a much more sophisticated form of communication for this kind of job.
here's my code from device A:
>>>>
int lightPin = 0; //define a pin for Photo resistor
int threshold = 850;
void setup(){
Serial.begin(9600); //Begin serial communcation
pinMode(13, OUTPUT);
pinMode(1, OUTPUT);
}
void loop(){
if(analogRead(lightPin) < threshold) {
digitalWrite(13, HIGH);
digitalWrite(1, HIGH);
}else{
digitalWrite(13, LOW);
digitalWrite(1, LOW);
}
}
>>>
And here it is from device B:
>>>>
int lightPin = 0; //define a pin for Photo resistor
int count = 0;
int threshold = 700;
int i = count;
void setup(){
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(7, INPUT);
}
void loop(){
// If the local sensor is triggered, increment the count, hit the buzzer on pin 13, then report the new score
if((analogRead(lightPin) < threshold)) {
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
count++;
i = count;
report_score();
}
// If an input comes in from the other sensor, increment count and report the new score
if((digitalRead(7) == HIGH)) {
count++;
i = count;
report_score();
}
digitalWrite(13, LOW); // Make sure the local buzzer is low at the end of each loop.
}
void report_score() {
while(i != -1) {
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
i--;
}
delay(1500);
}