Hi there,
I am currently working on a script that is reading values sent from an app on my phone via bluetooth (I am using an HC-05 module). These values can range anywhere between 0 and 100 and are sent every second. I would like to use specific values to change the colour of an LED.
For example:
If the serial monitor reads 9 I would like the LED to turn red.
And if the serial monitor reads 10 I would like the LED to turn green.
This works with values 9 and below but anything 10 and over does not. I am hoping someone could help me understand this.
Here is my script:
int incomingByte = 0; // for incoming serial data
int red_light_pin= 9;
int green_light_pin = 10;
int blue_light_pin = 11;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(red_light_pin, OUTPUT);
pinMode(green_light_pin, OUTPUT);
pinMode(blue_light_pin, OUTPUT);
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
Serial.print(incomingByte, DEC);
Serial.print("\n"); //New line
if(incomingByte == '9') {
digitalWrite(red_light_pin, 255);
digitalWrite(green_light_pin, 0);
digitalWrite(blue_light_pin, 0);
}
else if(incomingByte == '10') {
digitalWrite(red_light_pin, 0);
digitalWrite(green_light_pin, 255);
digitalWrite(blue_light_pin, 0);
}
}
}
Thank you!