Zoom Modem Ornament
In my recent video where I turned an old Zoom modem into a Christmas tree ornament, I showed the code I used to make the LEDs blink and play the modem sound. This is a pretty simple program with some standard elements that I use all the time in my projects. I especially use the timer and the button state routines more often than not.
Here is the code if you want to copy/download and use for your projects.
int timer = 500; //How long each set of leds should be on in milliseconds
int green = 8; //Digital pin for greed LEDs
int red = 7; //Digital pin for red LEDs
int yellow = 6; //Digital pin for yellow LEDs
int button = 5; //Digital pin that is connected to the button
int sound = 4; //Digital pin for the sound module trigger
unsigned long previous_time = millis(); //Use this to track when the time
bool green_on = true;
int button_state = 1; //This helps trach the state of the switch
int last_state = 0;
// the setup function runs once when you press reset or power the board
void setup() {
pinMode(green, OUTPUT);
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(button, INPUT_PULLUP);
pinMode(sound, OUTPUT);
digitalWrite(yellow, LOW);
}
// the loop function runs over and over again forever
void loop() {
if (millis() - previous_time > timer) {
if (green_on) {
digitalWrite(green, HIGH);
digitalWrite(red, LOW);
green_on = false;
previous_time = millis();
} else {
digitalWrite(green, LOW);
digitalWrite(red, HIGH);
green_on = true;
previous_time = millis();
}
}
button_state = digitalRead(button);
if (button_state != last_state) {
digitalWrite(sound, LOW);
delay(250);
digitalWrite(sound, HIGH);
last_state = button_state;
}
}