Blink Without delay help again.

Code in reply #19 is much better. AWOL is right, CurrentMillis could be local to loop().

i know AWOL, but what I'm trying to achieve here is that the on time is different with the off time, the implementation if this little exercise have a very really application in the future, i am trying to simplify stuff to the very basic so that i can program in the simplest term and when its time for me to make the project at least i have a good(if any) understanding of how to do it. so would this be better?

const uint8_t Led=13;
boolean LedState=LOW;
unsigned long PreviousMillis=0;
unsigned long TimerOFF=1000;
unsigned long TimerON=100;

void setup()
{
  pinMode(Led,OUTPUT);
}
void loop()
{
  unsigned long CurrentMillis=millis();
  if (LedState==LOW)
  {
    if (CurrentMillis-PreviousMillis>=TimerON)
    {
      PreviousMillis=CurrentMillis;
      LedState=HIGH;
    }
  }
  else
  {
    if (CurrentMillis-PreviousMillis>=TimerOFF)
    {
      PreviousMillis=CurrentMillis;
      LedState=LOW;
    }
  }
  digitalWrite(Led,LedState);
}

but what I'm trying to achieve here is that the on time is different with the off time,

...which is exactly what happens with the example if you change the value of "interval" when you change the state of the LED.

Awol,
It is to my limited understanding of C++/C programming that would say in my humblest opinion that the interval only set the duration between how long it will turn on/off.
lets say that i change the interval to 2000
it would mean that the led will turn on for 2 second and then turn off for 2 second and the cycle will repeat it self.
what I'm trying to achieve now is to turn the led on for maybe 3 second and then off for maybe 5 second.
I hope that my explanation is correct and I will be glad if I'm wrong cause only then i could improve my understanding and my self as a whole.

Ash I think AWOL's point is this...

If you change interval on the fly, at the time you change the LED state, the interval will be correct for the next pass with the LED in the new (on or off) state and will time that on or off section correctly. Then next time the LED state changes, the interval will get changed for that new (off or on) state. Difficult to explain in words.... yet another case where a flowchart will help.

No flowchart required -

unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
      interval = ON_TIME;
    }
    else
    {
      ledState = LOW;
      interval = OFF_TIME;
    }

ok now i get his point. haha sorry AWOL for not understanding what you meant by that. urm let me try to make it as been pointed out,

const int ledPin =  13;
int ledState = LOW;
long previousMillis = 0;
int Count=0;
unsigned long interval;

void setup() 
{
  pinMode(ledPin, OUTPUT);      
}

void loop()
{
 
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) 
  {
    previousMillis = currentMillis;   
    ledState=!ledState;
    digitalWrite(ledPin, ledState);
    Count++;
  }
  if(Count%2==0)
  {
    interval = 100UL;
  }
  else
  {
    interval =2000UL;
  }
}

is this something that you meant?

ok im far off then what you mean and yeah its great. i learn alot today/tonight

ash901226:
i learn alot today/tonight

And that's the beauty of a forum like this.... as long as we do learn!

is this something that you meant?

Yes, but not so complicated.

is it true that i read from somewhere that comparison is easier on arduino then using modulo and what not?

Forget code optimizations. Write code that works and is readable / maintainable. That's the first form of optimization. Low level optimization is better left off to the compiler.

i guess your rite tuxduino,
actually the real reason for me to make the blink without delay with variable on off time is so that i could make my own code to control a 6 axis Mill. i know big ambition rite. but hay its like what they say in my langguage " Impikan langgit supaya bila jatuh dapat awan" its something like hope for the sky if you fall your atleast in the clouds if im not mistaken. my stepper motor controller is using Step/direction control. i have try countless of library. in my opinions all have its pros and cons. but i know that maybe i could give it a try who knows maybe i can make my own controller, well if i fail atleast i will have learn alot i know some of you thinking while reading this why invent the wheel again and again. just couse i want to learn and nothing more.

Reinventing the wheel is one (good) way to learn, everyone has done that, at least once.
When tackling a bigger project, though, it's far better to learn how to use others' wheels. That is, libraries. When you put together buttons, blinking leds, stepper motors and some sensors, you'll go nowhere without libraries.

well i hope so, so now my problem is how to make it count the number of on it have done.

ash901226:
ok guys could you all help me to turn this into a class?
heres what i think so far

class Blink

...




would this be correct ? it does compile but im lost when i get to this part.

Hi Ash, did you not see my reply to your post in "Arduino Class C++"?

John

i got the count part working now

unsigned long OFF_TIME=1000;
unsigned long ON_TIME=100;

const int ledPin =  53;
int ledState = LOW;
long previousMillis = 0;
int Count=0;
unsigned long interval;

void setup() 
{
  pinMode(ledPin, OUTPUT);      
}

void loop()
{
  if (Count<=10)
  {
    unsigned long currentMillis = millis();

    if(currentMillis - previousMillis > interval) {
      // save the last time you blinked the LED 
      previousMillis = currentMillis;   

      // if the LED is off turn it on and vice-versa:
      if (ledState == LOW) {
        ledState = HIGH;
        interval = ON_TIME;
        Count++;
      }
      else
      {
        ledState = LOW;
        interval = OFF_TIME;
      }
      digitalWrite(ledPin,ledState);
    }
  }
  else
  {
    digitalWrite(ledPin,LOW);
  }
}

now would be a great time to start varying the off time

don't get me wrong johncc, i saw your answer at it work great but i saw a fetal flaw in using switch statement that's why i turn it into a if statement program. Btw thanks it works great and you know what i had been turning Steppers using your program.

PaulS:
...
The constructor will be called before the init() function is called. It is the init() function that sets up the hardware, so calling pinMode() before init() is a waste of time.
...

I thought of that but found that pinMode() was effective. This was on a Teensy 2 btw, so I'm not sure if its the same on an Uno, etc.

I suppose it's "undefined" or uncertain whether it should/could/would work or not, so I agree a separate setup() method is best.

Cheers,
John

Blinker.ino (1.44 KB)