int to unsigned long

Status
Not open for further replies.

Bastiaan

Well-known member
Hi guys I tried optimizing the code,
And would like to get rid of delays.

I use the encoder.h and put the encoder value into a If statement and switch off and on a led for a period of time. If have done it with delays but it’s kinda buggy.

Code:
Int OnTime=1000; Offtime=1000;// independent values

Unsigned long PreviousMillis=0;

Void setup()
{}

Void loop()
{

unsigned long newMillis= millis();
If(Ledstate==HIGH)
{
If((newMillis-PreviousMillis)>=OnTime)
{
Ledstate==LOW;
}
PreviousMillis=newMillis();
}

Else
{
If((newMillis-PreviousMillis)>=Offtime)
{
Ledstate==HIGH;
}
PreviousMillis=newMillis();
}

}

Could someone explain how to wrap the two integers? Because the digital pin for on/off shows miniature random spikes of a few microseconds.

I tried to wrap the if statement in the beginnin with
Code:
 if((unsigned long) do something...) ,
I tried wrapping the int itself,
I tried reformulating the if statements based on time stamp and when the millis() is overloading.
should I make a different newMillis for the OnTime?
 
akward, it bounces in the beginning then it stays high and blips at the 7seconds to Zero. (if OFFtime is 7 seconds that is), and is repeated.

ElapsedOFF and ElapsedON are global Elapsedmillis();

what the idea is based on the adafruit, which works if unsigned Longs are used...
https://learn.adafruit.com/multi-tasking-the-arduino-part-1/using-millis-for-timing

I tried to understand it,

I Applied the ElapsedMillis() from Paul Stoffregen, which works with the serial monitor. but I am not getting the curve...

thanks for looking in to it.



Code:
if((digitalRead(14)==HIGH)&&(ElapsedOFF>=OFFTime1))
  {
      
          
          // ElapsedOFF=ElapsedOFF-OFFTime1;
          Serial.print(" Elapsed ");
          Serial.print(ElapsedOFF);
          Serial.print(" OFFTime1 ");
          Serial.print(OFFTime1);
          Serial.print(" ElapsedOFF-OFFTime1 ");
          Serial.println(ElapsedOFF-OFFTime1);
          digitalWrite(14,LOW); digitalWrite(20,LOW);digitalWrite(19,HIGH);
          ElapsedOFF=ElapsedOFF-OFFTime1;
  }
  else if((digitalRead(14)==LOW)&&(ElapsedON>=ON_Time1))
  {
          
      
      
           
          Serial.print(" Elapsed ");
          Serial.print(ElapsedON);
          Serial.print(" ON_Time1 ");
          Serial.print(ON_Time1);
          Serial.print(" ElapsedON-ON_Time1 ");
          Serial.println(ElapsedON-ON_Time1);
          
          
    //    delay(OFFTime1+minimumLT3760);
        digitalWrite(14,HIGH); digitalWrite(20,HIGH);digitalWrite(19,LOW);
    //    delay(ON_Time1+minimumLT3760);
         ElapsedON=ElapsedON-ON_Time1;
        }
 
The counters are always counting. While you are timing the HIGH time, the LOW counter is counting too. When the HIGH time expires, the LOW time could already be expired.
You should set the alternate timer to zero instead of what you are doing.

Code:
if () {
   blah 
   blah
   blah
   ElapsedON = 0;
else if(){
   something
   something
   ElapsedOFF = 0;
}
 
The counters are always counting. While you are timing the HIGH time, the LOW counter is counting too. When the HIGH time expires, the LOW time could already be expired.
You should set the alternate timer kodi to zero instead of what you are doing.

Code:
if () {
   blah 
   blah
   blah
   ElapsedON = 0;
else if(){
   something
   something
   ElapsedOFF = 0;
}

Just what I needed, worked a treat, thank you!


this work perfectly.

thank you so much
 
Status
Not open for further replies.
Back
Top