LEDs

I am doing a project which able converts ASCII to integer meanwhile one of the three LEDs will light up according to the values received in ASCii. For example, if the values received is 127 the red LED lights up, and so on. The component that i am using are Arduino uno with ATMEGA 328 IC. However the LEDs will not light up according to the value i typed in the serial monitor when i tested it.

byte data; //char to int storage
int redPin = 5; // pin used for the LEDs
int greenPin = 6;
int yellowPin = 7;
int final = 0; // converted value

void setup() {
 // put your setup code here, to run once:
 Serial.begin(9600); //initalize serial
 pinMode(redPin, OUTPUT); // make the pins output
 pinMode(greenPin, OUTPUT);
 pinMode(yellowPin, OUTPUT);
}

char converter (char convertion)// function for ascii convert to int, for pitch
{
   char ch1 = Serial.read();
   char ch2 = Serial.read();
   char ch3 = Serial.read();
   char ch4 = Serial.read();
   Serial.print(final, HEX); // print data to the serial port as human readable ACSII
   
   if (ch1 == 'P')
   {
     if (ch2 >= '1')
     {
       data = (ch2 - '0') * 100 + (ch3 - '0') * 10 + ch4 - '0';
       delay(1000);
     }
     
   }
   
   else
   {
     data = (ch2 - '0') * 10 + (ch3 - '0');
   }
   
   Serial.print("end of loop\n");
   delay(1000);
}

void loop() {
 // put your main code here, to run repeatedly:
 
 final = Serial.read();
 Serial.println("start");
 Serial.println("Pitch\n");

 delay(2000);
 if(Serial.available() > 0) // don't read unless
 {
     if (converter(final) == 'P127')
     {
       digitalWrite(redPin, HIGH);
       delay(1000);
     }
 
     if (converter(final) > 'P127')
     {
       digitalWrite(greenPin, HIGH);
       delay(1000);
     }
 
     if(converter(final) < 'P127')
     {
       digitalWrite(yellowPin, HIGH);
       delay(1000);
     }
     Serial.print(final, HEX); // print data to the serial port as human readable ACSII
     final = 0; // reset back to zero for another interation. 
 }
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Please put your code in its own window as seen in other posts. This can be done by placing     [code] and [/code]  around the code. This makes it easier for others to read

And read this
http://forum.arduino.cc/index.php/topic,148850.0.html

Weedpharma

How to use this forum

Hi,
I hope you are using series resistors on your LEDs,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Tom.... :slight_smile:

How to use this forum

not sure you got a change to read this yet.

you can re-vist your first post and add code tags.

Classic error you do :-

if(Serial.available() > 0) // don't read unless
  {
      if (converter(final) == 'P127')

Which function contains:-

char converter (char convertion)// function for ascii convert to int, for pitch
{
    char ch1 = Serial.read();
    char ch2 = Serial.read();
    char ch3 = Serial.read();
    char ch4 = Serial.read();

So if the input buffer contains one character you read four characters from it. Smart?

Stop sniping. Stay on-topic. Only warning.