Week 9: Rain Mayhem

For this week’s task, I used several LEDs, a photoresistor, and a button to simulate a rare event in Abu Dhabi: rain.

LEDs:

I used four colors of LEDs:

  1. blue to represent rain
  2. red and yellow to represent the social chaos caused by the rain;
  3. green to represent the calm state of no rain

The blue and green LEDs are connected individually in parallel, while the other six red and yellow LEDs are connected in pairs of series, which are then connected in parallel.

Photoresistor: 

For this task, I thought that using the values read directly from the photoresistor would not make sense because I’m only considering two states: rain and no rain. For this reason, I decided to set threshold variables. If the photoresistor value exceeds the variable, then it is considered not raining, and if the photoresistor value is lower than the threshold then it is considered raining. This can also be abstracted as the presence or lack of rain clouds.

Button:

The button in this task represents some control over the chaos (yellow and red LEDs) present while it’s raining. If it is raining and the button is pressed, then the chaos will stop. This can also be abstracted as some kind of enforcement of social distancing regulations which might be broken in the rain mayhem.

Limitations(?):

I had to change my threshold variable multiple times while working on the project because the lighting in my surroundings changed. I think for this project to work correctly, the threshold value has to be updated every time it is run.

Below are the visuals and code of the task:

View post on imgur.com

const int knob = A0;
const int calmLed = 3;
bool raining = 0;
int threshold = 85
0;
int activeBlue = 0;
int buttonPin = 13;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  for (int i = 3; i <= 9; i++) {
    pinMode(i, OUTPUT);
  }
  pinMode(buttonPin, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  int buttonState = digitalRead(buttonPin);
  int knobVal = analogRead(knob);
  int mappedVal = map(knobVal, 420, 820, 0, 255);
  Serial.print("Knob Val: ");
  Serial.println(knobVal);
  Serial.print("Mapped Val: ");
  Serial.println(mappedVal);

  if (knobVal > threshold) {
    raining = 0;
  } else {
    raining = 1;
  }

  //control green light when not raining
  if (!raining) {
    //turn off all red, yello, and blue leds when not raining
    for (int i = 4; i <= 9; i++) {
      digitalWrite(i, LOW);
    }
    digitalWrite(3, HIGH);
    delay(500);
    digitalWrite(3, LOW);
    delay(500);
  } else {

    //control red and yellow leds when raining
    if (buttonState == LOW) {
      int x = 0;
      for (int i = 4; i <= 6; i++) {
        x = (int) random(0, 10);
        if (x > 7) {
          digitalWrite(i, LOW);
        } else {
          digitalWrite(i, HIGH);
        }
      }
    } else {
      for (int i = 4; i <= 6; i++) {
        digitalWrite(i, LOW);
      }
    }

    //control blue leds when raining
    int iterateBlue = (activeBlue % 3) + 7;
    digitalWrite(iterateBlue, HIGH);
    delay(200);
    digitalWrite(iterateBlue, LOW);
    activeBlue += 1;
  }
}

 

Leave a Reply