Week 9 – Annoyance Avoidance

Concept:

While brainstorming with my roommate about what to make for this week’s assignment, my suitemate arrived and expressed his frustration about our friends constantly knocking on our door when we’re not there. He complained about the disturbance, particularly during exams and at night. While apologizing to him, an idea popped into my head – a solution that would inform our friends about our availability. The concept involves two LEDs outside our room representing my roommate and me, which will turn on when we’re in the room and off when we’re not. If neither of us are in the room, we can press the two buttons together before leaving, causing the lights to flicker, indicating that we are not available. This solution could potentially address the problem for our friends and our suitemate.

Implementation:

To help navigate the board, I first drew the circuit diagram so that I would know what component would go where. To make my life easier, I used the red wire for 5V, black wire for GND, green wire for the green button, yellow wire for yellow button and the two white wires for the two LEDs. The circuit diagram looks like this:

I did run into a few problems with the code where pushing the button for a relatively longer period would reverse the effect that I wanted. I then added delay in certain places to resolve this issue. The code looks like this:

//globally declaring the three variables. Setting them to 1 so that all the lights are off
int yellowCount = 1;
int greenCount = 1;
int doubleCount = 1;

void setup() {
  //setting pinmodes. 8 and 13 as output since they are connected to the LEDs
  pinMode(8, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(A2, INPUT); //A1 and A2 as input for the switches
  pinMode(A1, INPUT);
}

void loop() {
  int yellowSwitch = digitalRead(A2); //read the yellow switch
  delay(100); 
  int greenSwitch = digitalRead(A1);
  delay(100);

  if (greenSwitch == HIGH && yellowSwitch == HIGH) //when both the switches are pressed together
  {
    doubleCount = doubleCount + 1; //increment this counter

  }
  else
  {
    if (yellowSwitch == HIGH) //for individual switches, increment their respective counters
    {
      yellowCount = yellowCount + 1;
    }

    if (greenSwitch == HIGH)
    {
      greenCount = greenCount + 1;
    }
  }

  if (doubleCount % 2 == 0) //if the counter is even
  {
    digitalWrite(8, HIGH);  //high for one
    digitalWrite(13, LOW); //low for the other
    delay(150); //wait
    digitalWrite(8, LOW); //then invert the voltages
    digitalWrite(13, HIGH); //to give off a flickering effect
    delay(150);

    yellowSwitch = digitalRead(A2); //read the two again
    greenSwitch = digitalRead(A1);

    if (yellowSwitch == HIGH) //if one of them is pressed
    {
      doubleCount = doubleCount + 1; //then this values goes to odd and then the flickering stops
    }

    if (greenSwitch == HIGH)
    {
      doubleCount = doubleCount + 1;
    }

  }
  else
  {
    if (yellowCount % 2 == 0) //if the yellow is pressed again and the count is even
    {
      digitalWrite(8, HIGH); //turn on the led
    }
    else
    {
      digitalWrite(8, LOW); //otherwise turn it off
    }

    if (greenCount % 2 == 0) //same for the green button
    {
      digitalWrite(13, HIGH);
    }
    else
    {
      digitalWrite(13, LOW);
    }
  }
}

The final implementation looks like this:

Further Improvements:

Instead of LEDs, an LCD or a display could be used that would display messages as “BOTH IN ROOM”, “MOEEZ IN ROOM”, or “NONE IN ROOM” etc. Moreover, the code could use some refinement as well with the delay function. Adding a security layer such as a password using a keypad so that only me and my roommate can update it can also help increase its accuracy.

Leave a Reply