A couple months ago, I was messing with one of my kid’s toys: it was a small, cheap kaleidoscope shaped like a camera, made of wood and plastic. Pressing the shutter button made it chirp and flash as if a photo were being taken.
After a while it stopped working, so I assumed I needed to replace the battery. But when I tried prying out the button cells, I cracked the wood and broke the housing. Instead of trying to repair the wood, I decided to take apart the whole contraption and salvage the LED, sound chip, and some electronic parts to use with my breadboard.
I hadn’t picked up the breadboard for at least a year, and I thought I could use my ‘new’ LED in a project to absolve my guilt at having broken a perfectly fine toy.
This mini project was focused on getting a row of LEDs to blink. Once that was working, I added a button switch that let me change the speed of the blinks.
A basic overview of my setup:
- I’m coding using the PlatformIO extension in Visual Studio Code.
- The breadboard’s chip is the WeMos D1 Mini.
- The sketch uses a few LEDs, resistors for protection, and a switch.
- Pressing the switch changes the speed of the blinking LEDs.
Code goes here for now.
#include <ESP8266WiFi.h>
#define PIN_LED_GRN D1
#define PIN_LED_RED D2
#define PIN_LED_CLR D3
#define PIN_LED_BLU D4
#define PIN_LED_YEL D5
#define PIN_BUTTON D6
#define LED_OFF 0
#define LED_ON 255
#define SPEED_MIN 50
#define SPEED_MAX 1000
long pastTime = 0;
long speed = SPEED_MAX;
boolean buttonPressed = false;
enum LedState {
BLU,
GRN,
RED,
YEL,
CLR
};
LedState currentLedState = BLU;
// function declarations:
void handleButtonPress();
void setup(void) {
Serial.begin(115200);
Serial.println();
Serial.println("Booting Sketch...");
attachInterrupt(digitalPinToInterrupt(PIN_BUTTON), handleButtonPress, FALLING);
pinMode(PIN_LED_BLU, OUTPUT);
pinMode(PIN_LED_GRN, OUTPUT);
pinMode(PIN_LED_RED, OUTPUT);
pinMode(PIN_LED_YEL, OUTPUT);
pinMode(PIN_LED_CLR, OUTPUT);
pinMode(PIN_BUTTON, INPUT_PULLUP);
digitalWrite(PIN_LED_BLU, LED_OFF);
digitalWrite(PIN_LED_GRN, LED_OFF);
digitalWrite(PIN_LED_RED, LED_ON);
digitalWrite(PIN_LED_YEL, LED_OFF);
digitalWrite(PIN_LED_CLR, LED_OFF);
}
void loop(void) {
// main code here, to run repeatedly:
if(buttonPressed) {
handleButtonPress();
}
if(millis() - pastTime >= speed) {
switch(currentLedState) {
case BLU:
digitalWrite(PIN_LED_BLU, LED_ON); // inverted for D4
digitalWrite(PIN_LED_GRN, LED_ON);
currentLedState = GRN;
break;
case GRN:
digitalWrite(PIN_LED_GRN, LED_OFF);
digitalWrite(PIN_LED_RED, LED_ON);
currentLedState = RED;
break;
case RED:
digitalWrite(PIN_LED_RED, LED_OFF);
digitalWrite(PIN_LED_YEL, LED_ON);
currentLedState = YEL;
break;
case YEL:
digitalWrite(PIN_LED_YEL, LED_OFF);
digitalWrite(PIN_LED_CLR, LED_ON);
currentLedState = CLR;
break;
case CLR:
digitalWrite(PIN_LED_CLR, LED_OFF);
digitalWrite(PIN_LED_BLU, LED_OFF); // inverted for D4
currentLedState = BLU;
break;
}
pastTime = millis();
}
}
// function definitions:
void IRAM_ATTR handleButtonPress() {
speed = speed / 2;
if(speed < SPEED_MIN) {
speed = SPEED_MAX;
}
}
Leave a Reply