1

I am trying to make a workaround for the delay function. I am trying to use the function in the Analog Write Mega example, by replacing delay with the function i made. However, whenever i use my function instead of delay i get incorrect led behaviour.

Here is the link to the example:

https://www.arduino.cc/en/Tutorial/AnalogWriteMega

Here is my code:

int currmillis = 0; //used in my function to find the current millis()
int prevmillis = 0; //used to hold previous value of currmillis
int boolval = 0;    //used to control whether to write the brightness value to the led or not
int time = 0;     //used in the delay function, difference between currmillis and prevmillis

int timeloop(int); //the function i wrote, it is at the bottom of the code

void setup() {
  Serial.begin(9600);
  for ( int i = 2; i <= 13; i++)
  {
        pinMode(i,OUTPUT); 
  }
}//Setting the pins to output and allowing serial communication.


void loop (){

  for ( int thisPin = 2; thisPin <= 13; thisPin++ ) //to move from one led to the next
  { 

    for ( int brightness = 0 ; brightness <= 255; brightness++) //change the brightness of the led
    {

      boolval = timeloop(2); //the delay function returns the time passed, if the time is greater than or equal to 2, the analogWrite will write the brightness to the led

      if (boolval >= 2){ 
            analogWrite(thisPin, brightness);   
      }// if 2 ms pass, the led brightness will increase from 0 to 255 and then decrease in the following loop from 255 to 0, Once it reaches 0, the main loop moves to the next led.

    }

    for ( int brightness = 255 ; brightness >= 0 ; brightness--)
    {
      boolval = timeloop(2);

      if (boolval >= 2){ 
            analogWrite(thisPin, brightness);  
      }
    }
          timeloop(100); // a delay of 100 ms using the function
  }
}

int timeloop (int interval){ // the delay function

  do{
    prevmillis = currmillis; //hold previous value
    currmillis = millis(); //find current value 
    time = (currmillis-prevmillis); 
  }while(time < interval); 

  return time;
}

Thank you

1
  • Your function is a pure delay. Why do you want to rewrite it? What different behaviour do you want?
    – frarugi87
    Oct 9, 2015 at 10:11

3 Answers 3

1
// You don't need to return anything since it's just a delay
// also I changed the parameters to long ints because millis()
// returns a long int
void timeLoop (long int startMillis, long int interval){ // the delay function

    // this loops until 2 milliseconds has passed since the function began
    while(millis() - startMillis < interval){} 
}

// Your call for a 2 millisecond delay would be
timeLoop(millis(), 2);

Also the leds may be acting weird because this schematic is wrong. As you can see one of the led's anode isn't connected to ground. It's circled in red. enter image description here

If you really want it to return something you could use this:

long int timeLoop (long int startMillis, long int interval){ // the delay function

    while(millis() - startMillis < interval){} 
    return millis() - startMillis;
}

But it really isn't needed.

1
  • thank you - seems super hard to find functional functions on arduino sites Mar 8, 2018 at 18:49
0

What's the point? Why rewrite delay()?

Anyway:

int timeloop (int interval){ // the delay function

  do{
    prevmillis = currmillis; //hold previous value
    currmillis = millis(); //find current value 
    time = (currmillis-prevmillis); 
  }while(time < interval); 

  return time;
}

In every iteration of that loop you are updating prevmillis. So you will never reach the wanted delay interval.

Apart from the fact that you may as well use the inbuilt delay rather than writing your own, and getting it wrong.


I am trying to make a workaround for the delay function

Why?

The XY Problem

4
  • The "Why" part interested me here, because I'm in the process of changing my delay(1000) to a millis() interval. Why? because I want a more accurate timer for sending data via serial. I need the processing time to be deducted from the one second delay, so the interval between loops is as close to 1 second as possible. That's fair and achievable, right?
    – n00dles
    Jun 7, 2017 at 12:05
  • "W": Because I think that is more sensible than sending and displaying data at an arbitrary rate, as my application involved scheduling to real-world time. "V": So that a user can schedule an event as part of the automation program! "U": Because... ..."F-22": Because I wasn't breast fed!... ..."M-6.8328*10^11": Because the big bang singularity occurred! "L-6.8328*10^11": Unknown.
    – n00dles
    Jun 7, 2017 at 12:50
  • What? Did the cat get loose on the keyboard?
    – Nick Gammon
    Jun 9, 2017 at 5:50
  • lol. Do you get it? It's my extension of "The XY Problem"!
    – n00dles
    Jun 9, 2017 at 20:02
0

Depending on what you want the alternative function to do, there are a megaton of timer libraries at the Arduino playground. One of them is likely to provide what you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.