Blink without Delay at driffent times as on and off ?

Blink without Delay

Im trying to use the Blink without Delay code but it gose on for 1000; and off for 1000; how can i make it go on for 300; and off for 1000; ?

 This example code is in the public domain.
 
 
  http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
  */
 
// constants won't change. Used here to 
// set pin numbers:
 const int ledPin =  13;      // the number of the LED pin
 
// Variables will change:
 int ledState = LOW;             // ledState used to set the LED
 long previousMillis = 0;        // will store last time LED was updated
 
// the follow variables is a long because the time, measured in miliseconds,
 // will quickly become a bigger number than can be stored in an int.
 long interval = 1000;           // interval at which to blink (milliseconds)
 
void setup() {
   // set the digital pin as output:
   pinMode(ledPin, OUTPUT);      
 }
 
void loop()
 {
   // here is where you'd put code that needs to be running all the time.
 
  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
   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;
     else
       ledState = LOW;
 
    // set the LED with the ledState of the variable:
     digitalWrite(ledPin, ledState);
   }
 }
  • find the 7 differences :wink: (code not tested)
#define OFFTIME 1000
#define ONTIME 300
unsigned long interval = 1000;           // interval at which to blink (milliseconds)

const int ledPin =  13;     
int ledState = LOW;     
long previousMillis = 0;  
 
void setup() 
{
   pinMode(ledPin, OUTPUT);      
 }
 
void loop()
{
  unsigned long currentMillis = millis();
  
  if (currentMillis - previousMillis > interval) 
  {
    previousMillis = currentMillis;   

    if (ledState == LOW)
    {
      ledState = HIGH;
      interval = ONTIME;
    }
    else
    {
      ledState = LOW;
      interval = OFFTIME;
    } 
    digitalWrite(ledPin, ledState);
  }
}

Thanks a lot i just tryed you code and it works realy great just what i was after :smiley: so i came up with this one with 3 LEDs flashing at driffernt Times :smiley:

#define OFFTIME 2000
#define ONTIME 300
unsigned long interval = 2000;           

#define OFFTIME1 4000
#define ONTIME1 300
unsigned long interval1 = 4000;        

#define OFFTIME2 7000
#define ONTIME2 300
unsigned long interval2 = 7000;           

const int ledPin =  30;     
int ledState = LOW;     
long previousMillis = 0;  

const int ledPin1 =  31;     
int ledState1 = LOW;     
long previousMillis1 = 0; 

const int ledPin2 =  32;     
int ledState2 = LOW;     
long previousMillis2 = 0; 
 
void setup() 
{
   pinMode(ledPin, OUTPUT);    
   pinMode(ledPin1, OUTPUT);    
   pinMode(ledPin2, OUTPUT);  
 }
 
void loop()
{
  unsigned long currentMillis = millis();
  
  if (currentMillis - previousMillis > interval) 
  {
    previousMillis = currentMillis;   

    if (ledState == LOW)
    {
      ledState = HIGH;
      interval = ONTIME;
    }
    else
    {
      ledState = LOW;
      interval = OFFTIME;
    } 
    digitalWrite(ledPin, ledState);
  }
  
  unsigned long currentMillis1 = millis();
  
  if (currentMillis1 - previousMillis1 > interval1) 
  {
    previousMillis1 = currentMillis1;   

    if (ledState1 == LOW)
    {
      ledState1 = HIGH;
      interval1 = ONTIME1;
    }
    else
    {
      ledState1 = LOW;
      interval1 = OFFTIME1;
    } 
    digitalWrite(ledPin1, ledState1);
  }
  
   unsigned long currentMillis2 = millis();
  
  if (currentMillis2 - previousMillis2 > interval2) 
  {
    previousMillis2 = currentMillis2;   

    if (ledState2 == LOW)
    {
      ledState2 = HIGH;
      interval2 = ONTIME2;
    }
    else
    {
      ledState2 = LOW;
      interval2 = OFFTIME2;
    } 
    digitalWrite(ledPin2, ledState2);
  }
}

Happy ending! And here is the sequel :slight_smile:

You are doing the same thing 3 times. Each time with variable1, variable2 variable3 ... Using arrays avoids repeating the same code.
Variable[1] Variable[2] Variable[3] replaces the indivdual ones. That does not save anything.
But then we use Variable[idx] and let idx take on the values 1, 2, and 3.
This means your code becomes (only showing parts)

int ledPin[] = { 30, 31, 32 } ;
int ledState[] = { LOW, LOW, LOW } ;
unsigned long ofTime = { 2000, 4000 , 4000 } ;
// and so on for all 

void setup() {
  for ( int idx = 0 ; idx < 3 ; idx++ )  // for all defined pins...
      pinMode(ledPin[idx],OUTPUT) ;  //  ..set them to output
}

void loop() {
  currentMillis = millis() ;
  for ( idx = 0 ; idx < 3 ; idx++ ) { // for all defined LEDs, do :
    if (currentMillis - previousMillis[idx] > interval[idx]) 
    {
      previousMillis[idx] = currentMillis;   

    if (ledState[idx] == LOW)
    {
      ledState[idx] = HIGH;
      interval[idx] = onTime[idx];
   :
  } // close brace that matches the for
} // end loop

So you only need to write your code once. There are a few more tricks with arrays but this will get you the essentials.

Very good example Msquare!

:astonished: could not get that code working ]:slight_smile: dammm it

"not working" could be anything from smoke out of the LED to one LED blinking at the wrong rate. Presumably you are somewhere between.

The code I wrote was showing the principle by "transforming" some of your code, but it got to just a tedious editing exercise after the first few lines, so look at your code, the bit I have made and make similar edits to your code. You need to declare more arrays, too (same lazy reasoning from me). You can not use #define as arrays, so they become ordinary array/variables

not working probably because

unsigned long ofTime = { 2000, 4000 , 4000 } ;
interval[idx] = onTime[idx];

ofTime or onTime ?? Change ofTime to onTime

https://www.w3.org/community/d-tasks/simple-page/