Unexpected result from loop

As you can tell I am very new to all this.

Basically as part of the learning process the SKETCH should

  1. Create an hours minutes and seconds timer.

  2. The sketch works just fine WITHOUT the LOOPS.

  3. The loops are to turn on a LED at a specific time and then turn it off at a specific time.

3.Whatever I am doing wrong just lets the program run for 1 seconds then updates the "seconds " to increment by 30 each loop but naturally never turns on the LED because the LOOP condition is not met.

Any help appreciated.

Increment__seconds_and_PRINT.ino (925 Bytes)

Here is the code formatted better and in code tags. OP please note

int minutes = 0;
int seconds = 0;
int hours = 0;


void setup()                            //SETUP
{
  Serial.begin (9600);  // Open up Serial port at a Baud Rate of 9600
  pinMode (7, OUTPUT);
}
void loop()
{
  Serial.println(String("H....") + hours + ("M....") + minutes + String("S....") + seconds);
  seconds++;
  if (seconds > 10)                           // does not print if seconds is less than 10
  {
    Serial.println ("xxxxxxxxxx");
    seconds = 0;
    minutes++;
  }
  if (minutes > 4)                           // does not print if minutes is less than 4
  {
    Serial.println ("oooooooooo");
    minutes = 0;
    hours++;
  }
  if (hours > 4)                           // does not print if hours is less than 4
  {
    Serial.println ("xxxxxxxxxx");
    hours = 0;
  }
  if (minutes = 2)
  {
    digitalWrite (7, HIGH); // Turn LED on at 20 seconds
  }
  //if(minutes=3){ digitalWrite (7,LOW);}   // turn LED off at 30 seconds
  delay (1000);
}

The sketch works just fine WITHOUT the LOOPS.

Which loops are you referring to ?

  if (minutes = 2)
  {
    digitalWrite (7, HIGH); // Turn LED on at 20 seconds
  }

Which is correct, the code that does not actually do a comparison or the comment ?

Sorry for the misuse of terminology.

Basically I was trying to use the "if" statement to turn on the LED if the "minutes " variable is 2.

The next "if" statement (Which is inactive in the sketch I sent in) would turn off the LED if the "minutes " variable is 3.

Your comment was

"Which is correct, the code that does not actually do a comparison or the comment ?"

Am not sure what you mean?

My thought was that the comparison was comparing the value of the variable "minutes" to "=2" and if that statement was true then I had thought the "digitalWrite" would have actioned.

Am I incorrect in this thinking or is my error in the code itself?

Thanks

if (minutes = 2) { digitalWrite (7, HIGH); // Turn LED on at 20 seconds

 if (minutes = 2)

Read carefully the reference for the if statement.

groundFungus: if (minutes = 2)

Read carefully the reference for the if statement.

Yup - classic C newbie trap.

https://www.includehelp.com/c-programming-questions/what-is-difference-between-assignment-and-equalto-operator.aspx https://www.tutorialspoint.com/cprogramming/c_operators.htm https://www.tutorialspoint.com/cprogramming/c_assignment_operators.htm

Farticus: Your comment was

"Which is correct, the code that does not actually do a comparison or the comment ?"

Am not sure what you mean?

if (minutes = 2) { digitalWrite (7, HIGH); // Turn LED on at 20 seconds

1. When you are asked to assign 3 to a byte variable named x, how do you do it? You do it this way: byte x = 3;. So, = is an assignment operator.

2. When you are asked to check if the variable minutes has arrived at the value of 2 or not, what do you do? You compare the value of the variable with 2. To do this comparison, will you use an assignment operator or a comparison operator? From the following list of operators, please pick up the appropriate comparison operator and then correct your codes.

Many thanks to GolamMostafa for the help.

I now understand the difference between = and ==.

My problem was a pure “Rookie” error which I now understand.

I had thought that the IF statement presented the argument

“IF (variable) (=) (Integer) then the argument would return a True or False answer.”

I now realise that I was just resetting the variable each time.

I had thought that the IF statement presented the argument

Pro tip. Never just go with what you think. Always read the reference. That way you’re never wrong.