For this assignment, I used a digital sensor (switch) that acts as an ON/OFF button for an LED and an analog sensor (photoresistor/light sensor) to control the brightness of another LED. I built 2 simple LED circuits, each connected to their sensors using code.
To choose the values for the thresholds of the analog sensor, I first printed the values of the analog output to see what values the photosensor ranges from and to (depending on the room light I was in). Then, I divided them into 4 ranges.
It’s November already! Unbelievable. This means that we can start thinking about and preparing for Christmas more explicitly—and that includes decorations! Looking at the red and green LEDs, I was inspired by the festive thought of Christmas lights. With the idea to implement analog and digital sensors as controllers for the flickering of the lights, I set out to work.
Process
After a few iterations, I was able to settle on a setup that worked:
The row of four alternating red and green LEDs are the Christmas lights; the red LEDs are connected to Pin 13, while the green ones are connected to Pin 12. The colors of the wires match the colors of the connected LEDs. Electricity from the 5V pin flows to both the digital sensor (the switch) and the analog sensor (the potentiometer), from which data goes respectively to Pin 2 and the A2 pin. All of the circuits, of course, flow back to the GND pin.
As for the code, I first initialized the needed variables and got things started in the setup() function:
//Initialize Variables
int currentButtonState;
int lastButtonState;
int LEDState = 0;
int sensorValue;
void setup() {
//Initialize Serial
Serial.begin(9600);
//Set Pins
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(2, INPUT);
//Set the Starting State of the Button
currentButtonState = digitalRead(2);
}
The currenButtonState and lastButtonState variables are crucial to a central feature of my circuit, which is how the button is used to switch between the LEDs’ three different modes (off, on, alternating blinks). For this part of the code, I referred to this tutorial: Arduino Button Toggle LED Tutorial.
The LED’s modes are linked to the LEDState variable; 0 is off, 1 is on, and 2 is alternating blinks. As shown in the code above, LEDState starts at 0, meaning the LEDs are initially off.
Following is the first part of the code in the loop() function:
void loop() {
//Saves the Last State
lastButtonState = currentButtonState;
//Reads the New, Current State
currentButtonState = digitalRead(2);
//Sensor Value; 100 is the minimum for the delay interval
sensorValue = analogRead(A2) + 100;
Serial.println(sensorValue);
//Delay for Read Stability
delay(1);
//Only Runs When Button is Pressed
if (lastButtonState == HIGH && currentButtonState == LOW) {
Serial.println("The button has been pressed.");
//Change LED State with Each Press
if (LEDState == 0){
LEDState = 1;
} else if (LEDState == 1){
LEDState = 2;
} else if (LEDState == 2){
LEDState = 0;
}
//Rest of Code Below
}
As demonstrated in the code, an if statement is used t0 check if the button has been pressed—and when the button is pressed, a chain of nested if else statements are used to change the value of the LEDState variable to the next one in line. Also notable is how the sensorValue is used to store the value collected from the potentiometer with an extra 100 added to it. The reason for this is revealed in the rest of the code:
void loop() {
//Rest of the Code Above
//0 = Off; 1 = On; 2 = Alternating Blinks, Delay interval controlled by potentiometer
if (LEDState == 0){
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
} else if (LEDState == 1){
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
} else if (LEDState == 2){
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
delay(sensorValue);
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
delay(sensorValue);
}
}
Based on the value of LEDState, a different mode is accessed. sensorValue is used as the parameter value in the delay() functions to determine the length of the interval of each blink; turning the potentiometer counterclockwise will make the LEDs blink quicker, while turning it clockwise will make them blink slower. 100 is added to the value from the potentiometer to determine sensorValue because I determined that 100 would be a good minimum value to set for the delay() function (a parameter of 0 would essentially be no blinking, while anything under 100 is much too rapid).
Here is a video of what the finished project looks like:
Reflections
While I initially struggled to grasp the workings of Arduino and circuits, I feel that I have begun to get a hang of how things work. Tinkering with the kit on my own has allowed me to review and figure things out, and being able to pull this off successfully has been a confidence boost! I feel much more comfortable with Arduino than I was prior to this assignment.
One major part of the project I could improve: using the delay() function has the critical drawback of rendering my program unable to read any button presses that happen while Arduino is paused (making it seem unresponsive). It turns out there is indeed a way to make the LEDs blink without using the delay() function, one that I only found out about after wrapping things up; it will be something to keep in mind as I continue to work with Arduino.
To work on his assignment I had a few ideas on how to creatively replace the switch button, and my ideas varied in the process of creation. To start with, I built a simple circuit with LED and Switch button, ran it and tested the program.
Then I removed the button and began experimenting with wires. While I have my free time I do some embroidery, and often in that process I need to check the back of the needlework. I always lack the additional light from underneath to see mistakes with needles or the mess that happens sometimes. In order to implement the idea with the unusual switch, I sewed ( no harm done to the cords) the ends of the wires to the needlework so that when folded, they would touch each other. In the beginning I did it with mini aluminum foil balls, but then I found it inconvenient and removed them. The result is very simple, but a nice start.
Final Result: switch video
I would make the contact of both ends of the wires more precise in the future, because sometimes when I moved my hand accidentally and the light turned off.
Originally, I had zero ounce of creativity involved in this project, and was about to finish it in its incomplete state because I couldn’t solve one part of the circuit; but after talking with the professor before Tuesday’s class, I was able to fix the circuit and operate both lights! This was when I spontaneously got the idea to operate the lights in a Christmas-y mood, so I decided to play a Christmas song in the background and turn the LEDs on and off according to the beats of it.
Process/Coding:
I first began creating the analog sensor, because I was more confident with this one since I almost finished it during last Thursday’s class time. I followed the analog schematic sketch that professor provided in our class lectures, and then I simply attached a red LED light to the circuit to make it be connected to the analog sensor. However, I hit a wall when I tried to control the red LED light’s brightness via analog sensor; although the serial monitor showed that my serial print sensor value was reading correctly, the data was not transfer into my LED light for some reason. Thankfully, I was able to consult Professor Shiloh because I was working in the lab, and he told me that I had connected one of my wires in the wrong place – I had forgotten that only the numbers with squiggly lines are the ones that work for analog circuits, and I had plugged it into 13, which was a non-squiggly number. I also had to fix digitalWrite to analogWrite before I could have the operation run smoothly. Here’s a piece of my code after I had fixed it:
Then, I moved onto the digital sensor part, which was a little bit more challenging for me. For this I also referred to professor’s example that he gave during lectures, which was this one:
I basically combined this one and the analog circuit I made earlier, and I also wrote out the code for controlling the blue LED light with the button that I added. Here’s the code that I used for it:
int currentButtonState = digitalRead(buttonPin);
// if the button is currently being prssed down, AND during the last frame is wasn't pressed down
if (currentButtonState == HIGH && prevButtonState == LOW) {
// flip the LED state
if (ledState == HIGH){
ledState = LOW;
} else if (ledState == LOW){
ledState = HIGH;
}
However, when I tried to run the entire code, I realized that only the red LED light was still flashing, but the blue LED light was not on at all. I tried changing the code, but even when I changed it so that the blue LED light was on the whole time, it stayed constant without responding to the button. I couldn’t figure out the problem, so I even asked one of the lab assistants, and we played around with the circuit wires till we realized that I already had a wire from the analog circuit plugged in at 5v, when I also needed to plug in a wire connected to the button in that space as well. So when I plugged in the wire from digital circuit after pulling out the analog circuit wire, the blue LED light responded to the button correctly!
However, because I had to make space for the digital circuit, this meant the red LED light wasn’t going to work; although I’ve tried to play around with different positions of the wires and on the breadboard, I couldn’t get both to work simultaneously but rather separately on their own.
Here’s what I had by this point of the progress:
At this point I was on the verge of giving up…but thankfully Professor Shiloh came to my rescue as usual! Turns out I just had one of the red wires’ ends plugged in a wrong hole of the breadboard (pictured below) – when I switched the end over to the other side’s ‘+’ strip and plugged it in there, both lights started operating smoothly!
At this point, I had the idea of making my project related to Christmas, so I took care of some of the details such as:
Picking a song as the background soundtrack that my lights were going to operate to. I ended up choosing ‘Silent Night’ by Michael Buble (the king of Christmas songs, period) because it was nice and slow, which made the switching between the lights easy.
I had to move locations because the lab lights were too bright, thus making it harder for the LEDs to be depicted on camera. Thankfully, the outside was a perfect location because it was already pretty late at night, making the surroundings dark so that the LEDs could really pop against it.
I controlled the blue light with a button and the red one with my fingers.
Final Product:
I had to break down the video into smaller clips because the file was too big, but here they are, along with the photos:
Reflection:
Despite the very rough start and almost having an even worse ending, I’m glad I stuck with it till the end and asked for help – it was so satisfying to see how everything clicked and operated at the end! Rewatching the video also cracked me up because my stiff, amateur controlling of the LEDs was a stark contrast with the song’s solemn vibe, haha. Something I’d like to fix next time is making the red LED (the analog one) more easy to control because it was hard for me to turn it off completely with my fingers. But overall, it really put my skills to test and was a fun project to work on!
The reading, ‘physical computing’s greatest hits (and misses)’ gives out the message that the professor has been constantly telling us throughout this course. That is, we shouldn’t build our idea as it has been used before but rather learned from the way it was used and alter it to add our own original touch. The writer then goes on to give ideas for interactive projects made such as theremin-like instruments, gloves, floor plans, video mirrors, and much more. The glove was definitely the most fascinating to me as I’m thinking of incorporating it into my final project idea (building a game where hand movement can move players to avoid obstacles).
The second reading explains how we shouldn’t interpret our own work as when we do we are essentially describing what will happen and how the audience will react to this certain interactive media. However, as we mentioned in a previous reading, observing, communicating, and listening are essential to making an object interactive. So what should be done is first, make an initial statement by building your project or designing its behavior then remain silent. After this, allow the audience to take in the project, letting the decide how it works, what each aspect means, and how they will respond to it. By doing this, you should observe their actions listening to what their behavior says about the project, whether it’s easy to understand if they feel emotionally affected by it, and so on. Thus, inventors should limit the amount of bias and instructions they give to a user as the only way to get honest feedback is to stay quiet and observe.
This week’s assignment was about combining digital and analog sensors (switch and potentiometer) to control the behavior of at least two LEDs in a creative way. I figured that using more than two LEDs would lead to too many cables on the board so I decided to go with only two LEDs.
To control the two LEDs with analog and digital sensors, I used my doll (which I’m sometimes scared of because it looks too real) as a jewel detector. If you give the doll gold jewelry (or steel or any type of conductor really, but let’s stick to gold vs non-gold for the purpose of the story), the green LED will start blinking. My job as an assistant is to be on guard; if the green light is blinking everything is fine and the jewelry is gold. But if the light is not turning on and I sense suspicious activity, I press the switch and the red LED turns on.
Breadboard Setup:
Code Highlight:
The LED will only blink if the number received from the potentiometer is greater than 500:
// %%%%%%%%%%%%%%%%%%%%%%%%%%
// convert from 0-1023 proportional to the number of a number of from 0 to 255
if (0 >= inputValue && inputValue <= 500) {
// convert from 0-1023 proportional to the number of a number of from 0 to 255
outputValue = map(inputValue, 0, 500, 0, 255);
}
else if(500 < inputValue && inputValue <= 1023) {
// turn the LED on (HIGH is the voltage level)
digitalWrite(ledPin, HIGH);
// wait for a second
delay(100);
// turn the LED off by making the voltage LOW
digitalWrite(ledPin, LOW);
delay(100);
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%
I am pretty sure when people saw blue and red LED’s together, many of them thought of police light in some way. So did I. Because there were tight requirements in this assignment, I felt those requirements somewhat limited my options.
Schematics
As shown in the picture, there are 1 button, 1 potentiometer, and two LED’s. The parts were chosen to meet the following requirements:
One analog sensor
One digital sensor
LED output in analog fashion
LED output in digital fashion
Setup Overview
The setup is identical to the schematics.
Code Highlight
In the code, I implemented three different output(LED light) modes. Those modes can be set via a switch, and the speed of progress for each modes can be controlled via potentiometer. The below three functions are responsible for different output modes. Brief descriptions of each mode can be found below.
void blink(); // both turn on&off at the same time
void alternate(); // alternate lighting
void glow(); // only blue LED; gradually increase and decrease brightness
These different modes are chosen through a switch, which will be processed in the following code:
If the previous button state was false and the current state is true, the output mode will change to the next one. The previous state has to be tested because a button will keep sending true while it is being pressed. We only want the moment where the signal changes from false to true.
The video shows three different output modes and modified progress speeds (slow & fast).
Reflection
Although it took quite a bit of trials and errors, I became familiarized with using circuit components, Arduino IDE, and fritzing. In the future, I would like to learn how to efficiently design my circuit.
For this assignment, I wanted to make my switch useful and involve it in something I do. I read and write a lot for my Political Science & Law and Society classes, so I chose to use a book to make my switch work. I borrowed some conductive fabric from the IM Lab, my Advanced Introduction to Law and Literature, and my Arduino kit, and got to work.
I decided that I wanted to use an LED light to light up the pages once the book is opened so that the reader is able to read in the dark. However, I obviously knew the LED light we have in our kits is too small, so when looking at the images and video below imagine a bigger and brighter light.
I found this assignment pretty fun to do besides the fact that it didn’t do what I wanted to do which was light up the pages. But, with a bigger and brighter light it would have worked out. For future improvements I’d have to find a way to take into consideration when the reader flips the pages or doesn’t have the book open all the way like it is needed for this switch to work.
For my assignment, I decided I wanted to create something that was fun and could keep me busy for quite some time. Thus, I decided to create a ball-tossing game where the ball has to enter the cup in order for the LED switch to light up. I used the standard materials within the Arduino kit (wires, resistor, LED light) as well as a small foam cup, a silicon ball wrapped in aluminum, and tape.
Initially, I wanted to have the wires taped at the bottom of the cup. After testing out whether the aluminum ball would light up the LED, I realized it wasn’t going to work and instead made holes in the side of the cup for the wires to fit through and sort of catch the ball when it fell (with the help of tape to keep the wires steady).
Images:
Video:
Future Improvements:
Something that I wanted to do but couldn’t figure out was to add another LED light for a sort of backboard and if the ball hits the background the other LED will light up so I think it would be really to do that.
It would also be cool to add more cups assigned to different lights.
Material-wise, I think using a more stable and rigid cup would be useful as it sometimes does move if I hit and miss it. Furthermore, I could add a piece of cardboard or paper in the bottom of the cup to make the wires stable and rigid as I had problems (which I eventually fixed after finding the right angle and using tape on the outside of the cup) where the piece of aluminum wouldn’t touch both wires thus not lighting up the LED.
For my assignment, I wanted to make something unique but also practical. The initial idea I had outlined was a switch that did not exclusively need human manipulation to work and could be tweaked to work automatically. My first thought was a circuit involving a photo sensor.
Brainstorming:
In class, we talked about photoresistors on Tuesday and that gave me the idea to build a circuit with a photoresistor. The dynamic resistivity of these special devices allowed them to ‘open’ and ‘close’ the circuit depending upon the light conditions. So, I came up with the idea of connecting the photoresistor in a circuit so that other parts of the circuit (in my case an LED) would turn on when there was adequate light shining on the photoresistor (the resistance would be very low in this case) and then turn off once the light level reduced (causing the resistance of the photoresistor to shoot up to some absurdly high level). This type of circuit could be used to automate street lights (with a reversed mechanism obviously) to turn off lights during the day and turn them on in the evening.
Implementation:
The photoresistor that I used:
LED on in adequate light:
LED turned off in lower light conditions:
Reflection:
During my experiment, I realized the circuit was not going to be perfect in the real world. although theoretically, in zero light conditions, the resistivity in the photoresistor would approach a near-infinite level, however not only is that impossible in real life, there was ambient light all around the IM lab where I was doing the experiment. It led to the LED being faintly lit even when the circuit was supposed to be ‘open’.
In the future, I will have to link the photoresistor to perhaps a mechanical device like a servo that can physically open the circuit or close it depending on the current passing through the wire.