(Backstory) So, I took an computer engineering class in high school (grade 11 level), where I learned a bit about breadboarding as well as arduinos (I would like to stress a bit). It has been 5 years, and I don't remember much; but the other day my little brother started taking the same class. He has the following assignment, which I have also endeavored to try for fun:
(Not Backstory) I need to make a potentiometer control a 7-segment display so that it displays a number (0-9) based on the potentiometers value. I hopped on to tinkercad to test stuff out before acuatally doing anything, and I came up with the following real quick:
I've tested the above model and I know it works, but the following code does not work.
int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;
int potentiometer = 9;
void setup() {
for(int pins = 2; pins <=8; pins++){
pinMode(pins, OUTPUT);
}
pinMode(potentiometer, INPUT);
}
void loop() {
int pRes;
pRes = analogRead(potentiometer);
if (pRes >=0 && pRes <=101){
//0
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
}
else if (pRes >=102 && pRes <=203){
//1
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
else if (pRes >=204 && pRes <=305){
//2
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);
}
else if (pRes >=306 && pRes <=407){
//3
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);
}
else if (pRes >=408 && pRes <=509){
//4
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}
else if (pRes >=510 && pRes <=611){
//5
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}
else if (pRes >=612 && pRes <=713){
//6
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}
else if (pRes >=714 && pRes <=815){
//7
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
else if (pRes >=816 && pRes <=917){
//8
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}
else if (pRes >=918 && pRes <=1023){
//9
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}
}
Can someone explain to me what I've done wrong (and offer corrections)?
-
1In what way does it "not work"?Majenko– Majenko2022年01月24日 19:01:50 +00:00Commented Jan 24, 2022 at 19:01
-
I think you wired the potentiometer incorrectly. The middle pin goes to an analog input pin (you used a digital pin on one of the outer pins), the outer pins to ground and 5V respectively.chrisl– chrisl2022年01月24日 19:16:24 +00:00Commented Jan 24, 2022 at 19:16
-
i see no debugging code ... how do you know that the pot is being read as expected?jsotola– jsotola2022年01月24日 19:21:18 +00:00Commented Jan 24, 2022 at 19:21
1 Answer 1
- Only the pins with labels starting with "A" (A0 through A5 on the Uno) can be used as analog inputs.
- The pin of the potentiometer that is currently not connected has to be connected to 5V.
Explore related questions
See similar questions with these tags.