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.
Looking forward to hearing ideas to a solution
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