I have a relay and bluetooth module connected to Arduino Uno. I send data such as digit 1 to the bluetooth module and I view it in the Serial Monitor. If I send 1 from serial monitor I get 4910 printed.
I would like to turn on the relay and have it stay on until I send a different digit. However, in my code below it doesnt work, the relay doesnt stay on.
Only, if I put the digital.write under while(Serial.available()) it works.
const int ledPin = 11;
int relayOn;
void setup() {
//initialize serial port for logs
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
while (Serial.available()) {
Serial.println("Data: ");
Serial.println(Serial.read());
if(Serial.read())
{
relayOn = Serial.parseInt();
}
while (relayOn > 1)
{
digitalWrite(ledPin, HIGH);
}
}
}