Led's & Potentiometer - HCI Arduino Uno Build
- Sep 29, 2020
- 2 min read
Updated: Sep 30, 2020
For Human and Computer Interactions this week I had been given the task to come up with a working Arduino sketch and component setup that used multiple LEDs and the dial potentiometer.
I decided that I would put together a row of LED lights and try to use the potentiometer to make the light travel back and forth through the lights as you turned the dial. Based off of what we have done for example setups in class the set up on the board was really easy for me. The programming side of it was challenging to me I understood what needed to happen but didn't know exactly how to type it, but with looking at the code from some project examples and help from Tom (who has way more experience with C++) I was able to get it working.
To do this I basically broke the potentiometer into 6 output sections and set each output from the potentiometer to a different light be using the "HIGH" for on and "LOW" for off and typing in my desired outcome for each one. Using the info from the book (page27) I had originally assigned 0-5 to the potentiometer based off of the level of voltage, but when I ran that it did not work right. Tom saw that the chart that had listed the 0-5 volts also had the number 0 - 1023, so with that we broke that 1023 number down into 6 parts = 0, < 200, < 400, < 600, < 800, and > 800 (one for all lights off, red light on, yellow light on, green light on , blue light on, and all lights on) and that did the trick.

Components Used
Arduino Uno Board
Breadboard
5-volt power
1 Red LED
1 Yellow LED
1 Green LED
1 Blue LED
Potentiometer
4 330 Ω Resistors
9 Wires
VIDEO DEMO
CODE
*/
int sensorPin = A0; // The potentiometer is connected to analog pin 0
int sensorValue; //We declare another integer variable to store the value of the potentiometer
int ledPins[] = {10,11,12,13}; // Defines an array to store the pin numbers of the 4 LEDs.
void setup() // this function runs once when the sketch starts up
{
pinMode(ledPins[0],OUTPUT); // ledPins[0] = 10
pinMode(ledPins[1],OUTPUT); // ledPins[1] = 11
pinMode(ledPins[2],OUTPUT); // ledPins[2] = 12
pinMode(ledPins[3],OUTPUT); // ledPins[3] = 13
}
void loop() // this function runs repeatedly after setup() finishes
{
sensorValue = analogRead(sensorPin);
if(sensorValue == 0)
{
digitalWrite(ledPins[0], LOW);
digitalWrite(ledPins[1], LOW);
digitalWrite(ledPins[2], LOW);
digitalWrite(ledPins[3], LOW);
}
if(sensorValue >= 1 && sensorValue < 200)
{
digitalWrite(ledPins[0], HIGH);
digitalWrite(ledPins[1], LOW);
digitalWrite(ledPins[2], LOW);
digitalWrite(ledPins[3], LOW);
}
if(sensorValue >= 200 && sensorValue < 400)
{
digitalWrite(ledPins[0], LOW);
digitalWrite(ledPins[1], HIGH);
digitalWrite(ledPins[2], LOW);
digitalWrite(ledPins[3], LOW);
}
if(sensorValue >= 400 && sensorValue < 600)
{
digitalWrite(ledPins[0], LOW);
digitalWrite(ledPins[1], LOW);
digitalWrite(ledPins[2], HIGH);
digitalWrite(ledPins[3], LOW);
}
if(sensorValue >= 600 && sensorValue < 800)
{
digitalWrite(ledPins[0], LOW);
digitalWrite(ledPins[1], LOW);
digitalWrite(ledPins[2], LOW);
digitalWrite(ledPins[3], HIGH);
}
if(sensorValue >= 800)
{
digitalWrite(ledPins[0], HIGH);
digitalWrite(ledPins[1], HIGH);
digitalWrite(ledPins[2], HIGH);
digitalWrite(ledPins[3], HIGH);
}
}


































Comments