Hi, this is my first Arduino project, and I am having trouble with serial communication.
I have a sensor on analog pin 3, and what I want is for the Arduino to wait until a signal is sent to serial (by another program) and then respond by checking the sensor and printing its current value.
I am testing this by plugging my Arduino in, opening up the serial monitor in the Arduino program, and sending single characters every once in a while. It seems to mostly be working - the Arduino doesn't print anything until I send it a character, and when it does, it prints a reasonable value for the sensor - but for every character I send, the Arduino prints the sensor value twice. For example, say the serial monitor is blank. I type '1' and hit send. Now the serial monitor says "1017 \n 1017". The weirdest part is that the two values that print together don't always match, like sometimes it will print "1017 \n 1016".
Why is this happening, and how should I change my code so that it prints only once? Here is my current code:
int inPin = 3; // name the phototransistor input pin
int val = 0; // set input to 0 to start
int inByte = 0; // set up variable to pick up data sent from python
void setup(){
Serial.begin(9600); // set up serial communication
}
void loop(){
inByte = Serial.read();
if (inByte != -1) { //if python has asked for value
val = analogRead(inPin); //get input
Serial.println(val); // print input to serial window
delay(1);
}
}
What line ending setting do you have for the Serial Monitor? If you enter '1', and hit send, with a line ending of CR and LF, there will be three characters sent.
wildbill & PaulS - thank you both very much for pointing me towards my Serial Monitor settings!
I had serial monitor set to "Newline" instead of "No line ending". I hadn't realized that that setting was for the input I was giving at the top, and not for how to display what the Arduino was sending. It works as expected now!
SparksFLY:
Thank you. Your fix, fixed mine, too. Golly, I have to get some sleep. This doggone stuff is very addictive.
Arnold
For whatever reason, my code will only process single digits. Any ideas? Anyone?
long number = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.flush(); // clear any "junk" out of the serial buffer before waiting
while (Serial.available() == 0)
{
}
while (Serial.available() > 0)
{
number = Serial.read() - '0';
}
Serial.print("You entered: ");
Serial.println(number);
Serial.print(number);
Serial.print(" multiplied by two is ");
number = number * 2;
Serial.println(number);
Serial.println();
For whatever reason, my code will only process single digits. Any ideas?
Because that's all your input ever consists of.
If you want multi-digit numbers, you need to signal to your sketch
a) where the number begins
b) where the number ends
Because you only read one at a time before processing. Currently you are doing this
wait for char
read char
process char
repeat
you need to do something more like
wait for char
read char
is it CR/LF? // or other end of chars delimiter
if yes
process chars in array
else
save char in array
keep reading chars
repeat