Skip to content

Instantly share code, notes, and snippets.

@ItsMichal
Last active September 24, 2021 07:51
Show Gist options
  • Save ItsMichal/83bbf5e1ecba49d85802d44381818a4b to your computer and use it in GitHub Desktop.
Save ItsMichal/83bbf5e1ecba49d85802d44381818a4b to your computer and use it in GitHub Desktop.
Police LEDs Arduino Nano 33 IoT

Police LED Demo

Hi- this is a quick script to control 2 LEDs with 2 Momentary Switches. It's a very simple script that switches between 4 unique states.

Schematic

Schematic

//Michal Bodzianowski (2021)
//A simple demo that controls 2 LEDs with 2 Momentary Switches
//Name refers to the siren state and color of LEDs.
//GPIO pins of everything used.
//corresponds to Arduino Nano 33 IoT
int redSwitch = 12;
int blueLED = 10;
int whiteSwitch = 8;
int whiteLED = 6;
//Tracks the HIGH/LOW state of switches.
int redState = 0;
int whiteState =0;
void setup()
{
//Set up switches and LEDs as i/o accordingly
pinMode(redSwitch, INPUT);
pinMode(whiteSwitch, INPUT);
pinMode(blueLED, OUTPUT);
pinMode(whiteLED, OUTPUT);
}
void loop()
{
//Read in the state of switches
redState = digitalRead(redSwitch);
whiteState = digitalRead(whiteSwitch);
//Siren state
//Swap between white LED and blue LED
//every 100ms if both switches pressed
if(redState == HIGH && whiteState == HIGH)
{
digitalWrite(whiteLED, HIGH);
digitalWrite(blueLED, LOW);
delay(100);
digitalWrite(whiteLED, LOW);
digitalWrite(blueLED, HIGH);
delay(100);
}
//Both state - turn both LEDs on if white switch pressed
else if(whiteState == HIGH)
{
digitalWrite(whiteLED, HIGH);
digitalWrite(blueLED, HIGH);
}
//White LED state - turn white LED on if red switch pressed
else if(redState == HIGH)
{
digitalWrite(whiteLED, HIGH);
}
//No state- keep both off if nothing pressed.
else
{
digitalWrite(whiteLED, LOW);
digitalWrite(blueLED, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment