Arduino Timer Help.


hi guys,

i need create timer starts counting when button pressed. timer count , print onto serial monitor next 60 seconds , stop printing after ward. print next 60 seconds if button pressed again.

i've created code limited programming skill have it's not doing want do. millis() starts counting moment run code, not when press button. because of i'm having trouble getting time being recorded on serial monitor next 60 seconds when button pressed.

all helps appreciated.

here code:

code: [select]
// set pin numbers:
const int buttonpin = 8;

// variables change:
int buttonstate = 0;         

void setup() {
  // initialize pushbutton pin input:
  pinmode(buttonpin, input);
  serial.begin(9600);
}

void loop() {
  // read state of pushbutton value:
  buttonstate = digitalread(buttonpin);

  // check if pushbutton pressed.
  // if is, buttonstate high:
  if (buttonstate == high) {
    unsigned long milliseconds;
    int starttime = milliseconds;
    while ( milliseconds < starttime + 60000 ) {
      milliseconds = millis();
      serial.print(milliseconds);
      serial.println(" ms");
      delay(1000);
    }
  }
}

millis() works similar clock, starting when code has started , never stopping or being reset.

you need record value in millis() when button first pressed. 

when current time (given millis()) minus recorded time (previously given millis()) greater or equal 1000 milliseconds (or whatever printing interval is), time print onto serial monitor record new time measure next print.

see "example of subtracting - recommended" in nick gammon's forum article @ http://gammon.com.au/millis


Arduino Forum > Using Arduino > Programming Questions > Arduino Timer Help.


arduino

Comments

Popular posts from this blog

Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode - Raspberry Pi Forums

class MPU6050 has no member named begin

missing filename after '-o'