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.