The Police

In this weeks assignment I was inspired by current protests in Poland. The police lights are flashing all the time in Warsaw nowadays and I wanted to have some control over that. At least on my mini Arduino board 🙂

I decided to have two modes, which I could switch around by pressing the button. One is the rapid mode, where the two LEDs blink rapidly in the police fashion. The other is the controlled mode, where the user can switch between the two LEDs by the means of the potentiometer. By doing that, they can control the LEDs and almost play with them just as the DJs do with their sounds decks .

And here is my code:

int buttonPin = 2;
int blueLedPin = 7;
int redLedPin = 8;
int prevBtnState = 0;
bool mode1 = true;
long timer = 0;
long debounce = 200;

void setup() {
  pinMode(blueLedPin, OUTPUT);
  pinMode(redLedPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
}

void loop() { 
  int knobValue = map(analogRead(A0), 190, 860,  10, 500);
  int btnState = digitalRead(buttonPin);
  
  if (!btnState && prevBtnState  && millis() - timer > debounce){
    mode1 = !mode1;
    timer = millis();
  }  
  
  if (mode1) {
    Serial.println("MODE 1");
      digitalWrite(redLedPin, HIGH);
      delay(75);
      digitalWrite(blueLedPin, HIGH);
      delay(75);
      digitalWrite(redLedPin, LOW);
      delay(75);
      digitalWrite(blueLedPin, LOW);
      delay(75);
  }
  else {
      if (knobValue < 250) {
        digitalWrite(blueLedPin, HIGH);
        digitalWrite(redLedPin, LOW);
      }
      else {
        digitalWrite(blueLedPin, LOW);
        digitalWrite(redLedPin, HIGH);
      }
  }  
  prevBtnState = btnState;
}

Leave a Reply