Week 9 Sensors – Christmas Tree

Concept:

For this assignment, we had to get information from an analog and a digital sensor and control 2 LEDs, one with the analog and one with the digital. Since it is almost Christmas season, I wanted to create a Christmas tree lit by a light sensor and a switch separately. I really like this time of the year because it is special in my hometown. It is all about family and friends gatherings, rainy days, meaningful conversations with delicious food, and an end-of-year celebration.

Highlight:

Reviewing the lecture notes and my notes was key in constructing this assignment. I started by drawing the tree, coloring it, and marking where I wanted the LEDs to go. I had to glue things in place because the paper was not strong enough to handle all the LEDs and wires.

I was a little scared of assembling the circuit in the wrong way and creating a shot, but it worked out well. However, I had to figure out how to deal with many things to make this project work. For instance,  I  had to figure out how to make the LED anode longer. To fix this problem, I  decided to use wires and attach them to the anode and the board instead of attaching the LEDs directly to the board. It was frustrating to attach the wires to the LEDs especially to keep them in place. To distinguish between the positive and negative anodes of the LED I used foil for one and copper tape for the other. Doing so made it less confusing to attach them to the board. I had some doubts about whether the circuit was correct or not so I re-assembled them multiple times to make sure I made it correctly. Attaching the switch and the light sensor was easier. Even though I made two codes one for the switch and the other for the light sensors, on the board they both share the same GND and the 5V.

The code is like to what we did in class. I tried to figure out how to add all Pins in one line of code but I could not figure out how,

Light Sensor:

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(2, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A4);
  if (sensorValue > 255) {
    digitalWrite(13, LOW);
    digitalWrite(11, LOW);
    digitalWrite(9, LOW);
    digitalWrite(7, LOW);
    digitalWrite(5, LOW);
    digitalWrite(3, LOW);
    digitalWrite(2, LOW);
    delay(1000);  // turn the LED off by making the voltage LOW
  }
  // wait for 30 milliseconds to see the dimming effect
  else {
    digitalWrite(13, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(2, HIGH);
  }
}

Switch :

// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = A2;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);
  pinMode(13, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(2, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int buttonState = digitalRead(pushButton);
  // print out the state of the button:
 if (buttonState==1) {
  digitalWrite(13, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(2, HIGH);
  
    // turn the LED off by making the voltage LOW
  }
  // wait for 30 milliseconds to see the dimming effect
  else {
      digitalWrite(13, LOW);
    digitalWrite(11, LOW);
    digitalWrite(9, LOW);
    digitalWrite(7, LOW);
    digitalWrite(5, LOW);
    digitalWrite(3, LOW);
    digitalWrite(2, LOW);
    
    
  }
}


 

 

Reflection and future improvements:

Trusting the process was a huge reason why this project worked. It is not complicated, but it needs a lot of focus. In the future, I want to make it more presentable and neater. I want to also find a way where both the sensors work together in the same code.

Merry Christmas!

Leave a Reply