Pages: [1]   Go Down
Author Topic: Code modification  (Read 598 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

   I need LED to be on for 2sec and off for 5sec without using delay function.
How to modify the following code to do this?

const int ledPin1 =  13;          // pin number for relay 1

int ledState1 = LOW;             
long previousMillis1 = 0;       
long interval1 = 2000;          // interval at which to blink (milliseconds) for LED1
//long interval2 = 5000;     // interval at which LED1 is off (milliseconds)

void LED1()  //FUNCTION TO TURN ON AND OFF LED1.
{
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis1 > interval1)
  {
    previousMillis1 = currentMillis;   
    if (ledState1 == LOW)
      ledState1 = HIGH;
    else
      ledState1 = LOW;
    digitalWrite(ledPin1, ledState1);
  }
}

void setup()
{
  pinMode(ledPin1, OUTPUT);    // set the digital pin as output:
}

void loop()
{
  LED1();
 }
Logged

USA
Online Online
Sr. Member
****
Karma: 17
Posts: 390
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Think:  You're testing a variable, "interval1," to see if it's time to change the state of the LED.  You want the ontime and the offtime to be different.  You never change the value of "interval1."

What do you need to do?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8967
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
 if(currentMillis - previousMillis1 > (ledState1 ? interval1 : interval2))

That's a shortcut version of:
Code:
 unsigned long interval;
  if (ledState1)
        interval = interval1;  //  LED stays ON for this time interval
  else
        interval = interval2;  //  LED stays OFF for this time interval
  if(currentMillis - previousMillis1 > interval)
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

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

   Thank you!
   I am very new to coding.  So can you please show the whole code but not only idea.
In my code blinking is with equal interval, but I need LED to be off for longer time.
I am just learning.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8967
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:

const int ledPin1 =  13;          // pin number for relay 1

int ledState1 = LOW;             
unsigned long previousMillis1 = 0;       
const unsigned long interval1 = 2000;          // interval at which to blink (milliseconds) for LED1
const unsigned long interval2 = 5000;     // interval at which LED1 is off (milliseconds)

void LED1()  //FUNCTION TO TURN ON AND OFF LED1.
{
  unsigned long currentMillis = millis();

 if(currentMillis - previousMillis1 > (ledState1 ? interval1 : interval2))
  {
    previousMillis1 = currentMillis;   
    ledState1 = !ledState1;
    digitalWrite(ledPin1, ledState1);
  }
}

void setup()
{
  pinMode(ledPin1, OUTPUT);    // set the digital pin as output:
}

void loop()
{
  LED1();
 }
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 503
Posts: 19095
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

http://www.gammon.com.au/blink
Logged


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

Thank you!
Logged

Pages: [1]   Go Up
Jump to: