How to create several timed millis() events?

Hello, I am working on a binary counter with 5 LEDs in it. I am using the millis() function so that every half an hour the LED increases a float by 0.5 in value. Each float represents a set for the coding of the binary counter. However, in the middle of the half an hour, if anytime a person pushes a toggle button, then the integer will increase in value by 0.5, causing the counter to move one number higher. The problem with my millis() function however, is that when I code readVal==0; the integer value does increase.

However, the other blocks of code propagate the condition. I want my integer value to increase evenly

like 9, push, 9.5. 9.5, push, 10. 10, push, 10.5.

However the integer value directly goes from 9 to 10.5 in a single push. How can I achieve separate timed events with the millis function?

int pin1 = 12;
int pin2 = 11;
int pin3 = 10;
int pin4 = 9;
int pin5 = 8;
int readVal;
float timE=9;
unsigned long dt1 = 180000;
unsigned long dt2 = 5760000;
unsigned long preTime=0; 
void setup() {
  // put your setup code here, to run once:
  pinMode(A0, INPUT);
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin4, OUTPUT);
  pinMode(pin5, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  
   unsigned long curTime=millis();
   Serial.print(" The time now is: ");
   Serial.println(timE);
   Serial.print(", ReadVal is: ");
   readVal=digitalRead(A0);
   Serial.println(readVal); 
   delay(300);
   if (timE==9){
    digitalWrite(pin1, LOW);
    digitalWrite(pin2, LOW);
    digitalWrite(pin3, LOW);
    digitalWrite(pin4, LOW);
    digitalWrite(pin5, HIGH);
    if(readVal==0){
      timE=timE+0.5;
      
    }
    if(curTime-preTime>=dt1){
      timE=9.5;
      preTime=curTime;
      
      
    }
   }
   

   if (timE==9.5){
    digitalWrite(pin1, LOW);
    digitalWrite(pin2, LOW);
    digitalWrite(pin3, LOW);
    digitalWrite(pin4, HIGH);
    digitalWrite(pin5, LOW);
    if(readVal==0){
      timE=timE+0.5;
    }
    if(curTime-preTime>=dt1){
      timE=10;
      preTime=curTime;
      
    }
   }
   
   
   if (timE==10){
    digitalWrite(pin1, LOW);
    digitalWrite(pin2, LOW);
    digitalWrite(pin3, LOW);
    digitalWrite(pin4, HIGH);
    digitalWrite(pin5, HIGH);
    if(readVal==0){
      timE=timE+0.5;
    }
    if(curTime-preTime>=dt1){
      timE=timE+0.5;
      preTime=curTime;
      
      
    }
   }
     
  }

Thanks for your help!

Hi,
Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

How have you got your button wired?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Hi,
Is this a school project?

SAME CODE?

Tom.. :smiley: :+1: :coffee: :australia:

I'd use a large integer datatype, increment by one, and test for odd/even

Hi Tom, here it is
image

Also, the diagram shows an Uno Board, but I couldnt find a Mega 2560 on the website. Hope you could help, thank you

Oh that seems feasible. Could I get to know how to test for the odd/even coding?

Yes we are working together

The long way is to divide by two and look for a remainder (modulo arithmetic)
The quick way is to test the LSB.

Hi.
Replace that 330R with a more reasonable 10K.
Or remove it and use.

pinMode(A0, INPUT_PULLUP);

This will turn on the internal pull up resistor.

You may also be experiencing contact bounce on the button.

Tom.. :smiley: :+1: :coffee: :australia:
PS. Please answer post #3.

1 Like

Yes sir

None of them seem to work, how can I solve the code?
thank you

Are you the same person in the other thread?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

I'm overloaded if I have to serve two identical threads at the same time, so I'm out.

Three non-blocking timers with different intervals

an integer-value can

NEVER

go from 9 to 10.5

integer means

NO DECIMAL-POINT

integers are counting 1,2,3,4,5,6,...9,10,11,12...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.