need some help with debouncing along side with serial communication !!

hello guys,
i am trying to make a Serial communication between 2 arduinos
so what i need to do is this : having a push button connected to the first arduino and both arduino are connected via RX and TX
so when i click the button, a led will goes on, and if i click it again it will goes off
my problem is in the debouncing, i need to know how to make the debouncing with that ?
here are my codes so far :

for the TX arduino

int switchPin = 7;

boolean currentButton = LOW;
boolean lastButton = LOW;
int ledOn;

boolean debounce(boolean last){
  boolean current = digitalRead(switchPin);
  if(last != current){
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}

void setup(){
  pinMode(switchPin, INPUT);
  digitalWrite(switchPin, LOW);
  Serial.begin(9600);
}

void loop(){
  currentButton = debounce(lastButton);
  if(lastButton == LOW && currentButton == HIGH){
    ledOn = 1;
  }else if(lastButton == HIGH && currentButton == LOW){
    ledOn = 0;
  }
    if(ledOn == 1){
      Serial.write('1');
    }
    if(ledOn == 0){
      Serial.write('0');
    }
  
  lastButton = digitalRead(switchPin);
}

for the RX arduino :

void setup(){
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  Serial.begin(9600);
}

void loop(){
  if(Serial.available() > 0){
    char letter = Serial.read();
    
    if(letter == '1'){
      digitalWrite(13, HIGH);
    }else if(letter == '0'){
      digitalWrite(13, LOW);
    }
  }
}

You already have a poor man's debounce implemented. If that's not working, explain what the code is doing right now.

Arrch:
You already have a poor man's debounce implemented. If that's not working, explain what the code is doing right now.

the code works fine with one arduino not 2, untill i changed :

  currentButton = debounce(lastButton);
  if(lastButton == LOW && currentButton == HIGH){
    ledOn = !ledOn;
  }
  lastButton = currentButton;
  digitalWrite(ledPin, ledOn);

to this :

currentButton = debounce(lastButton);
    if(lastButton == LOW && currentButton == HIGH){
      ledOn = 1;
    }else if(lastButton == HIGH && currentButton == LOW){
      ledOn = 0;
    }
    if(ledOn == 1){
      Serial.write('1');
    }
    if(ledOn == 0){
      Serial.write('0');
    }

yes your right i tried it, but i don't get how to send a serial command i mean here how to do it, if you see my codes which i added to the original codes, i tried to create a variable and according to that variable, i sent a char 0 or 1 and on the RX arduino, if it is a 0 turn led off and if it is 1 turn it on so how can i do it if my codes didn't work ?

firashelou:
the code works fine with one arduino not 2, untill i changed :

Despite your inability or unwillingness to properly explain the issue, I think I have an idea of what is going wrong. It sounds like you want to use the switch as a toggle. In other words, you want to press and release once, and the LED turns on. Then, another press and release and the switch turns off. I'm guessing right now, it turns the LED on when you press the switch and turns it off when you release it? This section of the code is the issue:

if(lastButton == LOW && currentButton == HIGH){
      ledOn = 1;
    }else if(lastButton == HIGH && currentButton == LOW){
      ledOn = 0;
    }

You're turning the LED on when it transitions from LOW to HIGH, or when the switch is pressed down.

You're turning the LED off when it transitions from HIGH to LOW, or when the switch is released.

You need to keep track of the current state of the LED. When a transition occurs (either HIGH to LOW or LOW to HIGH, not both), change the current state, and send an update. Alternatively, you can offload the "toggling" to the receiving Arduino and have the sender just send a single, uniform byte whenever the transition occurs.

if(lastButton == LOW && currentButton == HIGH){
    ledOn = !ledOn;
  }

Notice how the working code is only looking at the LOW to HIGH transition, and not the other?

ok then how can i do it ?

i tried this but it didn't work and the TX led on arduino stayed on !!

currentButton = debounce(lastButton);
    if(lastButton == LOW && currentButton == HIGH){
      ledOn != ledOn;
    }
    if(ledOn == true){
      Serial.write('1');
    }else if(ledOn == false){
      Serial.write('0');
    }

Probably because you are writing to the Serial buffer faster than it can send out. Try moving the send code into the signal edge if statement. If that doesn't work, post the FULL code.

Arrch:
Probably because you are writing to the Serial buffer faster than it can send out. Try moving the send code into the signal edge if statement. If that doesn't work, post the FULL code.

about the serial buffer, for both arduino the baud rate is 9600 if that's what you mean ?

ok i did it now the TX led is flashing when i press

int switchPin = 7;

boolean currentButton = LOW;
boolean lastButton = LOW;
boolean ledOn = false;

boolean debounce(boolean last){
  boolean current = digitalRead(switchPin);
  if(last != current){
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}

void setup(){
  pinMode(switchPin, INPUT);
  digitalWrite(switchPin, LOW);
  Serial.begin(9600);
}

void loop(){
    currentButton = debounce(lastButton);
    if(lastButton == LOW && currentButton == HIGH){
      ledOn != ledOn;
      if(ledOn == true){
      Serial.write('1');
    }else if(ledOn == false){
      Serial.write('0');
    }
    }
   
  
  lastButton = currentButton;
}

firashelou:
about the serial buffer, for both arduino the baud rate is 9600 if that's what you mean ?

No. loop() runs really fast. It could run hundreds of times between each Serial byte being sent. What happens when you dump 100 balls per second into a large bin while only 1 ball per second is being removed? It overflows. It you dump 500 red balls into the bin before dumping 500 green balls, you're going to be waiting nearly 7 minutes after you've dumped your green balls in before a green ball is actually removed. Now think of the red/green as '1'/'0'.

ok i did it now the TX led is flashing when i press

So the sender looks like it's working.

Arrch:

firashelou:
about the serial buffer, for both arduino the baud rate is 9600 if that's what you mean ?

No. loop() runs really fast. It could run hundreds of times between each Serial byte being sent. What happens when you dump 100 balls per second into a large bin while only 1 ball per second is being removed? It overflows. It you dump 500 red balls into the bin before dumping 500 green balls, you're going to be waiting nearly 7 minutes after you've dumped your green balls in before a green ball is actually removed. Now think of the red/green as '1'/'0'.

ok i did it now the TX led is flashing when i press

So the sender looks like it's working.

what's the solution then ? a delayMicroseconds(2) for example ?

firashelou:
what's the solution then ? a delayMicroseconds(2) for example ?

Only send the data after a signal transition. You don't need to send it if nothing has changed.

Arrch:

firashelou:
what's the solution then ? a delayMicroseconds(2) for example ?

Only send the data after a signal transition. You don't need to send it if nothing has changed.

how is that !!?
i thought about making a new variable but it won't work !!

You spent about ten minutes working that through before posting again, but not posting your code which didn't work.

i tried this but still :
is that what you mean ?

void loop(){
  currentButton = debounce(lastButton);
  if(lastButton == LOW && currentButton == HIGH){
    ledOn != ledOn;
    
    if(currentButton == HIGH){
      if(ledOn == true){
        Serial.write('1');
        Serial.println("1");
      }else if(ledOn == false){
        Serial.write('0');
        Serial.println("0");
      }
    }
  }
   
  lastButton = currentButton;
}
if(lastButton == LOW && currentButton == HIGH){
    ledOn != ledOn;
    
    if(currentButton == HIGH){

You already know currentButton is HIGH, so why bother testing it again?

AWOL:

if(lastButton == LOW && currentButton == HIGH){

ledOn != ledOn;
   
    if(currentButton == HIGH){



You already know currentButton is HIGH, so why bother testing it again?

ok so ?
i am getting this by the way on the RX arduino :
avrdude: stk500_getsync(): not in sync: resp=0x00

 ledOn != ledOn;

What do you think that is doing?

Did you mean ledOn = !ledOn; ?

AWOL:

 ledOn != ledOn;

What do you think that is doing?

Did you mean ledOn = !ledOn; ?

this is it !! that was a stupid mistake !! :S

so now the codes are :

int switchPin = 7;

boolean currentButton = LOW;
boolean lastButton = LOW;

boolean ledOn = false;

boolean debounce(boolean last){
  boolean current = digitalRead(switchPin);
  if(last != current){
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}

void setup(){
  pinMode(switchPin, INPUT);
  digitalWrite(switchPin, LOW);
  Serial.begin(9600);
}

void loop(){
  currentButton = debounce(lastButton);
  if(lastButton == LOW && currentButton == HIGH){
    ledOn = !ledOn;
    
    if(ledOn == true){
      Serial.write('1');
      Serial.println("1");
    }else if(ledOn == false){
      Serial.write('0');
      Serial.println("0");
    }
    
  }
  
  lastButton = currentButton;
}

big thanks guys you saved my working night, thank you for all the help :slight_smile:

but the other error is still there !! lol
i checked the forum here there is post about it but none of the solution suggested is in my case !

You're putting in quite some time, but so far you keep on running into problems with your project.
Now you have 2 problems at the same time, gave up on one and are trying to do the same thing in an other way.
OK for me, but this looks to me like you aren't learning a lot at the moment.

You have shown to not understand the blink without delay principle yet.
Part of that can be very well used for debouncing, but of course only if you understand what's going on.

You also stated that debouncing works for one, but not for 2 Arduino's that are connected through serial (is what in read).
Are you trying to do a remote debounce ?
If so, what's the point of that ?

I'd advice you to work through some more of the examples that were delivered with the IDE.
Only after understanding those you can use them.

And if you're going to use a sketch you found somewhere but need help understanding what it does, tell about what you think it is doing instead of telling the sketch must be wrong.

As for your last problem:
If you want to upload a sketch, temporary disconnect any serial connections, because that is what is used for upload.
The other connection might work like a parasite and mess up the upload.

MAS3:
You're putting in quite some time, but so far you keep on running into problems with your project.
Now you have 2 problems at the same time, gave up on one and are trying to do the same thing in an other way.
OK for me, but this looks to me like you aren't learning a lot at the moment.

You have shown to not understand the blink without delay principle yet.
Part of that can be very well used for debouncing, but of course only if you understand what's going on.

You also stated that debouncing works for one, but not for 2 Arduino's that are connected through serial (is what in read).
Are you trying to do a remote debounce ?
If so, what's the point of that ?

I'd advice you to work through some more of the examples that were delivered with the IDE.
Only after understanding those you can use them.

And if you're going to use a sketch you found somewhere but need help understanding what it does, tell about what you think it is doing instead of telling the sketch must be wrong.

As for your last problem:
If you want to upload a sketch, temporary disconnect any serial connections, because that is what is used for upload.
The other connection might work like a parasite and mess up the upload.

ok thanks yes right i must disconnect the other one,
well i can say i learned and understood my deboucing sketch which i was working on, there was a part which i didn't get before but after this post not just solved the problem but i understood that part :slight_smile: