Hello, I am pretty much new with arduino and i am trying to make a very simple project which consists of making a led shine with the intensity that we tell it on the serial port of the computer...the code that i have written is the following:
const int pin=6;
int value=0;
void setup(){
pinMode(pin,OUTPUT);
Serial.begin(9600);
}
void loop(){
if(Serial.available()){
value = Serial.read(); //here we send the pwm value
}
Serial.println(value);
analogWrite(pin, value); //here we should update the pwm value for the led
}
at first it is starts with a value of cero, but then it doesn't matter what value i send, the println just shows 10....do you have any idea what i am doing wrong?
When something is available, you read it. So far so good.
But then you print the value and use analogWrite. You do that when something was available or not.
When you type something in the serial monitor, a CarriageReturn or LineFeed might be added. Guess what ? The LineFeed has code 10.
First of all, thank you for you answers. But i still have some problems...
I changed the code in the part of the if-condition and now it looks like this:
if(Serial.available()){
value = Serial.read(); //here we send the pwm value
Serial.println(value);
analogWrite(pin, value); //here we should update the pwm value for the led
}
but now when i send a value of cero, it prints:
48
13
10
if i send a value of ten, it prints:
49
48
13
10
if i send a value of 37, it prints:
51
55
13
10
which explains why i was getting the 10 all the time before..
Your problem is the selection of Serial.read() . The function reads everything across the monitor and return all bytes of information for each character including line feeds and carriage returns.
The correct function is value = Serial.parseInt() (Serial.parseInt() - Arduino Reference). This will take care of rading all interger digits and return when encounter anything that is not an interger. For example: typing 10,34,54 in the monitor will return just 10 becuase the Serial.parseInt will return when encountered the comma.
if(Serial.available()){
value = Serial.parseInt(); //here we send the pwm value
Serial.println(value);
analogWrite(pin, value); //here we should update the pwm value for the led
}
}
well i still have one thing...when using this Serial.parseInt() function, it gives a zero if no valid integer is found within one second (it is how it is defined...) so the led has the right value just for one second, then it turns off...
I don't understand it because it is inside the if-condition so if there is no more serial data available, it should not do go through serial.parseInt() for more than one second...
You need to write your code so it saves the value it receives from the Serial port and uses the saved values in analogWrite().
That way analogWrite() has a value even if nothing is received.
And analogWrite() should not be part of the if (Serial.available() test
Another approach is only to call analogWrite() if the value has changed.
I presume there will be some situations when you want to send a value of zero to stop analogWrite() completely. That could be confused with a 0 returned by Serial.parseint() when it times out. You will need a strategy for dealing with that.
My inclination would be to save the data as it arrives and parse it afterwards using atoi().