how would i turn on/off a led via Serial monitor using on and off?
profpjmg:
If i write the code only with Serial.read() the code works, and if i write the code only with Serial.parsInt() the code also works, but both together d'ont work.Could you tell me why? I would be very thankful.
1. This is OP's sketch.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
char valA;
int valB;
void setup()
{
Serial.begin(9600);
lcd.begin(16,2);
}
void loop()
{
if (Serial.available()>0)
{
valA=Serial.parseInt();
lcd.write(valA);
valB=Serial.read();
lcd.write(valB);
}
}
1. All those problems that you have stated have come from your ' no knowledge' on the working principle of the int x = Serial.parseInt(); instruction. Let us see how this instruction works.
(1) This instruction waits for a 'certain amount of time' to see that characters/digits are coming from the InputBox of the Serial Monitor to the UNO. This 'certain amount of time' (say 20 sec) is pre-set in the setup() function by this instruction: Serial.setTimeout(20000);.
(2) Assume that within 5 sec of the above time-window, the user has entered 123Y from the InputBox of the Serial Monitor. The parseInt() method keeps receiving/storing all numeral characters ( 0 to 9) as they are coming from the Serial Monitor; but, it will stop accepting anymore characters/digits as soon as it receives/stores a non-digit (other than 0 to 9) character. Let us ( at the moment) forget what will happen if the user sends abc1234Y od -w123? Now, the stored information is: 1 2 3 Y
(3) The parseInt() method will process the ASCII codes of 1 2 3 (and not Y). The characters will be taken out from the FIFO and will converted to integer value which will be saved into variable int valA (not char valA as the OP has declared). The lcd.print()/Serial.print() method will show 123 on the screen.
(4) Initially, there was 4 characters (123Y) in the FIFO. We have taken out 3 characters (123), and now there is one character (Y) left in the FIFO.
(5) You have executed 'char valB = Serial.read();'
instruction (you should execute char/byte valB = Serial.read();
); this instruction will bring out character Y from the FIFO and will keep in the variable valB.
(6) After that, you have executed this instruction: lcd.write(valB); as a result, Y will appear on the monitor.
Hope that the issue is clarified to you.