Go Down

Topic: How to easily send data from one arduino to another? (Read 788 times) previous topic - next topic

cat6

Nov 25, 2012, 12:50 am Last Edit: Nov 25, 2012, 12:59 am by cat6 Reason: 1
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:
>>>>
Code: [Select]

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:
>>>>
Code: [Select]

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

Nick Gammon

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.

Nick Gammon

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.

cat6

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.

cat6

#4
Nov 25, 2012, 01:07 am Last Edit: Nov 25, 2012, 01:09 am by cat6 Reason: 1
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?

Go Up