Something is wrong in my timing sketch and cant figure out what it is.
int led = 13;
int led1 = 12;
const int button1 = 2;
const int button2 = 3;
unsigned long timer1 = 5000;
unsigned long timer2 = 10000;
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
}
void loop() {
unsigned long time = millis();
unsigned long timing1;
unsigned long timing2;
int b1;
int b2;
b1 = digitalRead(button1);
b2 = digitalRead(button2);
//i want timer to start running(and resetting if b1 is 0), output go high when button is pressed
if (b1 == 0)
timing1 = (time + timer1);
(digitalWrite(led, HIGH));
//output should go low when timer runs out
if (timing1 == time)
(digitalWrite(led, LOW));
if (b2 == 0 )
timing2 = (time + timer2);
(digitalWrite(led1, HIGH));
if (timing2 == time)
(digitalWrite(led1, LOW));
}
if (b1 == 0)
timing1 = (time + timer1);
(digitalWrite(led, HIGH)); // <<<<<<<<<<<<<<<<<<<<< meant to be part of the "if"?
If you mean that the digitalWrite is part of the "if" then you need add braces otherwise only the first line is part of the "if" and the second will always run
BulldogLowell:
what are you trying to do, it isn't really obvious in your code.
if button is pressed, then timer should be set and output go high, when timer runs out, output should go low.
in future there will be 2 pir sensors and many outputs with different timings, thats why i cant use delay().
Another question comes up, what if millis() starts over and timer has started just before 0? Output will stay HIGH forever?
millis() - Arduino Reference
Description
Returns the number of milliseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.