Arduino IDE, Blink Without Delay, Two Intervals, One LED, 240615

Thread Starter

allenpitts

Joined Feb 26, 2011
168
Hello AAC forum,

Have done the Blink Without Delay in the Examples. Have
also done a sketch that blinks several LEDs by having each LED
have its own LEDx BWOD function and calling the different LEDx
functions from the loop() function. (Can post this sketch if
you are interested but basically it creates a BWOD function for
each LED and then calls each LED BWOD function from the loop() function.

Several forum posts on controlling several LEDs with different
intervals have also been studied, copied and implemented
successfully.

In these examples the BWOD toggles between high and low using the constuct
C++:
    if the LED is off turn it on and vice-versa:
   if (ledStateOn == LOW) {
     ledStateOn = HIGH;
   } else {
    ledStateOn = LOW;
    }
What is sought is to have the same LED blink longer
off than on. So it was conjectured that the toggle constuct
copied herewith above would be omitted and there should
be two BWOD functions, on for ON and another
for OFF.

Copied my attempt at this herewith below. Having trouble
getting it to compile.
Compilation error: expected unqualified-id before 'if'

Thanks.

Allen Pitts
Dallas TX

C++:
/* Blink without Delay
based on documentation found at
https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay/
and
Blink_1_wo_Delay_Attiny85_240402,ino
Blink_1_wo_Delay_Attiny85_two_intervals_2400615.ino (this sketch)
uses two intervals so the On interval can be different from the Off interval.
*/

// constants won't change. Used here to set a pin number :
const int ledPin3 = 3;  // the number of the LED pin

// Variables will change :
int ledStateOn = LOW;
int ledStateOff = LOW;


unsigned long previousMillisOn = 0;
unsigned long previousMillisOff = 0;

// constants won't change :
const long intervalOn = 1000;
const long intervalOff = 500;

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

void loop() {
  runLED3();
}

void runLED3() {
  unsigned long currentMillisOn = millis();

  if (currentMillisOn - previousMillisOn >= intervalOn && ledStateOn = LOW)
  {
    // save the last time you blinked the LED
    previousMillisOn = currentMillisOn;

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

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin3, ledStateOn);

  }

  unsigned long currentMillisOff = millis();

  if (currentMillisOff - previousMillisff >= intervalOff && ledStateOff = HIGH)
  {
    // save the last time you blinked the LED
    previousMillisOff = currentMillisOff;

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

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin3, ledStateOff);
  ledStateOn = LOW;
  }

}
 

Attachments

k1ng 1337

Joined Sep 11, 2020
1,038
Try this code. It changes the delay time based on the LED's current state.

C-like:
// Define constants for pin and timing
const int ledPin = 13;     // The pin number of the LED
unsigned long previousMillis = 0;  // Stores the last time LED was updated
int onTime = 300;   // Time LED is ON (in milliseconds)
int offTime = 700;  // Time LED is OFF (in milliseconds)
bool ledState = LOW;  // Tracks the current state of the LED

void setup() {
  pinMode(ledPin, OUTPUT); // Initialize the LED pin as an output
}

void loop() {
  unsigned long currentMillis = millis(); // Get the current time

  // Check if it's time to change the state of the LED
  if (currentMillis - previousMillis >= (ledState ? onTime : offTime)) {
    // Save the last time the LED state changed
    previousMillis = currentMillis;

    // Change the LED state
    ledState = !ledState;
    digitalWrite(ledPin, ledState);
  }
}
 
Top