This project uses potentiometer to generate analog signal input for Arduino, and have some fun control of several LEDs when rotating the potentiometer, like levelling up/down the LEDs, seamlessly changing LEDs blinking speed. Goals:
Potentiometer what and how, use as voltage divider
Analog input in Arduino, analogRead(), use map() function to scale down the number range
Compound if-else logic practice,, and try out the switch-case flow controlling statement
START A BIT > Projects Library > This page
[ ] Simple: 30-60 minutes
[x] Moderate: 1-2 hours
[ ] Challenging: more than 2 hours
The estimation bases on average situation without unexpected troubleshooting, assuming that the student meets prerequisites. It only indicates rough time needed to complete this project, but not about technical difficulty, not covering discussion and sharing time.
One should have prior knowledge
Purpose: Get to know what is potentiometer and how to setup hardware connection
Exchange with your classmates to inspect correctness for each other
Purpose: Understand how to read analog input, and inspect the converted numeric values by serial printing
What are the min and max value in your test?
Have we missed pinMode(A0, INPUT)?
int pot_input;
void setup() {
Serial.begin(9600);
}
void loop() {
// Read analog input from pin A0
// resulted as integer ranged 0-1023
pot_input = analogRead(A0);
Serial.println(pot_input);
delay(300);
}
Purpose: Use analog input value, practice compound if-else logic
if-else
Fix the incomplete /* MISSING CODES HERE */ to make it working
This example solution is clear with the logic, but not very neat, can you figure out another solution still using if-else?
/* Please fix the MISSING CODES and debug working */
#define LED_GREEN 2
#define LED_YELLOW 3
#define LED_RED 4
int pot_input;
void setup() {
Serial.begin(9600);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_YELLOW, OUTPUT);
pinMode(LED_RED, OUTPUT);
}
void loop() {
// Read analog input from pin A0
// Resulted as integer ranged 0-1023
pot_input = analogRead(A0);
Serial.println(pot_input);
if (pot_input < 300) {
/* MISSING CODES HERE */
}
else if (pot_input >= 300 && pot_input < 600) {
/* MISSING CODES HERE */
}
else if (pot_input >= 600 && pot_input < 900) {
/* MISSING CODES HERE */
}
else if (pot_input >= 900) {
/* MISSING CODES HERE */
}
delay(300);
}
if (pot_input < 300) {
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_RED, LOW);
}
else if (pot_input >= 300 && pot_input < 600) {
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_RED, LOW);
}
else if (pot_input >= 600 && pot_input < 900) {
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_YELLOW, HIGH);
digitalWrite(LED_RED, LOW);
}
else if (pot_input >= 900) {
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_YELLOW, HIGH);
digitalWrite(LED_RED, HIGH);
}
#define LED_GREEN 2
#define LED_YELLOW 3
#define LED_RED 4
int pot_input;
void setup() {
Serial.begin(9600);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_YELLOW, OUTPUT);
pinMode(LED_RED, OUTPUT);
}
void loop() {
// Read analog input from pin A0
// Resulted as integer ranged 0-1023
pot_input = analogRead(A0);
Serial.println(pot_input);
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_RED, LOW);
if (pot_input >= 300) {
digitalWrite(LED_GREEN, HIGH);
}
if (pot_input >= 600) {
digitalWrite(LED_YELLOW, HIGH);
}
if (pot_input >= 900) {
digitalWrite(LED_RED, HIGH);
}
delay(300);
}
Purpose: Use map() function to scale down the number range, and try out the seamless changing blinking LED
What is the different experience comparing with the blinking speed changing using buttons?
#define LED_GREEN 2
#define LED_YELLOW 3
#define LED_RED 4
int pot_input;
int scaled_value;
void setup() {
Serial.begin(9600);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_YELLOW, OUTPUT);
pinMode(LED_RED, OUTPUT);
}
void loop() {
// Read analog input from pin A0
// Resulted as integer ranged 0-1023
pot_input = analogRead(A0);
// Scale down the range 0-1023 to 50-1000, which will be used as ms
scaled_value = map(pot_input, 0, 1023, 50, 1000);
Serial.print(pot_input);
Serial.print(",");
Serial.println(scaled_value);
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_RED, LOW);
delay(scaled_value);
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_YELLOW, HIGH);
digitalWrite(LED_RED, HIGH);
delay(scaled_value);
}
Purpose: Creative task for your ideas, as an optional challenge task
Add more LEDs, and find a better way to solve the need to level up/down the LEDs when rotating the potentiometer
Use map() function to scale down the number range, and try out the switch-case flow controlling statement for the same result as Task 3. This requires further knowledge, not necessary to understand every detail, but good as practice and inspiration
#define LED_GREEN 2
#define LED_YELLOW 3
#define LED_RED 4
int pot_input;
int scaled_value;
void setup() {
Serial.begin(9600);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_YELLOW, OUTPUT);
pinMode(LED_RED, OUTPUT);
}
void loop() {
// Read analog input from pin A0
// Resulted as integer ranged 0-1023
pot_input = analogRead(A0);
// Scale down the range 0-1023 to 0-3
scaled_value = map(pot_input, 0, 1023, 0, 3);
Serial.print(pot_input);
Serial.print(",");
Serial.println(scaled_value);
switch(scaled_value) {
case 0:
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_RED, LOW);
break;
case 1:
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_RED, LOW);
break;
case 2:
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_YELLOW, HIGH);
digitalWrite(LED_RED, LOW);
break;
case 3:
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_YELLOW, HIGH);
digitalWrite(LED_RED, HIGH);
}
delay(300);
}
Purpose: Enhance basic knowledge understanding, reflect and summarize what we have learnt
What is potentiometer, what is the main purpose of using it in this project?
How do we get analog input? What are the values we get there in the program?
Tips: Review knowledge from here if needed
Follow the tutor to take 5-10 minutes to review the questions and answers, correct some possible misunderstood issues or faults
Take 1-2 minutes to explore anything on the tools and the making flow, with curiosity, no matter how much prior knowledge you have, you should make
At least 1 interesting finding
At least 1 curious question
Used by LP2519: Analog Input, 25w19