I'm working on a project to light an led on an uno arduino when I hit number one key on my laptop keyboard. Here is the code that I have so far. I just don't understand why it won't light the led on pin 13...any help would be greatly appreciated!
int inByte = 0; // initialize the variable inByte
const int ledPin = 13; // pin that the LED is attached to
void setup(){
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output
Serial.begin(57600); // set serial monitor to same speed
}
void loop(){
if (Serial.available()>0) { // check if any data received
inByte = Serial.read(); // yes, so read it from incoming buffer
if (inByte == 1){
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin,LOW);
}
}
}
How is the data being sent over serial, are you sending the actual number 1 or the keyboard character '1'? If you press the number one on a keyboard the actual byte being sent is the ASCII number for the character 1 (49 in decimal). If that is the case, try
if (inByte == '1'){
digitalWrite(ledPin, HIGH);
}
the single quotes say to match the character '1', not the actual integer
I tried your recommendation, but it didn't work. I know that the information is making it to the arduino: the rx led flashes for quick second when I send "1" over the serial connection... :~
I think that your serial monitor is configured to send something as an end-of-line" marker. When you type a '1', then hit , the monitor sends the Arduino a '1' followed by something that ends the line. That might be an ASCII carriage return, a newline, or both, depending on the option that's selected. The Arduino is turning on the LED, but, when it receives an end-of-line character, it turns the LED back off. At 57600 baud, there's something less than 200 microseconds between characters. The LED flashes so fast that you can't see it.
To fix it, you can set the "line ending" option to "No line ending." Or, you could leave the serial monitor just like it is, and change your code to turn the LED on when the Arduino receives a '1', and off when it receives a '2' - or any character that doesn't end the line. Then you could enjoy this program for a few seconds more, as, heady with success, you turn the light on and off a few times.
I'm using the serial monitor that comes with the arduino compiler and I have "no line ending" set. I also tried using 2 to set the led to a digital low to no avail...
void loop(){
if (Serial.available()>0) { // check if any data received
inByte = Serial.read(); // yes, so read it from incoming buffer
if (inByte == '1'){
digitalWrite(ledPin, HIGH);
}
else if (inByte == '2') {
digitalWrite(ledPin,LOW);
}
}
}
After adding the additional read-back line and double checking the baud rate of the serial monitor, the serial monitor responds with a 49. So I'm guessing that ASCII is being sent to the arduino? Do I have to set up a function to change the ASCII value to a one for HIGH (or a two for LOW) or should I just try to get the arduino to act on the value of 49?
Here's my almost identical code..... I'm looking for an 'h' or an 'H" and switch the led on then; it worked last time I tried it!
char incomingByte; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(13,OUTPUT);
digitalWrite(13, LOW);
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte);
}
if (incomingByte == 'h' || incomingByte == 'H')
{
digitalWrite(13, HIGH);
}
else
{
digitalWrite(13, LOW);
}
}
@Jim, Thanks your code works great! I'll study the differences and try to figure out why mine doesn't work. Probably has something to do with set and constant value for pin 13 near the beginning of the code...
Dude, I pasted your code, and it worked just as expected. I took everything from the beginning through setup() from the original code, and loop() to the end from the revised code you posted. I got the LED on when I sent a '1', off when I sent a '2', and no change for anything else. It works regardless of the setting of the end-of-line action. And, it worked in IDE 0.22 and in 1.01. If yours didn't work, and some other code did, something else was different.
Indeed. But, I used the original poster's code exactly as it was shown in the posts, with comments, variable types, and indention all unchanged, and I got the expected results - the LED illuminated when I sent a '1', darkened when I sent a '2', and ignored anything else. Again, that's with declarations and setup() from the original post, and the revised loop() from his later post, copied straight from the browser.
I'm going to guess now that it was a baud rate mismatch between the monitor and the program, as our buddy James suggested. I see that the working program uses 9600 baud, while the original uses 57600. Or, there's maybe a mismatch between the code that was actually in use, and the code that got posted.
I suspect we'll never know. The evidence has likely gone the way of all bits.
I agree the language reference does say the return value of Serial.read() is an int, so that part is fine. Though there has to be something, and I would love to hear what if the OP can figure out what went wrong.
You did, of course, change the serial monitor to use 57600?
I'm going to guess now that it was a baud rate mismatch between the monitor and the program, as our buddy James suggested. I see that the working program uses 9600 baud, while the original uses 57600. Or, there's maybe a mismatch between the code that was actually in use, and the code that got posted.
I checked the baud rate of the serial window and indeed there was a mismatch! I really appreciate all of your discussion...thank you very much!