Sorry I'm really new to coding and the Arduino and I am not sure what I'm doing wrong in this code to send a command from the serial monitor to my Arduino Uno and have it send out power through a pin (in my case pin 13 for a simple led).
// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later
int ledPin = 13;
String readString;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
Serial.println("serial on/off test 0021"); // so I can keep track
}
void loop() {
while (Serial.available()) {
delay(3);
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
if (readString == "on")
{
digitalWrite(ledPin, HIGH);
}
if (readString == "off")
{
digitalWrite(ledPin, LOW);
}
readString="";
}
}
How is your sketch supposed to read Serial data right after power on/restart, before any of it's hardware is initialized and you haven't even sent it anything?
if (serial == 'motor'){
motor isn't a single character, so you shouldn't be comparing it against one. It's a string.
I recommend looking at some of the examples to familiarize yourself with how to read things in from the Serial monitor.
int led = 13;
byte incomingByte; // variable to hold incoming databyte
void setup(){
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop(){
if (Serial.available()>0){ // any data received?
incomingByte = Serial.read(); // read it
if (incomingByte == '0'){ // act on the data
Serial.println ("led turning on");
digitalWrite (13, HIGH);
}
if (incomingByte == '1'){ // or act on the data
Serial.println ("led turning off");
digitalWrite (13, LOW);
}
if (( incomingByte != '1') && (incomingByte !='0')){ // or report it as bad
Serial.println ("bad data");
}
}
}