Issue with Serial Data when Trying to Light Up 5 LEDS

Hello All,

I am very new to this, and I am trying my best to post this question appropriately given that another user was kind of rude when I didn’t properly format my post.

Anyhow, here’s the problem. I have a 5 LED set up. I have five different scenarios in the code. When I enter a number between 1 and 100, I want them to light up based on how close to 100 I am. For instance, when I enter 60, I want three LEDs to light up…at 80…four will light up.

The code I wrote is accepted by the Arudino. When I press 1…1 LED lights up. If I press 0…all go dim. However, when I enter any number higher than 1, nothing else happens. I took a look at my serial data. When I enter the number 0, I see that the number 48 appears on the serial data screen. If I press 1, it shows up as 49…and so on and so forth between numbers 1 through 10.

Any help? Here is my code:

const int ledPin1 = 9;
const int ledPin2 = 10;
const int ledPin3 = 11;
const int ledPin4 = 12;
const int ledPin5 = 13;

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);
 
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);

  int value = 0;
  
}

void loop()

{

int value;

if(Serial.available())

{

delay(50);

while(Serial.available() >0)

{

value=Serial.read();
  
Serial.println(value, DEC);
  

  
if(value<='99' && value>='80'){
digitalWrite(13,HIGH);
digitalWrite(12,HIGH);
digitalWrite(11,HIGH);
digitalWrite(10,HIGH);
digitalWrite(9,HIGH);

}

else if(value<='79' && value>='60'){
digitalWrite(13,HIGH);
digitalWrite(12,HIGH);
digitalWrite(11,HIGH);
digitalWrite(10,HIGH);
digitalWrite(9,LOW);

}

else if(value<='59' && value>='40'){
digitalWrite(13,HIGH);
digitalWrite(12,HIGH);
digitalWrite(11,HIGH);
digitalWrite(10,LOW);
digitalWrite(9,LOW);
  
}
  
else if(value<='39' && value>='20'){
digitalWrite(13,HIGH);
digitalWrite(12,HIGH);
digitalWrite(11,LOW);
digitalWrite(10,LOW);
digitalWrite(9,LOW);
  
}
  
else if(value<='19' && value>='1'){
digitalWrite(13,HIGH);
digitalWrite(12,LOW);
digitalWrite(11,LOW);
digitalWrite(10,LOW);
digitalWrite(9,LOW);
  
}

  
else {
  
digitalWrite(13, LOW);
digitalWrite(12,LOW);
digitalWrite(11,LOW);
digitalWrite(10,LOW);
digitalWrite(9,LOW);

} 
  
  
}
  
}
}

Serial data comes as ASCII, see www.asciitable.com

So you need to subtract 48 from every byte received, and then combine 2 or 3 bytes into one number (value) if you're expecting 0 to 100.
So '0' comes in as 48, so subtract 48.
1,2,3,4,5,6,7,8,9 come in as 49, 50, 51, etc.

value then is byte0 * 100 + byte 1 * 10 + byte 2 to result in 0 to 100.

You also need to modify your if() statements. Things inside a single quote are 1 char so things like '80' or '60' don't make sense. After you get your input converted to a number, just use numbers in the if() statements

Hello All,

So this works for numbers 1 through 9:

value = Serial.read() - '0';

Then I added this so that I could get numbers 10 through 99 :

value = Serial.read() - '0';
  
  
value2 = Serial.read() - '0';

Serial.println(value && value2);

Any further guidance is very much appreciated!

'&&' is the logical and operator. The value you want is value * 10 + value2

You also need to check if there is a char available to read() before just blindly doing so. In the world of Arduino, the serial input is VERY slow and often doesn't become available all at once.

That worked perfectly! Thank you very much.