If statement - cant get it to true

Hi guys,

Im writing a simple code, that when an integer gets equal 8, the LED must turn on, else it must be off.

Should be simple. HOwever, i cannot get my if statement to be true.

Further background about my code. I enter the current clock in the serialmonitor: 0810.
It then reads 08, as the hour 08
and the minute 10.
After reading the string, it converts the string to int and update the variable.

To confirm this has been done properly, i use the serial print function to print the current value of the two integers.

After that, i make my if statement. That if the hour = 08 / time, is equal to 8, then the LED must be turned on, else it has to be turned off.

Once making this statement, i cant get it accept that the hour is equal to 8, meaning it will allways be off.

Here is my code:

int Time = 0;
int Minut = 0;
const int LED = 7;

void setup() {
 // Initialize Serial Monitor
  Serial.begin(115200);

 pinMode(7, OUTPUT); // initialization of output 7 as "Output for Time"
digitalWrite(LED, LOW); // Start out with the LED be turned off.
 
}

void loop() {

// Read current clock and divide into hour and minute.
  if (Serial.available() > 0) {
    // read the incoming string:
    String incomingString = Serial.readString();

//Read the first two digits, to understand which hour it is.
String CurrentHour = incomingString.substring(0,2);
Serial.println(CurrentHour);


//read the minute.
String CurrentMinute = incomingString.substring(2,4);
Serial.println(CurrentMinute);

//Convert string to integer.
int Time = CurrentHour.toInt();
int Minut = CurrentMinute.toInt();

//Confirm CurrentHour string is converted to integer variable Time and proof by print to serial
Serial.print("Nuværende time er: ");
Serial.println(Time);
Serial.print("Nuværende minut er: ");
Serial.println(Minut);

    // prints all incomming data
    Serial.print("I received: ");
    Serial.println(incomingString);
  }


//If statement, turn on LED if Currenthour / time is equal to 8.
**if (Time == 8 ) { **
**digitalWrite(LED, HIGH); // LED on the output 7**
**Serial.println("LED ON");**
**delay(500);**
**} else {**
digitalWrite(LED, LOW); //  
Serial.println("LED OFF");
delay(5000);
}

A copy of serialmonitor after i type in 0808.

Serial monitor

Looking forward to hearing ideas to a solution :smiley:

I have tried to read around on the forum about the subject, but cant seem to find a solution my my particulair issue.

Thank you in advance

Add printing the Time values to the conditions code and you will see that the Time is not 8:

if (Time == 8 ) { 
digitalWrite(LED, HIGH); // LED on the output 7
Serial.println("LED ON");
Serial.print("Nuværende time er: ");
Serial.println(Time);
delay(500);
} else {
digitalWrite(LED, LOW); //  
Serial.println("LED OFF");
Serial.print("Nuværende time er: ");
Serial.println(Time);
delay(5000);
}

You have a two separate variable Time - one global, which is assigned to 0 at the start, and the second is local inside the

if (Serial.available() > 0)

block.
You assign your value 8 to local Time variable, but using the global one in conditions.

you have defined Time as a global and in the if() in loop();

int Time = 0;
int Minut = 0;
.....
void loop() {
  // Read current clock and divide into hour and minute.
  if (Serial.available() > 0) {
 .....
   //Convert string to integer.
    int Time = CurrentHour.toInt();
    int Minut = CurrentMinute.toInt();

remove the definition in loop(), e.g. remove int

    //Convert string to integer.
    Time = CurrentHour.toInt();
    Minut = CurrentMinute.toInt();

Thank you all alot :slight_smile:

The terms global and local variables is new to me. But makes good sense. have been reading up on it and now understand that part.

I removed "int" from the loop, and now it works perfectly as intended.

Thank you :slight_smile:

have a read of scope rules

Thanks for the tip, will do :slight_smile:

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