Left Right Init with Timers

so basically is what i have is two outputs connected to LED, with a Toggle switch Connected to Pin 7 ("Enable") i need the Outputs to overlap when they Are Flipped back and forth, i have pseudo Code

is pin 7 Enabled
FlipStates
on Enablepin HIGH
linit on 1 mim
if timer = 57 then
rinit on 1 min "60-Seconds"
Timer = 60 Turn linit off
if timer = 57 then
linit on 1 min "60-Seconds"
Timer = 60 Turn rinit off
Loop::

 Basically Flipping the Two on and off with a 3 Second Overlap 

i will be starting a wokwi Design i will post as well , hopefully Someone can help me out

Diagram ::
Screenshot at 2024-04-18 10-35-50

when the pin changes state, one LED is turned on immediately and a timer started to turned the other LED off 3 secs later. Capture the time when the pin changes state and turn the LED off when the current value of millis() is 3 sec greater than the time when the pin changed state

the Led's are Saving state. can i make them turn off Completely , and it seems the Slide switch isn't turning them off Consistently , any help would be Great

const int switchPin = 8;
const int ledPin1 = 7;
const int ledPin2 = 2;

int switchState = 0;
int ledState1 = LOW;
int ledState2 = HIGH;

void setup() {
  pinMode(switchPin, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
}

void loop() {
  switchState = digitalRead(switchPin);

  if (switchState == HIGH) {
    ledState1 = !ledState1;
    ledState2 = !ledState2;
    digitalWrite(ledPin1, ledState1);
    delay(250);
     digitalWrite(ledPin2, ledState2);
    delay(250); // Adjust delay time as needed
  if (switchState == LOW) {
    ledState1 = !ledState1;
    ledState2 = !ledState2;
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
  }
  }
}

WOKWI-PROJECT

do you need to recognize the change in state of the switch?

this is code for a momentary button press

// simple button processing

const byte butPin = A1;
byte       ledPin = 13;

byte butState;

enum { Off = HIGH, On = LOW };

// -----------------------------------------------------------------------------
void loop (void)
{
    byte but = digitalRead (butPin);

    if (butState != but)  {     // check that button state changed
        butState = but;
        delay (10);         // debounce

        if (LOW == but)     // button pressed
            digitalWrite (ledPin, ! digitalRead (ledPin));
    }
}

// -----------------------------------------------------------------------------
void setup (void)
{
    digitalWrite (ledPin, Off);
    pinMode      (ledPin, OUTPUT);

    pinMode     (butPin, INPUT_PULLUP);
    butState  = digitalRead (butPin);
}

Yes , the Switch is the Enable to start the Outputs Flipping back and Forth

how do you recognize when the switch has been moved (i.e. toggle between left and right)?

well it's supposed to work like this " when switch is to the right it's enabled" and the LED's turn on and off Flip Flop back and Forth every minute and to the left it stops "disabled" since they dont have a general toggle switch i used the slider

so the switch is just on on/off switch?

confused because you code seems to control the LED depending on switchState, instead of simply turning the LEDs off in the one switch position

look this over

const int switchPin = 8;
const int ledPin1 = 7;
const int ledPin2 = 2;

enum { Off = HIGH, On = LOW };

const unsigned long T1 = 1000;
const unsigned long T2 = 5000 - T1;

unsigned long period;
unsigned long msec0;

int           state;

// -----------------------------------------------------------------------------
void loop ()
{
    unsigned long msec = millis ();

    byte sw = digitalRead (switchPin);

    if (LOW == sw) {
        digitalWrite (ledPin1, Off);
        digitalWrite (ledPin2, Off);
        state  = 0;
        msec0  = msec;
        period = 0;
        return;
    }

    if (msec - msec0 >= period)  {
        msec0 = msec;
        Serial.println (msec);

        switch (state)  {
        case 0:
            digitalWrite (ledPin2, Off);
            digitalWrite (ledPin1, On);     // handles start
            period = T2;
            break;

        case 1:
            digitalWrite (ledPin2, On);     // both on
            period = T1;
            break;

        case 2:
            digitalWrite (ledPin1, Off);
            period = T2;
            break;

        case 3:
            digitalWrite (ledPin1, On);     // both on
            period = T1;
            break;
        }

        if (4 <= ++state)
            state = 0;
    }
}

void setup () {
    Serial.begin (9600);
    pinMode (switchPin, INPUT_PULLUP);
    pinMode (ledPin1,   OUTPUT);
    pinMode (ledPin2,   OUTPUT);
}

Testing on WOKWI it never turns off the LEDS, when i go to the left. WOKWI Arduino

did you notice the values for Off and On?
try reversing them

on my board, the multifunction shield, LOW turns the LED on.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.