Bar Graph controlling by potentiometer

Hi guys, I’ve done with this assignment, however, the Prof. asked me to re-write my code but she said No global variables are permitted. Would you please help me with that and just fix something up ?

thanks …

int n = 10;
int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10,11};
int leds[] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};
 
int p = A0;
int value = 0;
 
void setup() {
  int count = 0;
  for(count = 0; count < n; count++) {
    pinMode(ledPins[count], OUTPUT);
  }
}
 
void loop(){
  value = analogRead(p);
  value /= 100; 
  
  int count = 0;
  for(count = 0; count < n; count++) {
    if(value > count) {
      leds[count] = HIGH;
    }
    else {
      leds[count] = LOW;
    }
  digitalWrite(ledPins[count], leds[count]);
  }
 
}

Hi, should be easy enough just using #define.

Paul

There are a couple of different ways of looking at such a request, one of which it to ask for a decent explanation why you are not supposed to use global variables.

You make the variables local by putting the variable definitions for "count" inside setup() and "value" inside loop().

And of course, you make "p" a const. In actual fact, the compiler will do that for you.

If that is not sufficient, then I would really want to know why.

Paul__B:
I would really want to know why.

Because The Professor said so 8)

Pedro147: Because The Professor said so 8)

Maybe the Professor wants you to ask why...

Just move the arrays into each function that needs it and declair them as static int