Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: int to unsigned long

  1. #1

    int to unsigned long

    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?

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,171
    Using elapsedMillis makes this much simpler.

    https://www.pjrc.com/teensy/td_timin...pedMillis.html

  3. #3
    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-tas...lis-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;
            }

  4. #4
    Senior Member
    Join Date
    May 2017
    Posts
    208
    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;
    }

  5. #5
    slap myself for the head, I assumed... big mistake.

    Thanks!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •