I need help with millis() command

Hi, I am building intervalometer for my Canon EOS 70D. I need some code to work in background and some code to have delays, but delay function freezes the whole processor so I need to use millis() function. I don't understand how to make it work. I want to control the duration with a single button (I have programmed it works, but if the timer is active the button does not work because of the delay function) In code I have changed some things from delay to millis(). I think you got it what I mean. Help me. :cry:

int buttonPushCounter = 1;
int buttonState = 1;
int lastButtonState = 1;


const unsigned long eventInterval = 1000;
const unsigned long eventInterval1 = 5000;
const unsigned long eventInterval2 = 10000;
const unsigned long eventInterval3 = 30000;
const unsigned long eventInterval4 = 60000;
unsigned long previousTime = 0;


void setup() {
  Serial.begin(9600);

  pinMode(15, INPUT_PULLUP);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
}

void loop() {

  unsigned long currentTime = millis();

  buttonState = digitalRead(15);
  if (buttonState != lastButtonState) {
    if (buttonState == LOW) {
      buttonPushCounter++;
    }
  }


if (buttonPushCounter == 1 || buttonPushCounter == 2 || buttonPushCounter == 3 || buttonPushCounter == 4 || buttonPushCounter == 5){
    digitalWrite(3, HIGH);
    digitalWrite(LED_BUILTIN, HIGH);
    Serial.println("trigger active");
  }

  if (digitalRead (3 == HIGH));{
  delay(500);
  digitalWrite(3, LOW);
  digitalWrite(LED_BUILTIN, LOW); //LED may be omitted if expedient (low light, etc)
  Serial.println("trigger inactive");
  }

  //Timers

  if (buttonPushCounter == 1);{

    /* Event code */
    digitalWrite(3, HIGH);
    delay(100);
    digitalWrite(3, LOW);
    
   /* Update the timing for the next time around */
    previousTime = currentTime;
  
}
  
  if (buttonPushCounter == 2)eventInterval1;{
    if (currentTime - previousTime >= eventInterval1) 
    /* Event code */
    digitalWrite(3, HIGH);
    
   /* Update the timing for the next time around */
    previousTime = currentTime;
  }

  
  if (buttonPushCounter == 3)eventInterval2;{
    if (currentTime - previousTime >= eventInterval2) 
    /* Event code */
    digitalWrite(3, HIGH);
    
   /* Update the timing for the next time around */
    previousTime = currentTime;
  }

  
  if (buttonPushCounter == 4)eventInterval3;{
      if (currentTime - previousTime >= eventInterval3) 
    /* Event code */
    digitalWrite(3, HIGH);
    
   /* Update the timing for the next time around */
    previousTime = currentTime;
  }

  
  if (buttonPushCounter == 5)eventInterval4;{
    if (currentTime - previousTime >= eventInterval4) 
    /* Event code */
    digitalWrite(3, HIGH);
    
   /* Update the timing for the next time around */
    previousTime = currentTime;
  }

  //LED CONTROL

  //LED 1

  if (buttonPushCounter == 2) {
    digitalWrite(6, HIGH);
  }

  if (buttonPushCounter == 6) {
    digitalWrite(6, LOW);
  }

  //LED2

  if (buttonPushCounter == 3) {
    digitalWrite(7, HIGH);
  }

  if (buttonPushCounter == 6) {
    digitalWrite(7, LOW);
  }

  //LED3

  if (buttonPushCounter == 4) {
    digitalWrite(8, HIGH);
  }

  if (buttonPushCounter == 6) {
    digitalWrite(8, LOW);
  }

  //LED4

  if (buttonPushCounter == 5) {
    digitalWrite(9, HIGH);
  }

  if (buttonPushCounter == 6) {
    digitalWrite(9, LOW);
  }

  //End LED control
  //............................................................
  //Reset Counter

  if (buttonPushCounter == 6) buttonPushCounter = 1;

  //End Reset Counter
  //............................................................


  lastButtonState = buttonState;
  Serial.println(buttonPushCounter);
}

Demo.ino (3.3 KB)

You appear to have a semicolon at the end of an "if".
Don't do that.

And don't post pictures of code, post code.
In code tags.

Hi modisu,

I'm sorry I don't fully understand what the behaviour shall be.
Please extend your description in this kind of way:
Please give this outputs selfexplaining names

use self-explaining names NOT output 3 output 6

output 3, output 6 etc. are connected to something. Name this "somethings"

I want to control multiple IO-pins configured as output to be high or low for different times.

program starts
button is pushed for the first time: output "X" shall be switched to HIGH

button is pressed for the second time: after "B" seconds output "Y" shall be switched LOW

button is pressed for the thrid time: ......

in addition your codeline

if (buttonPushCounter == 2)eventInterval1;{

does this even compile?

what is eventInterval1???

You seem to have some misconceptions about how programming works.
Because of this it is really important to describe the programs behaviour in normal words.

descriptions as code have the danger of beeing misunderstood because of the possible misconceptions

best regards Stefan

Please follow the advice on posting a programming question given in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

You just want a pin to go high every X-amount of time? Like.. blinking?

-jim lee

Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R

jimLee:
You just want a pin to go high every X-amount of time? Like.. blinking?

-jim lee

HI, yes you are correct

StefanL38:
Hi, Stefan I have edited the file...

int buttonPushCounter = 1;

int buttonState = 1;
int lastButtonState = 1;

const unsigned long eventInterval1 = 1000;//Interval 1 second
const unsigned long eventInterval2 = 5000;//Interval 5 seconds
const unsigned long eventInterval3 = 10000;//Interval 10 seconds
const unsigned long eventInterval4 = 30000;//Interval 30 seconds
const unsigned long eventInterval5 = 60000;//Interval 60 seconds
unsigned long previousTime = 0;

const int BUTTON = 15;//Button to conrol interval
const int LED_1 = 6;//Led 1
const int LED_2 = 7;//Led 2
const int LED_3 = 8;//Led 3
const int LED_4 = 9;//Led 4
const int OUT = 3;//Output to camera

void setup() {
 Serial.begin(9600);

pinMode(BUTTON, INPUT_PULLUP);//Button to conrol interval
 pinMode(LED_1, OUTPUT);//Led 1
 pinMode(LED_2, OUTPUT);//Led 2
 pinMode(LED_3, OUTPUT);//Led 3
 pinMode(LED_4, OUTPUT);//Led 4
 pinMode(OUT, OUTPUT);//Output to camera
}

void loop() {

unsigned long currentTime = millis();

buttonState = digitalRead(BUTTON);
 if (buttonState != lastButtonState) {
   if (buttonState == LOW) {
     buttonPushCounter++;
   }
 }

if (buttonPushCounter == 1 || buttonPushCounter == 2 || buttonPushCounter == 3 || buttonPushCounter == 4 || buttonPushCounter == 5){
   digitalWrite(OUT, HIGH);
   digitalWrite(LED_BUILTIN, HIGH);
   Serial.println("trigger active");
 }

if (digitalRead (OUT == HIGH));{
 delay(50);
 digitalWrite(OUT, LOW);
 digitalWrite(LED_BUILTIN, LOW); //LED may be omitted if expedient (low light, etc)
 Serial.println("trigger inactive");
 }

//Timers

if (buttonPushCounter == 1)eventInterval1;{
   if (currentTime - previousTime >= eventInterval1)

/* Event code /
   digitalWrite(OUT, HIGH);
   Serial.println("Selected button1");  
     
  /
Update the timing for the next time around /
   previousTime = currentTime;
 
}
 
 if (buttonPushCounter == 2)eventInterval2;{
   if (currentTime - previousTime >= eventInterval2)
   /
Event code /
   digitalWrite(OUT, HIGH);
   Serial.println("Selected button2");
   
  /
Update the timing for the next time around */
   previousTime = currentTime;
 }

if (buttonPushCounter == 3)eventInterval3;{
   if (currentTime - previousTime >= eventInterval3)
   /* Event code /
   digitalWrite(OUT, HIGH);
   
  /
Update the timing for the next time around */
   previousTime = currentTime;
 }

if (buttonPushCounter == 4)eventInterval4;{
     if (currentTime - previousTime >= eventInterval4)
   /* Event code /
   digitalWrite(OUT, HIGH);
   
  /
Update the timing for the next time around */
   previousTime = currentTime;
 }

if (buttonPushCounter == 5)eventInterval5;{
   if (currentTime - previousTime >= eventInterval5)
   /* Event code /
   digitalWrite(OUT, HIGH);
   
  /
Update the timing for the next time around */
   previousTime = currentTime;
 }

//LED CONTROL

//LED 1

if (buttonPushCounter == 2) {
   digitalWrite(LED_1, HIGH);
 }

if (buttonPushCounter == 6) {
   digitalWrite(LED_1, LOW);
 }

//LED2

if (buttonPushCounter == 3) {
   digitalWrite(LED_2, HIGH);
 }

if (buttonPushCounter == 6) {
   digitalWrite(LED_2, LOW);
 }

//LED3

if (buttonPushCounter == 4) {
   digitalWrite(LED_3, HIGH);
 }

if (buttonPushCounter == 6) {
   digitalWrite(LED_3, LOW);
 }

//LED4

if (buttonPushCounter == 5) {
   digitalWrite(LED_4, HIGH);
 }

if (buttonPushCounter == 6) {
   digitalWrite(LED_4, LOW);
 }

//End LED control
 //............................................................
 //Reset Counter

if (buttonPushCounter == 6) buttonPushCounter = 1;

//End Reset Counter
 //............................................................

lastButtonState = buttonState;
 Serial.println(buttonPushCounter);
}

if (digitalRead (OUT == HIGH));Oops2

TheMemberFormerlyKnownAsAWOL:

if (digitalRead (OUT == HIGH));

Oops2
?? What do you mean ??

digitalRead() takes a pin number as its argument
What have you given it ?

See also reply #1

UKHeliBob:
digitalRead() takes a pin number as its argument
What have you given it ?

I described what does the pin OUT mean like that:

const int OUT = 4;//Output to camera

Then set pinMode in void setup like that:

pinMode(OUT, OUTPUT);//Output to camera

And then you digitalRead it . . how?

OK, but what about how you try to use digitalRead()
Please explain how it reads the OUT pin

UKHeliBob:
OK, but what about how you try to use digitalRead()
Please explain how it reads the OUT pin

I read it like this:

if (digitalRead (OUT == HIGH)){

delay(50);
digitalWrite(OUT, LOW);
digitalWrite(LED_BUILTIN, LOW); //LED may be omitted if expedient (low light, etc)
}




And i use this code to add +1 to the counter:





buttonState = digitalRead(BUTTON);
if (buttonState != lastButtonState) {
    if (buttonState == LOW) {
buttonPushCounter++;
}
}




It compiles sucsesfuly.

digitalRead (OUT == HIGH))Look.
Carefully.

TheMemberFormerlyKnownAsAWOL:

digitalRead (OUT == HIGH))

Look.
Carefully.

I don't understand you... What do you mean

What is the value of OUT?
The value of HIGH is 1.
Do you think they are ever going to be equal?

If they are not, which pin are you going to read?
Hint: the value of 'false'' is 0.

Please stop including your reply inside the quote.

It compiles sucsesfuly.

So?

It makes the out HIGH using this code line

 if (digitalRead (OUT == HIGH)){
  delay(5);
  digitalWrite(OUT, LOW);
  digitalWrite(LED_BUILTIN, LOW); //LED may be omitted if expedient (low light, etc)
  }

  //Timers

  if (buttonPushCounter == 1)eventInterval1;{
    if (currentTime - previousTime >= eventInterval1)

    /* Event code */
    digitalWrite(OUT, HIGH);
    digitalWrite(LED_BUILTIN, HIGH);
      
   /* Update the timing for the next time around */
    previousTime = currentTime;
  
}
digitalWrite(OUT, HIGH);