[Solved] Issue with lighting LED with potentiometer.

I’m doing a project for University and I’ve ran into trouble. When “P” is inputed into the serial port, the program reads the value from the potentiometer and then converts the result to a 0…31 range, in order to display the result in binary with 5 LED’s.

I can’t get this to function properly and I’ve tried looking on google and other forums but I can’t find a solution anywhere.

(I’ve removed some of the code due to max character cap, but it was working and irrelevant to this issue. Also had to remove some of the working code but a lot of it was just repeated. )

String inputInt;
char Operator = inputInt.charAt(0);
int intOne;
int intTwo;
int prevAns;
int answer;
int LED_1 = 5;
int LED_2 = 6;
int LED_4 = 9;
int LED_8 = 10;
int LED_16 = 11;
//------------------------------------------------------------------------------


void printWelcomeMessage()
{
  Serial.println("Please enter the integer values you wish to calculate:");
}
//-------------------------------------------------------------------------------


String promptReadln()
{
  while (!Serial.available()) {};
  return Serial.readString();
}


//-------------------------------------------------------------------------------


void errorMessage()
{
  Serial.println("ERROR");
  Serial.println("");
}


//-------------------------------------------------------------------------------



//-------------------------------------------------------------------------------

void turnOffLED() {
  digitalWrite(LED_1, LOW);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}

//-------------------------------------------------------------------------------

void LED0() {
  digitalWrite(LED_1, LOW);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);

}
void LED1() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED2() {
  digitalWrite(LED_1, LOW);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED3() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED4() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED5() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, HIGH);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED6() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED7() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_4, HIGH);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED8() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED9() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, HIGH);
  digitalWrite(LED_16, LOW);
}
void LED10() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, HIGH);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED11() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, HIGH);
  digitalWrite(LED_8, HIGH);
  digitalWrite(LED_16, LOW);
}
void LED12() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED13() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, HIGH);
  digitalWrite(LED_16, LOW);
}
void LED14() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_4, HIGH);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED15() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_4, HIGH);
  digitalWrite(LED_8, HIGH);
  digitalWrite(LED_16, LOW);
}


//-------------------------------------------------------------------------------

void potentiometerToNewRange(int sensorValue) {
  if (0 >= sensorValue <= 31) {
    LED0();
  }
  else if (32 >= sensorValue <= 63) {
    LED1();
  }
  else if (64 >= sensorValue <= 95) {
    LED2();
  }
  else if (96 >= sensorValue <= 127) {
    LED3();
  }
  else if (128 >= sensorValue <= 159) {
    LED4();
  }
  else if (160 >= sensorValue <= 191) {
    LED5();
  }
  else if (192 >= sensorValue <= 223) {
    LED6();
  }
  else if (224 >= sensorValue <= 255) {
    LED7();
  }
  else if (256 >= sensorValue <= 287) {
    LED8();
  }
  else if (288 >= sensorValue <= 319) {
    LED9();
  }
  else if (320 >= sensorValue <= 351) {
    LED10();
  }
  else if (352 >= sensorValue <= 383) {
    LED11();
  }
  else if (384 >= sensorValue <= 415) {
    LED12();
  }
  else if (416 >= sensorValue <= 447) {
    LED13();
  }
  else if (448 >= sensorValue <= 479) {
    LED14();
  }
  else if (480 >= sensorValue <= 511) {
    LED15();
  }
  else if (512 >= sensorValue <= 543) {
    LED16();
  }
  
}

//-------------------------------------------------------------------------------

void getPotentiometerValue() {
  int sensorValue = analogRead(A0);
  //Serial.println(sensorValue);
  potentiometerToNewRange(sensorValue);
  delay(300);
}

//-------------------------------------------------------------------------------

String stageOne(String inputInt) {
  if (inputInt.length() == 5) {
    previousAnswer(inputInt);
  }
  else if (inputInt.length() == 8) {
    whatOperator(inputInt);
  }
  else if (inputInt.length() == 2 && inputInt.charAt(0) == 'P') {
    getPotentiometerValue();
  }
  else {
    errorMessage();
  }
}

//-------------------------------------------------------------------------------




//-------------------------------------------------------------------------------


void setup()
{
  Serial.begin(9600);
  pinMode (LED_1, OUTPUT);
  pinMode (LED_2, OUTPUT);
  pinMode (LED_4, OUTPUT);
  pinMode (LED_8, OUTPUT);
  pinMode (LED_16, OUTPUT);
  prevAns = 0;
  printWelcomeMessage();
  Serial.println("");
}
//-------------------------------------------------------------------------------


void loop()
{
  inputInt = promptReadln();
  inputInt.toUpperCase();
  Serial.print("Input: ");
  Serial.print(inputInt);
  stageOne(inputInt);
  if (answer != "P") {
    prevAns = answer;
  }
  Serial.println("");

}

Project.ino (13.4 KB)

Solved?

There's no explanation of how you fixed it or what was wrong. No use to other forum readers looking for the solution to a similar problem.

Something like this comes up frequently.
See this thread.
Leo…