hi everyone!
trying to re-learn Arduino after many years without any practice.
as a pretty basic exercise to myself, I'm trying to read 1 or 0 from the serial monitor and use it to light up an LED (and print it to the serial monitor too) and for some reason, I'm getting weird numbers...
can anyone help me, please?
Set your serial monitor to use no line ending.
Welcome to the group.
Show us a good schematic of your circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.
In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
Use the </> icon from the ‘reply menu’ to attach the copied sketch.
Is your monitor baud rate matching your serial baud rate?
@mental_mamba97, your topic has been moved to a more suitable location on the forum (as you're using IDE 1.8.13).
Please don't post screenshots of code. What are you going to do if your code no longer fits on a screen? Please take some time to read How to get the best out of this forum and next time please use code tags when posting code as described in the link.
What weird numbers? 48 (for zero) or 49 (for one)?
yes, exactly.
how did you know?
yes it is
print prints a number of you pass it a number (char, int, long, ...).
You can use Serial.write(ledState) to see the character.
Also have a look at e.g. http://www.asciitable.com/ to see the numbers that belong to characters.
try this with the serial monitor set to "No line ending" (bottom). a newline would set the pin to a non-zero value
const byte ledPin = 10;
void
loop ()
{
if (Serial.available ()) {
char c = Serial.read ();
digitalWrite (ledPin, c - '0');
Serial.println (c);
}
}
void
setup ()
{
Serial.begin (9600);
pinMode (ledPin, OUTPUT);
}
thank you! Serial.write fixed it!
there is another problem though. it prints out 3 lines in a row
Press 1 to turn LED ON
Press 0 to turn LED OFF
led state is: 1
led state is:
led state is:
What is the line-ending in serial monitor set to? The two extra characters are more than likely <CR> (carriage return) and <LF> (linefeed).
it was set to both. so, for future reference, if I want to see the current value of a certain variable, I should use Serial.write? What's the difference with Serial.print?
Not necessarily. Your 'problem' is that the serial monitor sends a character which is basically a number. And print converts a number to the text representation of that number and sends that.
Let's say that you send the character 'A'; it goes over the line as 65. Serial.print converts that to the text representation of the number 65 which is the characters '6' and '5' which in turn will be the numbers 54 and 53 that are send to the serial monitor. Because the serial monitor is a text based utility, it treats 54 and 53 as numbers representing characters and you will see the characters '6' and '5'.
Serial.write does not do a conversion so if you write the number 65 it will write the number. Serial monitor does not change so it will treat 65 as a number representing a character.
now I have a different problem... >_<
for some reason, now it will just print LED OFF and won't turn on/off the led
int led = 0;
int ledPin = 13;
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println("Press 1 to turn LED ON");
}
void loop()
{
if (Serial.available() > 0)
{
led = Serial.read();
if (led == 1)
{
digitalWrite(ledPin, HIGH);
delay(500);
Serial.print("LED ON");
}
else (led == 2);
{
digitalWrite(ledPin, LOW);
delay(500);
Serial.print("LED OFF");
}
}
}
Oops.
You probably should use
(led == '2')
but you certainly will want to lose the semicolon.
Please remember to use code tags when posting code
thank you! and i will
when I cut off the semicolon it gives me a compilation error message
![]()
so it looks like i have to put it there
plus, how do i make him hold it? the led turns on for the 0.5sec delay and then turns off
No, the semicolon goes, but you need an "if" after the "else"
alright, that worked
what about making it hold it? the led turns on for the 0.5sec delay and then turns off
Post your code.
