Button is not working when serial is reading?

I found that I cannot push button to make the led light when there is serial.read() or mySerial.read() there.
#mySerial is software serial

I want to do this:
Board A push button and send sign to Board B
Board B get the sign and flash orange light
Board B wait for the button push
if Board B button is push, green/red light will turn on and signal will send to Board A
Board A receive signal and turn on the green/red light too.

Aduino board A (Push button to send signal and then wait for the signal from Board B)

#include <SoftwareSerial.h>
#define callButton 8
#define greenLight 13
#define redLight 7
int val;
SoftwareSerial mySerial(2, 3); //RX, TX
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
pinMode(callButton, INPUT_PULLUP);
pinMode(greenLight, OUTPUT);
digitalWrite(greenLight, LOW);
digitalWrite(redLight, LOW);
}

void loop() {
 if (mySerial.available() > 0)
 {
 mySerial.listen();
 val = mySerial.read();


  
if (digitalRead(callButton) == LOW)
{
mySerial.write(200);
pinMode(callButton, INPUT_PULLUP);
delay(500);
}
  
  
if (val == 100)
{
digitalWrite(greenLight, HIGH);
delay(250);
}
  
  
if (val == 50)
{
digitalWrite(redLight, HIGH);
delay(250);
}  
  
if (val == 70)
{
digitalWrite(redLight, LOW);
digitalWrite(greenLight, LOW);
delay(250);
}  

}
}

Aduino board B (receive signal from board A and then push button to reply)

#define greenLight 13
#define orangeLight 12
#define redLight 11
#define greenButton 7
#define redButton 4
#define resetButton 8
#include <SoftwareSerial.h>
int val;
SoftwareSerial mySerial(2, 3); //RX, TX
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
pinMode(greenLight, OUTPUT);
digitalWrite(greenLight, LOW); 
pinMode(orangeLight, OUTPUT);
digitalWrite(orangeLight, LOW);
pinMode(redLight, OUTPUT);
digitalWrite(redLight, LOW);
pinMode(redButton, INPUT_PULLUP);
pinMode(greenButton, INPUT_PULLUP);
pinMode(resetButton, INPUT_PULLUP);  
}


  
void loop() {
if (mySerial.available() > 0)
{
	val = mySerial.read();




  
if (val == 200)
{
  for(int i = 0; i < 20; i++)
   {
      digitalWrite(orangeLight, !digitalRead(orangeLight));
  	  delay(300);
   }
  mySerial.end();
}
 
  
  
if (digitalRead(greenButton) == LOW)
{
digitalWrite(greenLight, HIGH);
mySerial.write(100);
delay(100);
}
  
if (digitalRead(redButton) == LOW)
{
digitalWrite(redLight, HIGH);
mySerial.write(50);
delay(100);
}

  
if (digitalRead(resetButton) == LOW)
{

digitalWrite(redLight, LOW);
digitalWrite(greenLight, LOW);
digitalWrite(orangeLight, LOW);
mySerial.write(70);
delay(100);
}
}
}

In the first code, everything is inside the if (mySerial.available() > 0) so nothing can happen till there is serial available. Separate the reading of serial data from the reading the button.

Like so:

void loop()
{
   if (mySerial.available() > 0)
   {
      mySerial.listen();
      val = mySerial.read();

      if (val == 100)
      {
         digitalWrite(greenLight, HIGH);
         delay(250);
      }

      if (val == 50)
      {
         digitalWrite(redLight, HIGH);
         delay(250);
      }

      if (val == 70)
      {
         digitalWrite(redLight, LOW);
         digitalWrite(greenLight, LOW);
         delay(250);
      }

   }
   if (digitalRead(callButton) == LOW)
   {
      mySerial.write(200);
      pinMode(callButton, INPUT_PULLUP);
      delay(500);
   }
}

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Why you make a pinMode in loop ??
and you have that in the Board A-sketch in the setup
Forgot pinMode (redlight, OUTPUT);

That is the original code that I just re-arranged so the button could be read without serial input available. I did not try to correct all of the code. I have no idea why the OP has a pinMode there nor what it is supposed to do.

Yes, sorry, I wrongly set the quota on your contribution instead of the contribution from the TO. I apologize to you for this.

Awecome!! it works!!! Thank you so much!!!

I just want the button back to "up".

you don't need to do that yourself. INPUT_PULLUP adds a hardware resistor to + 5V in the input. After you have 'commanded' it in the setup, it is the same as if you had attached a resistor to the input pin to + 5V. So it always works, so that when you let go of the button, the pin automatically goes back to HIGH.

You're right! I tested it. Thank you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.