millis(), timings, timer

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));
}

Try auto-formatting your code and losing the excess parentheses and see if that throws up any clues.

 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

 if (b1 == 0)  
{
  timing1 = (time + timer1);  
    (digitalWrite(led, HIGH));
}
void loop() {
  unsigned long time = millis();
  unsigned long timing1;
  unsigned long timing2;

  int b1;
  int b2;
  b1 = digitalRead(button1);
  b2 = digitalRead(button2);

  Serial.println("timing1");
  Serial.println(timing1);
  delay(1000);


  if (b1 == 0)  
  {
    timing1 = (time + timer1);  
    (digitalWrite(led, HIGH));

  }
  if (timing1 == time) (digitalWrite(led, LOW));



  if (b2 == 0)  
  {
    timing2 = (time + timer2);  
    (digitalWrite(led, HIGH));
    Serial.println("timing2");
    Serial.println(timing2);
  }
  if (timing2 == time)   
    (digitalWrite(led1, LOW));

}

Serial monitor prints out timing = 0, i guess theres problem with calculating that value?

what are you trying to do, it isn't really obvious in your code.

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().

something like this?

int led1 = 13;
int led2 = 12;
const int button1 = 2; 
const int button2 = 3;
int lastB1;
int lastB2;
unsigned long startTime1;
unsigned long startTime2;
unsigned long timer1 = 5000;
unsigned long timer2 = 10000;

void setup() {      
  Serial.begin(9600);
  pinMode(led1, OUTPUT);   
  pinMode(led2, OUTPUT);   
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
}

void loop() 
{
  int b1 = digitalRead(button1);
  if (b1 == LOW && lastB1 == HIGH) 
  { 
    startTime1 = millis();
    digitalWrite(led1, HIGH);
  }
  lastB1 = b1;
  //
  if (millis() - startTime1 > timer1)
  {
    digitalWrite(led1, LOW);
  }
  //
  int b2 = digitalRead(button2);
  if (b2 == LOW && lastB2 == HIGH ) 
  {
    startTime2 = millis();
    digitalWrite(led2,HIGH);
  } 
  lastB2 = b2;
  if (millis() - startTime2 > timer2)
  {
    digitalWrite(led2, LOW);
  }
}

BulldogLowell:
something like this?

works fine, thank you.

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.

the way I wrote it you will not have that problem :blush:

using unsigned song subtraction in a mills timer

What happens if you use a signed song? What IS a signed, or unsigned, song?

he he :blush:

my typo could have bee a lot worse!

PaulS:
What happens if you use a signed song? What IS a signed, or unsigned, song?

Well, an unsigned song is a positive one, whereas a signed song might be kind-of negative.

This is all pretty obvious stuff. :wink: