Serial Monitor Input Works Only Once to Control LED

My son and I are working with an Arduino Uno and a breadboard kit. We built a circuit with two buttons for on and off control off the LED. We are trying to also get the Arduino to turn "ON" and "OFF" by also sending that text over the serial monitor.

Going through the forum and experimenting a few ways, we have had partial success. When the Arduino is booted, we can type "ON" and the LED fades up (we watch the feedback through the serial monitor as it counts the steps up). Once complete, if we type "OFF" nothing happens. The board will still respond to the buttons being pressed.

Now, if we boot the Arduino and press the on button the LED turns on. If we then type "OFF" into the serial monitor, it fades the LED off. Once complete, if we type "ON" nothing happens. The board will still respond to the buttons being pressed.

We are using the Arduino IDE (1.8.13) running on Linux (debian 11) on a Zimaboard. We first tried a simple Serial.read function that worked partially. Eventually we ended up with the code below thinking that an incomplete read of the serial input was the culprit, but no such luck. Any help on what to try next would be appreciated!!!

Here is the code:

int ledpin=9;
int inputpin1=3;
int inputpin2=2;
int value = 0;
const byte numChars = 32;
char receivedChars[numChars];

boolean newData = false;

void setup () {
  pinMode(ledpin, OUTPUT);
  pinMode(inputpin1, INPUT);
  pinMode(inputpin2, INPUT);
  Serial.begin(9600);
}
void loop() {
  receiveWithEndMarker();
 
  if (digitalRead(inputpin1)==LOW or receivedChars == "OFF") {
    Serial.println(receivedChars);
    for(value; value>=0; value--){
    analogWrite(ledpin,value);
    delay(10);
    Serial.println(value);
    }
    Serial.println("Light OFF");
    newData = false;
  }

  else if (digitalRead(inputpin2)==LOW or receivedChars == "ON") {
    Serial.println(receivedChars);
    for(value; value<=255; value++){
    analogWrite(ledpin,value);
    delay(10);
    Serial.println(value);
    }
    Serial.println("Light ON");
    newData = false;
  }
}

 void receiveWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx>= numChars) {
        ndx = numChars -1;
      }
    }
    else {
      receivedChars[ndx] = '\0';
      ndx = 0;
      newData = true;
    }
  }
}
    if (digitalRead(inputpin1) == LOW or receivedChars == "OFF")

You cannot use == to compare strings. Use strcmp() instead

if (digitalRead(inputpin1) == LOW or strcmp(receivedChars, "OFF") == 0)

That fixed it! Thank you for your feedback.

I am glad that it worked

For the benefit of anyone looking at this topic in the future please post your revised sketch

Here it is!

int ledpin=9;
int inputpin1=3;
int inputpin2=2;
int value = 0;
const byte numChars = 32;
char receivedChars[numChars];

boolean newData = false;

void setup () {
  pinMode(ledpin, OUTPUT);
  pinMode(inputpin1, INPUT);
  pinMode(inputpin2, INPUT);
  Serial.begin(9600);
}
void loop() {
  receiveWithEndMarker();
  
  if (digitalRead(inputpin1)==LOW or strcmp(receivedChars, "OFF") == 0) {
    for(value; value>=0; value--){
    analogWrite(ledpin,value);
    delay(10);
    Serial.println(value);
    if (value==0){
      Serial.println("Light OFF");
    }
    }
    newData = false;
  }

  else if (digitalRead(inputpin2)==LOW or strcmp(receivedChars, "ON") == 0) {
    for(value; value<=255; value++){
    analogWrite(ledpin,value);
    delay(10);
    Serial.println(value);
    if (value==255){
      Serial.println("Light ON");
    }
    }
    newData = false;
  }
}

 void receiveWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx>= numChars) {
        ndx = numChars -1;
      }
    }
    else {
      receivedChars[ndx] = '\0';
      ndx = 0;
      newData = true;
    }
  }
}

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