Pages: [1]   Go Down
Author Topic: Serial communication between two arduino  (Read 2047 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Sr. Member
****
Karma: 1
Posts: 334
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Dear all,

Transmitter code :
Code:
void setup(){
  Serial.begin(9600);
}

void loop(){
 

 {
   if (digitalRead(8)==LOW)
   {
   Serial.write(1);
   }
  }
}

Receiver code:
Code:
void setup()
{
 
  Serial.begin(9600);
}
void loop()
{
  int incoming;
 
  if (Serial.available()) {
    delay(1);
   
    while (Serial.available() > 0){
    incoming=Serial.read();
    if(incoming==1)
    {
      Serial.print(incoming);
      digitalWrite(13, HIGH);
      delay(100);
      digitalWrite(13,LOW);
      delay(100);
    }
    }
  }

}
 

I realised that when I press the button, it will not just send 'a' 1, but instead it shows like around 20++ 1 in the serial monitor.

Why ? I thought only will serialprint only one 1 ? Instead of so many ?

Thanks
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 50
Posts: 3429
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The serial.write does indeed only send one byte, but on the next iteration of loop, if the input is still low, it will print another, then another ad nauseum. The arduino is fast, so if that input is low for even a short time, you'll get lots of transmissions.
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you want it to only send once, you need to detect the transition, rather than the state. The transition occurs when the switch goes from HIGH to LOW, or LOW to HIGH. Keep track of the last value you read and compare it to the current value. If they are not the same, a transition occurred.
Logged

Offline Offline
Sr. Member
****
Karma: 1
Posts: 334
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How can I progam in that way ?

Thanks smiley-grin
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How can I progam in that way ?

Look at the StateChangeDetection example.
Logged

Offline Offline
Sr. Member
****
Karma: 1
Posts: 334
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Where is that example ? I cant locate it ><
Logged

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 15
Posts: 348
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://arduino.cc/en/Tutorial/ButtonStateChange
Logged

Offline Offline
Sr. Member
****
Karma: 1
Posts: 334
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks alot. I will try it again and get back to all of you smiley-mad
Logged

Offline Offline
Sr. Member
****
Karma: 1
Posts: 334
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Cool ! I get it !

However, just wonder, for the below coding :
Code:
if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
    Serial.println("The LED is ON");
  } else {
   digitalWrite(ledPin, LOW);
  }

the %4 is just example is it ? to show us that the chip knows when to light up the led ? which is the press which is multiple of 4
Logged

Pages: [1]   Go Up
Jump to: