0

(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)?

3
  • 1
    In what way does it "not work"?
    – Majenko
    Commented 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
    Commented Jan 24, 2022 at 19:16
  • i see no debugging code ... how do you know that the pot is being read as expected?
    – jsotola
    Commented Jan 24, 2022 at 19:21

1 Answer 1

1
  1. Only the pins with labels starting with “A” (A0 through A5 on the Uno) can be used as analog inputs.
  2. The pin of the potentiometer that is currently not connected has to be connected to 5V.
2
  • Thanks for the help, can't believe I overlooked something like that.
    – Dezter
    Commented Jan 24, 2022 at 19:31
  • Also I switched HIGH and LOW lol
    – Dezter
    Commented Jan 24, 2022 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.