Witam wszystkich użytkowników,
mam problem ze sterowaniem on/off funkcji stroboskopowej.
niestety po przestawieniu wyłącznika w pozycję off jedna z diod zostaje w pozycji świecącej (jest to losowe zdarzenie, zależne w którym momencie funkcja została przerwana).
Pytanie:
- jak prawidłowo przerwać funkcję aby obie diody gasły?
mam problem ze sterowaniem on/off funkcji stroboskopowej.
Kod:
#define RED 0x0
#define BLUE 0x1
// Variable to track which LED is on
byte whichLED = RED;
// Where are the LEDs connected?
const int LED_Red = 7;
const int LED_Blue = 11;
const int switch = 4;
// State variables for the LEDs
byte Red_State = LOW;
byte Blue_State = LOW;
byte switch_state = LOW;
// Some delay values to change flashing behavior
unsigned long switchDelay = 250;
unsigned long strobeDelay = 50;
// Seed the initial wait for the strobe effect
unsigned long strobeWait = strobeDelay;
// Variable to see when we should swtich LEDs
unsigned long waitUntilSwitch = switchDelay; // seed initial wait
void setup() {
pinMode(LED_Red, OUTPUT);
pinMode(LED_Blue, OUTPUT);
pinMode(switch, INPUT);
}
void loop()
{ switch_state = digitalRead(switch);
if (switch_state == HIGH)
{
digitalWrite(LED_Red, Red_State); // each iteration of loop() will set the IO pins,
digitalWrite(LED_Blue, Blue_State); // even if they don't change, that's okay
// Toggle back and forth between the two LEDs
if ((long)(millis() - waitUntilSwitch)>=0) {
// time is up!
Red_State = LOW;
Blue_State = LOW;
whichLED = !whichLED; // toggle LED to strobe
waitUntilSwitch += switchDelay;
}
// Create the stobing effect
if ((long)(millis() - strobeWait)>=0) {
if (whichLED == RED)
Red_State = !Red_State;
if (whichLED == BLUE)
Blue_State = !Blue_State;
strobeWait += strobeDelay;
}
else
Red_State = LOW;
Blue_State = LOW;
}
}
Pytanie:
- jak prawidłowo przerwać funkcję aby obie diody gasły?