Luke Nguyen – Week 10 Assignment – Thanos Snap


Concept & Inspiration:

I thought of Thanos in Marvel Comics while I listened to a song by Beyoncé where he is referenced. Analog versus sensor seems to be a duality that reminds me of the notion of half and half that Thanos is fervent believer of. He believes that if he wipes out half of the population of the universe, he could restore its balance (half-populated and half-empty); fully populated would tip the scale. This assignment is set in that universe. I want the population of the universe to be represented by the number of LEDs; I used 4 LEDs in this case. I would play 2 roles as I operate the circuit: the creator of life and Thanos. To bring “life” to the LEDs, I would use the photocell as the analog sensor. To destroy “life” inside the LEDs, I would use the button as the digital sensor.

Circuit:

Code:

When I touch the photocell (the analog sensor), I would bring “life” to the LEDs by making them turn on. When I press the button (the digital sensor), I would wipe out half of the universe (half of the number of lives) by turning off two pre-determined LEDs. This way, I only have to code in a way so that that is possible.

int led1 = 4;
int led2 = 7;
int led3 = 8;
int led4 = 12;
int brightness = 0;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  pinMode(led1, OUTPUT);
  pinMode(A3, INPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A2);
  int buttonState = digitalRead(A3);

  sensorValue = constrain(sensorValue, 500, 900);
  brightness = map(sensorValue, 500, 860, 255, 0);


  if (brightness > 0){
    analogWrite(led1, brightness);
    analogWrite(led2, brightness);
    analogWrite(led3, brightness);
    analogWrite(led4, brightness);

    if (buttonState == HIGH ){
      digitalWrite(led3, LOW);
      digitalWrite(led4, LOW);
    }

  }

  Serial.println(sensorValue);
  delay(30);  // delay in between reads for stability

}

Challenges and reflections:

My biggest challenge has to do with the coding. I couldn’t figure out how to write the code for both the third and fourth LED, both of which are controlled by the analog sensor and the digital sensor at the same time, in a way that would make them turn on when I touched the photocell and turn off when I pressed the button.

A first few attempts resulted in these 2 LEDs not turning on at all, them only turning on when I was not touching the photocell, them turning on when I touched the photocell but not turning off when I pressed the button.

The commented-out parts are these first few attempts:

void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A2);
  int buttonState = digitalRead(A3);

  sensorValue = constrain(sensorValue, 500, 900);
  brightness = map(sensorValue, 500, 860, 255, 0);

  // analogWrite(led1, brightness);
  // analogWrite(led2, brightness);
  // analogWrite(led3, brightness);
  // analogWrite(led4, brightness);



  // if (buttonState == LOW && sensorValue > 600 ){
  //   digitalWrite(led3, HIGH);
  //   digitalWrite(led4, HIGH);
  // }
  // else{
  //   digitalWrite(led3, LOW);
  //   digitalWrite(led4, LOW);
  // }


  if (brightness > 0){
    analogWrite(led1, brightness);
    analogWrite(led2, brightness);
    analogWrite(led3, brightness);
    analogWrite(led4, brightness);

    if (buttonState == HIGH ){
      digitalWrite(led3, LOW);
      digitalWrite(led4, LOW);
    }

  }
  // else{
  //   if (buttonState == LOW ){
  //     digitalWrite(led3, HIGH);
  //     digitalWrite(led4, HIGH);
  //   }
  //   else{
  //     digitalWrite(led3, LOW);
  //     digitalWrite(led4, LOW);
  //   }
  // }

  Serial.println(sensorValue);
  delay(30);  // delay in between reads for stability

}

 

Leave a Reply