Pages: [1]   Go Down
Author Topic: help with delay  (Read 369 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi.I am new in Arduino.i write small dimming program.when I press button led brightness incresses from 0 to 255 and stays at 255 (pwm=255).when I press button again led brightness decreses from 255 to 0 and stays at 0 (pwm=0).I use delay function.but I want change DELAY function with something like this if(currentMillis - dimMillis > 25) .but programs stops working correctly.please help me
Code:
#include <Button.h>

Button button = Button(9,BUTTON_PULLDOWN);
int ledPin=10;
int ledState=0;

void setup()
{
pinMode(ledPin,OUTPUT);
Serial.begin(9600);
}

void loop()
{
 
   if(button.uniquePress())
   {
   ledState = !ledState;
   Dimmer(ledPin,ledState);
   }
   
}

/**********Dimmer Function**************/
void Dimmer (int pin, int x)
{
 
  if(x==1)
  {
    Serial.println("Dimming up");
    for(int var=0 ; var<255 ; var++)
    {
     analogWrite(pin,var);
     delay(25);
    }
  }
 
  else
  {
    Serial.println("Dimming down");
    for(int var=255 ; var>0 ; var--)
    {
     analogWrite(pin,var);
     delay(25);
    }
  }
}



i tried likes this bit it crashes
Code:
#include <Button.h>

Button button = Button(9,BUTTON_PULLDOWN);
int ledPin=10;
int ledState=0;

unsigned long currentMillis;
unsigned long dimMillis;

void setup()
{
pinMode(ledPin,OUTPUT);
Serial.begin(9600);
}

void loop()
{
   currentMillis = millis();
   
   if(button.uniquePress())
   {
   ledState = !ledState;
   Dimmer(ledPin,ledState);
   }
   
}

/**********Dimmer Function**************/
void Dimmer (int pin, int x)
{
 
  if(x==1)
  {
    Serial.println("Dimming up");
    for(int var=0 ; var<255 ; var++)
    {
     if(currentMillis - dimMillis > 25)
     {
        dimMillis+=25;
        analogWrite(pin,var);
      }
    }
  }
 
  else
  {
    Serial.println("Dimming down");
    for(int var=255 ; var>0 ; var--)
    {
     if(currentMillis - dimMillis > 25)
     {
        dimMillis+=25;
        analogWrite(pin,var);
     }
    }
  }
}
Logged

Croatia
Offline Offline
Sr. Member
****
Karma: 12
Posts: 492
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When your program starts dimMillis has value 0.
You never assign it anything else, so if(currentMillis - dimMillis > 25) after approximately 6 seconds of code running always returns true.
Try updating it here:
Code:
if(button.uniquePress())
   {
   ledState = !ledState;
   Dimmer(ledPin,ledState);
   }
Logged

Lost Wages
Offline Offline
Full Member
***
Karma: 11
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your dimmer function looks to me to be comparing apples and oranges.

Code:
 if(x==1)
  {
    Serial.println("Dimming up");

    for(int var=0 ; var<255 ; var++)
    {
     if(currentMillis - dimMillis > 25)
     {
        dimMillis+=25;
        analogWrite(pin,var);
      }
    }
  }

The code enters a for/loop which is going to iterate 255 times -- no matter what
Inside this loop you subtract dimMillis from currentMillis and test if the result is greater than 25.
The currentMillis was read at the top of the main Loop(), while dimMillis will be all over the place -- sometimes smaller, sometimes larger. Thevalue is never reset to zero anywhere and, being a global variable retains whatever value that it last had.

When dimMillis is smaller then it gets incremented and you write the for/loop index to the LED pin.  When dimMillis is larger -- no writes to the LED.
I can't divine your intention from the code, but have to think that this is your problem.

Have a look and let us know.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When you replace blocking calls to delay() with non-blocking code that checks the value of millis(), you have to change the structure of your code fundamentally.

When you use delay(), you can write code that carries out a sequence of actions and your position within the code corresponds to your position within the sequence.

When remove the blocking calls to delay(), you need to provide a different way to know where you are within the sequence. Usually, this means creating a state variable.

For example:
Code:
{
  for(int i = 0; i < 255; i++)
  {
    doSomething(i);
    delay(INTERVAL);
  }
}

Could become:

Code:
{
  static unsigned long lastTime = millis();
  if(millis() - lastTime >= INTERVAL)
  {
    lastTime += INTERVAL;
    if(position < 255)
    {
      position++;
      doSomething(position);
    }
  }
}

You see that converting the loop to a non-blocking structure involved replacing the loop counter i with a global variable to remember where you have got up to. Similarly, if you had code that needed to carry out a sequence of actions then you could use a state machine to record which action was being performed.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Lost Wages
Offline Offline
Full Member
***
Karma: 11
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PeterH,

A much more informational and eloquent answer than mine. Nicely put. Karma bump.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for answerin.i tried how you say.but it only toggles.
Code:
#include <Button.h>

Button button = Button(9,BUTTON_PULLDOWN);
int ledPin=10;
int ledState=0;

void setup()
{
pinMode(ledPin,OUTPUT);
Serial.begin(9600);
}

void loop()
{
 
   if(button.uniquePress())
   {
   ledState = !ledState;
   Dimmer(ledPin,ledState);
   }
   
}

/**********Dimmer Function**************/
void Dimmer (int pin, int x)
{
 
  if(x==1)
  {
    Serial.println("Dimming up");
    static unsigned long lastTime = millis();
    if(millis() - lastTime >= 25)
    {
      lastTime += 25;
      for(int var=0 ; var<255 ; var++)
      {
       analogWrite(pin,var);
      }
    }
   }
 
  else
  {
    Serial.println("Dimming down");
    static unsigned long lastTime = millis();
    if(millis() - lastTime >= 25)
    {
      lastTime += 25;
       for(int var=255 ; var>0 ; var--)
       {
         analogWrite(pin,var);
       }
    }
  }
}
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3375
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Get rid of the for loop. The blink without Delay concept doesn't work with a For loop.
Logged

Pages: [1]   Go Up
Jump to: