Button Controlled Timer?


i'm trying incorporate timer larger project. in essence, want have set when button pressed, start timer. after specified amount of time passes, led turn on. want restart timer (and turn off led, if necessary) if button pressed.

my main problem, however, know next nothing programming. managed start blinkwithoutdelay sketch , repurpose led turn on after 5 second interval, , turn off. there, have no idea how link button presses in manner described above. spent time messing around it, couldn't come close getting work.

here starting code timer:
code: [select]
const int redpin =  8;               
unsigned long previousmillis = 0;     
int redstate = low;                 
const long interval = 5000;     

void setup() {
  pinmode(redpin, output);
  serial.begin(9600);
}

void loop() {
  unsigned long currentmillis = millis();
  serial.println("current time: ");
  serial.println(currentmillis/1000);
  if(currentmillis - previousmillis >= interval) { 
    previousmillis = currentmillis;   
    serial.println("time: ");
    serial.println(previousmillis/1000);
    redstate = high;
    digitalwrite(redpin, redstate);
  } else {
    if(currentmillis - previousmillis < interval) {
      redstate = low;
      digitalwrite(redpin, redstate);   
    }
  }
}



and, if helps, here i'm planning on merging with:
code: [select]
const int buttonpin = 6;
const int redpin = 8;
const int greenpin = 10;
const int bluepin = 12;

int buttonpushcounter = 0;   
int buttonstate = 0;         
int lastbuttonstate = 0;
int stepgoal = 15;

void setup() {
  pinmode(redpin, output);
  pinmode(greenpin, output);
  pinmode(bluepin, output);
  pinmode(buttonpin, input);
  serial.begin(9600);
}

void loop() {
  buttonstate = digitalread(buttonpin);
  if (buttonstate != lastbuttonstate) {
    if (buttonstate == high) {
      buttonpushcounter++;
      serial.println("number of steps: ");
      serial.println(buttonpushcounter);
      if (buttonpushcounter < stepgoal) {
        digitalwrite(bluepin, high);
      } else {
        serial.println("goal met!");
        digitalwrite(greenpin, low);
        delay(150);
      }
    } else {
      digitalwrite(bluepin, low);
    }
    delay(50);
  }
  lastbuttonstate = buttonstate;
  if (buttonpushcounter >= stepgoal) {
    digitalwrite(greenpin, high);
  } else {
    digitalwrite(greenpin, low);
  }
}


if had ideas, or if there glaring problems, appreciated.

well, 1 thing not show stopper, habit need break before grows on you. have classic "redundant else" syndrome. :)
that say, in else statement, check opposite of same condition tested in "if".

for example,
code: [select]

if (this > that) {
  foo();
  }
else if ( <= that) {
  oof();
  }

it's redundant , annoying.
the same effect achieved with:
code: [select]

if (this > that) {
  foo();
  }
else {
  oof();
  }


in general, when working conditionals, have think in terms of conditions have screened for.

also, in habit of adding helpful , meaningful comments. don't have @ all.

in second program, have used delay() function inside loop(). won't work millis(). screw timing of have used millis() for.


Arduino Forum > Using Arduino > Programming Questions > Button Controlled Timer?


arduino

Comments