Pages: 1 2 [3]   Go Down
Author Topic: Code not reacting to button event  (Read 1150 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

    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:
    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!
Logged

Leeds, UK
Online Online
Edison Member
*
Karma: 78
Posts: 1719
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

"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:
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:
if(digitalRead(armButton))
Logged

~Tom~

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
the '== HIGH' is redundant.
But, it makes your intent and meaning much clearer. Please keep doing it!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"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:
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:
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...
Logged

Pages: 1 2 [3]   Go Up
Jump to: