Hello,
I wrote a simple sketch to test out multitasking using LED's.
It is based on a simple blink sketch plus a button input to light a second LED when pressed.
Tech info:
ATtiny 13A
Programmed via ESP8266 using Arduino IDE
Power supply: 3.3VDC regulated supply
The way I intended it to work:
The Red LED would always blink on and off within 1 second (500 ms half cycle).
The Yellow LED would remain off while the button is not depressed.
The Yellow LED will light when the button is held down only.
The observed outcome:
The Red LED blinks as expected. ()
The Yellow LED remains off while the button is not pressed. ()
The Yellow LED does turn on when the button is held down. ()
All good so far .....
However, an extra unwanted behaviour is observed....
The Yellow LED 'flickers' in that it drops intensity when the Red LED is on,
and then gains back intensity when the Red LED turns off.
I am wondering why the Yellow LED does not come one with a single intensity when the
button is pressed, and what is making it drop intensity when the Red LED turns on.
Any insight would be greatly appreciated.
#include <avr/io.h> // seems to be needed for IO /* Blink ------------------------------------------------------------------------------ PINOUT PB5 u VCC PB3 PB2 PB4 PB1 GND PB0 */ const uint8_t RED_PIN = 4; // PB4 = package pin 3 (edited to fix comment) const uint8_t YELLOW_PIN = 1; // PB1 = package pin 6 = yellow (edited to fix comment) const uint8_t BUTTON_PIN = 0 ; // button pin is pin 5 PB0 uint8_t flg = HIGH; //button status uint32_t tme = 0; //unsigned time integer uint8_t tog = 0; // toggle switch uint32_t tog_flag = millis(); // initial time void setup() { DDRB |= (1 << RED_PIN)| (1 << YELLOW_PIN); // make led pins outputs } /// setup end void toggle(){ // toggle the flag 'tog' between 1 & 0 every 500 ms if (millis() - tog_flag > 500){ if (tog == 0){ tog = 1; // toggle the tog } else { tog = 0; // toggle } tog_flag = millis(); // reset the flag timer } } // end toggle void yellow(){ // turn on the yellow LED if button push detected flg = digitalRead(BUTTON_PIN); if (flg == LOW) { PINB |= (1 << YELLOW_PIN); } } void red() // red blink loop - simply blink on/off in 1s cycle { if (tog == 1) { PINB |= (1 << RED_PIN); } if (tog == 0) { PINB |= (0 << RED_PIN); } } void loop() { yellow(); // poll the button and light yellow if button held on toggle(); // call the toggle timer red(); // blink red on/off forever }
============== EDIT 27-APRIL-20 ===========
Updated schematic following mod's of post #3
Note that I have kept LED2 marked as yellow just to differentiate it - even though it has been changed to a red LED.