Skip to main content
2 of 4
Added Arduino code changes.
avc
  • 23
  • 6

Arduino Uno "if" statement is ignored in code and halts execution

I'm having a weird issue with my Uno trying to power on a PWM fan using a MOSFET.

I'm setting a simple power on/off code using commands sent over the Serial Monitor, and although this code worked before, after I tried to add some code to control the speed of the PWM fan, my if statements do not work anymore, even with the simple power on/off code:

    String command;           //Define the string variable "command"

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

  pinMode(5, OUTPUT);     //Set pin 5 as MOSFET Gate signal pin
  pinMode(13, OUTPUT);    //Set pin 13 to use onboard LED as a waiting for command status LED
  digitalWrite(5, LOW);   //Start with MOSFET off
  digitalWrite(13, LOW);  //Star with status LED off
  Serial.begin(9600);     //Start Serial comm for sending commands

}

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

  Serial.println("What's your command?"); //Ask for a command in the Serial Monitor

  while (Serial.available()==0) {        //Wait for user input stopping the code
    digitalWrite(13, HIGH);               //Blink the onboard LED indicating that the code is waiting for user input
    delay(100);
    digitalWrite(13, LOW);
    delay(100);
  }

  command=Serial.readString();          //Save the Serial Monitor String command to the "command" variable as a String
  Serial.println("data read");          //Confirm Serial Monitor input
  Serial.println(command);              //Print the command sent over the Serial Monitor to confirm the data received
  
  if (command=="On") {                  //Turn the MOSFET on
    digitalWrite(5, HIGH);
  }
  if (command=="Off") {                  //Turn the MOSFET off
    digitalWrite(5, LOW);
  }
  else {                                 //Provide feedback for a wrong command
    Serial.println("Wrong command!");
  }
}

This is my Serial Monitor output after sending "On":

What's your command?
data read
On

Wrong command!
What's your command?

To check if the pin works, I tested a simple code that worked fine:

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

  pinMode(5, OUTPUT);     //Set pin 5 as MOSFET Gate signal pin
  digitalWrite(5, LOW);   //Start with MOSFET off
  Serial.begin(9600);     //Start Serial comm for sending commands

}

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

    digitalWrite(5, HIGH);
    delay(2000);
    digitalWrite(5, LOW);
    delay(2000);

}

Does anyone have any idea why the if statements are not working anymore?

Thank you in advance!

AC

EDIT: After trying to use the "else" statement as recommended by per1234, the issue continues to happen, but changing the variable type to an integer works, although the Serial output is rather messy:

int command;           //Define the int variable "command"

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

  pinMode(5, OUTPUT);     //Set pin 5 as MOSFET Gate signal pin
  pinMode(13, OUTPUT);    //Set pin 13 to use onboard LED as a waiting for command status LED
  digitalWrite(5, LOW);   //Start with MOSFET off
  digitalWrite(13, LOW);  //Star with status LED off
  Serial.begin(9600);     //Start Serial comm for sending commands

}

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

  Serial.println("What's your command?"); //Ask for a command in the Serial Monitor

  while (Serial.available()==0) {        //Wait for user input stopping the code
    digitalWrite(13, HIGH);               //Blink the onboard LED indicating that the code is waiting for user input
    delay(100);
    digitalWrite(13, LOW);
    delay(100);
  }

  command=Serial.parseInt();          //Save the Serial Monitor imput command to the "command" variable as an int

  Serial.println("data read");          //Confirm Serial Monitor input
  Serial.println(command);              //Print the command sent over the Serial Monitor to confirm the data received
  
  if (command==1) {                  //Turn the MOSFET on
    digitalWrite(5, HIGH);
  } 
  if (command==2) {                  //Turn the MOSFET off
    digitalWrite(5, LOW);
  }
  else {                                 //Provide feedback for a wrong command
    Serial.println("Wrong command!");
  }
}

Does anyone experience this issue? I even tried rolling back my IDE to 1.8.3, which I used to write the code the first time, then after a library update, and also getting version 1.8.5, it stopped working.

Also, a side question regarding the post size, should I remove the original code when editing, or should I move it to another answer?

Thank you.

avc
  • 23
  • 6