Go Down

Topic: Code not reacting to button event (Read 1 time) previous topic - next topic

PaulS

If you put each { on its own line, and used Tools + Auto Format, your code would be MUCH easier to read.

Blank lines between functions are good. You have some, but some functions are still jammed up right after the previous one.

We harp on this stuff because hard to read code is hard to understand code. Hard to understand code is hard to maintain code.

brenryan


Using the classic 'blink without delay' as a starting point, you get this:
Code: [Select]

    unsigned long nextMillis = millis() + timer;
    for (int count = 10; count > -1; count--) {
      while(millis() < nextMillis) {
        if(/* button is pressed */) {
         
          //maybe do some debouncing here?
         
          mode = 0; //go back to standby mode
          return;
        }
      }
      nextMillis = millis() + timer;
      lcd.setCursor(0,1);
      lcd.print(count);
      lcd.print("   Seconds...");
    }
    mode = 2;



Thanks Tom,

That looks great, looking forward to testing it when I get back to the hardware.

Not sure what you mean by debouncing however...

If I was to implement this in my case, would it look something like this?

Code: [Select]
    unsigned long nextMillis = millis() + timer;
    for (int count = 10; count > -1; count--) {
      while(millis() < nextMillis) {
        if(digitalRead(armButton) == HIGH) {
         
          //maybe do some debouncing here?
         
          mode = 0; //go back to standby mode
          return;
        }
      }
      nextMillis = millis() + timer;
    lcd.begin(16,2);
    lcd.print("System Arming in");
    lcd.setCursor(0,1);
    lcd.print(count);
    lcd.print("   Seconds...");
    }
    mode = 2;


Also not sure what you mean by "blink without delay".

Again, apologies for my ignorance and thank you for you patience. This is my first introduction to C and my first micro-controller project!

Tom Carpenter

"Blink Without Delay" is one of the classic arduino example sketches (File->Examples->Digital->BlinkWithoutDelay). It demonstrates how you can replace a delay() with a loop using the millis() function which allows the MCU to do other things while the delay is happening.

Your digitalRead() statement is fine, that would do what you want (The debouncing is optional and may not be necessary in this case).

Just FYI, digitalRead() returns a boolean value it is either true or false, aka HIGH or LOW. That means that in this statement:

Code: [Select]
if(digitalRead(armButton) == HIGH)

the '== HIGH' is redundant. You are essentially saying if true == true (or false == true). It can be written more simply as just:

Code: [Select]
if(digitalRead(armButton))
~Tom~

PaulS

Quote
the '== HIGH' is redundant.

But, it makes your intent and meaning much clearer. Please keep doing it!

brenryan


"Blink Without Delay" is one of the classic arduino example sketches (File->Examples->Digital->BlinkWithoutDelay). It demonstrates how you can replace a delay() with a loop using the millis() function which allows the MCU to do other things while the delay is happening.

Your digitalRead() statement is fine, that would do what you want (The debouncing is optional and may not be necessary in this case).

Just FYI, digitalRead() returns a boolean value it is either true or false, aka HIGH or LOW. That means that in this statement:

Code: [Select]
if(digitalRead(armButton) == HIGH)

the '== HIGH' is redundant. You are essentially saying if true == true (or false == true). It can be written more simply as just:

Code: [Select]
if(digitalRead(armButton))



Quote
the '== HIGH' is redundant.

But, it makes your intent and meaning much clearer. Please keep doing it!


Cheers guys, thanks for clearing that up for me. Looking forward to experimenting with it later on.

Thanks again for all the help...

Go Up