How to easily send data from one arduino to another?

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);
  }

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.

How have you connected them up? Pin 1 on A to pin 7 on B?

It might help to use names rather than lots of numbers everywhere.

My first guess it that since you have done a Serial.begin on A the serial port has taken over pin 1. Try another pin, like 3 or something.

Thanks, Nick. Trying that presently. And yeah, I'm normally big on meaningful variable names, but when you're changing things constantly just trying to get something to work, such nicieties tend to go out the window. Not good form, I know, but it happens.

brb with the results of that pin-change suggestion.

Ok, so...I've tried changing the output pin to #3 and that hasn't changed anything, although I AM noticing some weird stuff. Whenever a wire is connected to the #3 output pin, the buzzer (pin 13) stops working, even when the sensor is tripped. I can see it's being tripped because the little "L" LED on the board goes off whenever I block the sensor.

EDIT: btw, this kind of communication normally works, right? I'm not trying to do anything weird or problematic that people normally avoid?

On the face of it, what you are doing looks fine. Why are you doing analog reads on B?

Whenever a wire is connected to the #3 output pin, the buzzer (pin 13) stops working, even when the sensor is tripped.

On which board?

Ok, the analog reads are to detect the photocells' information. They seem to be working fine. Neither buzzer is going off when the sensor on A is triggered, but what should happen is that A's buzzer should go off and B should report the total count. If the count starts off at 2, and A's sensor is triggered, then you should get:
beep (from B)
beep-beep-beep (from A)

instead, like I said, starting off at zero, A never makes a sound and B just starts counting up at every available cycle. Did I mention the really weird thing is that this happens even when only a wire is connected to pin 7 of B and that wire is connected to nothing at all? Seriously. B goes off when its sensor is triggered AND it'll continue counting up as long as something is inside pin 7. :frowning:

That is normal if you don't pull the pin up or down with a resistor, or activate the internal pull-up. It is picking up stray signals.

Also you may want to make B count a transition. That is it was LOW and it is now HIGH. Not just add one if it happens to be HIGH.

Scratching my head here. For avoidance of doubt, could you please confirm that you have the grounds of the two Arduinos connected?

-br

What do you mean the grounds of the two arduinos connected? You mean the digital grounds? How would that help? Am I indeed missing something really important here? :fearful:

EDIT: Nick: you mean I should code in an edge-triggered event instead? How would I do that in Arduino C? I've done it in Verilog, but there was specialized syntax for creating such circuits in that case.

Instead of:

  // 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(); 
  }

You might have

  // If an input comes in from the other sensor, increment count and report the new score

  byte current_reading = digitalRead(7);
  static byte last_reading = LOW;     // static preserves values through each iteration

  if (current_reading == HIGH && last_reading == LOW) 
   {
   count++;
   i = count;
   report_score(); 
   }

  last_reading  = current_reading;

That detects a change from LOW to HIGH. Otherwise you might add 1000 to count before the sending end took the line LOW again.

If you don't have the grounds of the two arduinos connected, there is no circuit. You need one wire from the signal pin on A to the signal pin on B, and one wire from the ground on A to the ground on B.

-br

Bilroy: Ohhhhhhh...yeah, that um...makes some sense. I'll try that first.

Nick, I'll try your suggestion next and get back to both of you asap.

Yay! It works! See this awful pile of lego and electronics in all its now-functioning glory here: - YouTube

Thanks again for the help, guys.