I am struggling to get my head around why this just won't work. In my simple mind this simple sketch should do as it's told. But I am obviously missing something fundamental.
If anyone could shed some light on what I am doing wrong that would be very good!
The sketch is just to control a relay by typing on or off into the serial monitor. However although it uploads, all I get is the Serial.println
repeating and no action on the physical relay.
I can manage to control things over serial by using the switch, case statements but I wanted to try it using full words rather than single chars and also using IF statements etc.
Thanks in advance!
EDIT
I just changed the string inputs to a char input and serial.readstring
to just serial.read
and it works with just entering a 1 or a 2 for example. So to re-ask my question - how can I get it to respond the same but using words? Thanks!
int relay=8;
String serialInput;
void setup() {
Serial.begin(9600);
pinMode(relay, OUTPUT);
}
void loop() {
Serial.println("To toggle relay enter on or off");
while (Serial.available()==0) {}
serialInput = Serial.readString();
if(serialInput=="on") {
digitalWrite(relay, HIGH);
}
if(serialInput=="off") {
digitalWrite(relay, LOW);
}
}