[SOLVED] LED Flashing In Sequence change when Push Button is pressed

Hello gurus,

Here is my idea for my project:

  1. LED pin 11, 12 and 13 will flash as a sequence when the code starts running
  2. When a button is pressed, it will light up and stay on; and LED pin 11, 12 and 13 will stop flashing as a sequence.
  3. When a button is pressed again, the light will turn off and LED pin 11, 12 and 13 will start flashing as a sequence again.

If you look at my code, I have initially used 3 functions which I have commented as “updateLed1FlashState()”, "updateLed2Flashstate() and “updateLed3FlashState()” to create a sequence. I had a problem that the flashing sequence would not stop when either buttons is being pressed.

I then further thought it might be solved if I use a for loop for my flashing sequence, and now it is not reading if my button is being pressed or not.

May I ask if any of you would suggest which part of my logic is wrong please?

Cheers,
Karen

test6.ino (7.34 KB)

 if (readButton1()==true){
    Serial.println("Button1 Pressed");
    LEDFlashSequence == false;
    readButton2 == false;       //   ???? == ????   or just = ???
    readButton3 == false;

knut_ny: if (readButton1()==true){     Serial.println("Button1 Pressed");     LEDFlashSequence == false;     readButton2 == false;      //  ???? == ????  or just = ???     readButton3 == false;

I tried using readButton2() = false; but it showed an error of "lvalue required as left operand of assignment" :(((

..keep track of what is a function and what is a variable.

navn() is a function, You can compare with : if function == xx {} without the initial 'if' it makes no sense

int variable = 5; // makes sense The sentence variable == 5; // doesn't

knut_ny: ..keep track of what is a function and what is a variable.

navn() is a function, You can compare with : if function == xx {} without the initial 'if' it makes no sense

int variable = 5; // makes sense The sentence variable == 5; // doesn't

Ny thanks for your reply. I understand that but if I just put it as LEDFlashSequence = false;, I got an error of "assignment of function 'boolean LEDFlashSequence()'". Surprisingly the error is gone if I just put it as LEDFlashSequence == false; instead. That's why I am really confused too @@

cklkaren: Ny thanks for your reply. I understand that but if I just put it as LEDFlashSequence = false;, I got an error of "assignment of function 'boolean LEDFlashSequence()'". Surprisingly the error is gone if I just put it as LEDFlashSequence == false; instead. That's why I am really confused too @@

But what are you trying to accomplish by comparing a function's address with false? That statement doesn't actually do anything.

cklkaren: Ny thanks for your reply. I understand that but if I just put it as LEDFlashSequence = false;, I got an error of "assignment of function 'boolean LEDFlashSequence()'". Surprisingly the error is gone if I just put it as LEDFlashSequence == false; instead. That's why I am really confused too @@

Your function == false test while valid, is not what you want to do.

A function call uses () on the end, and returns a result. That result can be compared with a value. For example: if (fn() > 27) { }

If you use variables however, you can directly assign values to the variable, like: int varA = 27;

In your case, when you use a function, always put the () on the end. It'll make debugging easier for you. Not using () on the end on functions is advanced coding, and you have no need for it.

arduinodlb: Your function == false test while valid, is not what you want to do.

A function call uses () on the end, and returns a result. That result can be compared with a value. For example: if (fn() > 27) { }

If you use variables however, you can directly assign values to the variable, like: int varA = 27;

In your case, when you use a function, always put the () on the end. It'll make debugging easier for you. Not using () on the end on functions is advanced coding, and you have no need for it.

Hi christop and arduinodlb,

Thanks for your reply.

Maybe I should explain my idea more clearly:

  1. All buttons start off inactive
  2. If a button is not already active, if a button is pressed, then it becomes active and the corresponding LED will be switched on.
  3. If a button is already active, if ANY buttons are pressed, then the active button is deactivated.
  4. Flashing only occurs when NO buttons are active.

Because of (3) and (4), I would like to force the buttons to be deactivated, and hence I was thinking to use a boolean function. So is it possible to call a boolean function within an if statement? Or is my logic wrong here?

Cheers, Karen

cklkaren: I tried using readButton2() = false; but it showed an error of "lvalue required as left operand of assignment"

because = is an assignment, whereas == is a test. You cannot assign a value to a function.

So,

if (readButton2() == false) is valid.

I have got what I wanted to work.
Thanks everyone :)))

I am sharing my code below just if anyone is working on a similar project in future.
I haven’t cleaned it up but at least it’s working :DDD

test7.ino (5.78 KB)