Arduino: wait for serial input

13,079

Solution 1

This is the answer.

int switch1 = 2;
int motorled1 = 3;
int switch2 = 4;
int motorled2 = 5;
int d1 = 0;
int d2 = 0;
int buzzer;
char reset1;

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);

  pinMode(2, INPUT);
  pinMode(4, INPUT);
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:

  d1 = digitalRead(2);
  d2 = digitalRead(4);
  if (d1 == 1 && buzzer == 0) {
    digitalWrite(motorled1, HIGH);
    buzzer = 1;
  } else if (d2 == 1 && buzzer == 0) {
    digitalWrite(motorled2, HIGH);
    buzzer = 2;
  }

  if (Serial.available() > 0) {
    Serial.write(buzzer);

    reset1 = Serial.read();
    buzzer = 0;

    if (reset1 == 'R') {
      Serial.println("LED is off");
      digitalWrite(motorled1, LOW);
      digitalWrite(motorled2, LOW);
    }
  }
}

Solution 2

You seem to be using some asynchronous code structure. This is a good thing, but I am not really sure if this is what you intended to do.

In its current state, the code will continue looping over and over, checking if one of the buttons is pressed.

Now, there is two ways of achieving this:

  • Either make a proper state machine design, which would be the preferred way
  • Or just wait for the serial to be available at some point.

For the second solution, you could replace

if (Serial.available() > 0)
{
  reset1 = (Serial.read());

  digitalWrite(motorled1, LOW);
  digitalWrite(motorled2, LOW);
}

by

while (Serial.available() <= 0)
{}
reset1 = (Serial.read());

digitalWrite(motorled1, LOW);
digitalWrite(motorled2, LOW);

essentially doing nothing while there is nothing on the serial port. Of course, this will completely freeze the rest of the program, and isn't very flexible.

For the first way, there is once again multiple ways of doing it. Since you seem not to be familiar with C programming (no offense), one of the easiest ways of doing it would be to change your if (d1==1) and else if (d2==1) statements by if (d1==1 && serial_read == false) and else if (d2==1 && serial_read == true). Then, at the top of the program, add:

int serial_read = false;
if(Serial.available() > 0)
{
  reset1 = (Serial.read());
  serial_read = true;
}

This is the basic idea. I will let you sort out the various bugs and improvements (such as setting serial_read to false again) as an exercise.

I also strongly encourage you to read a bit about programming in general, and C programming in particular. I also advise you to try to stick to some convention for indenting your code. There is nothing worse than code with mixed indentation.

Share:
13,079
Tamanna Islam
Author by

Tamanna Islam

Updated on June 04, 2022

Comments

  • Tamanna Islam
    Tamanna Islam almost 2 years

    I am writing a simple arduino code. I have two leds and corresponding two switches. When one switch is pressed one led is on and other is off. Then there is a Serial.read function, which reads reset from the computer. Then both switch are off. After that other switch is pressed and other led is on. My problem is when one switch is on other shouldn't be working till the Serial.read happens.But in my case, when led1 is on if I press switch2 led2 is on and led1 is off. But that is not my desired operation. I want to make the logic that when led1 is on if I press switch2 led2 shouldn't be on and wait for the Serial.read to happen. Here is my code. I need to know what should be the correction in the logic:

    int switch1 = 2;
    int motorled1 = 3;
    int switch2 = 4;
    int motorled2 = 5;
    int d1=2; 
    int d2=3; 
    int reset1 = 0;
    int reset2= 0;
    
    
    void setup() {
      // put your setup code here, to run once:
      Serial.begin(9600);
      pinMode(2, INPUT);
      pinMode(4, INPUT);
      pinMode(3, OUTPUT);
      pinMode(5, OUTPUT);
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
      d1=digitalRead(2);
      d2=digitalRead(4);
    
      if (d1==1)
      {
        digitalWrite(3, HIGH);
        digitalWrite(5, LOW);
    
        if (Serial.available() > 0)
        {
          Serial.write(1);
        }
        if (Serial.available() > 0)
        {
          reset1 = (Serial.read());
    
          digitalWrite(motorled1, LOW);
          digitalWrite(motorled2, LOW);
        }
      }
      else if (d2==1)
      {
        digitalWrite(3, LOW);
        digitalWrite(5, HIGH);
    
        if (Serial.available() > 0)
        { 
          Serial.write(2);
        }
        if (Serial.available() > 0)
        {
          reset2 = (Serial.read());
    
          digitalWrite(motorled1, LOW);
          digitalWrite(motorled2, LOW);
        }
      }
    }