I've recently started to try things with the arduino UNO and was able to do most thigs reading the samples.
Now I'm trying to PWM a LED through the Serial connection.
That almost works but after receving the value it sets the output to a very low value.
I can barely see it shine.
Here is my Code:
const int ledPin = 9;
//--------------------------------------------------------------------------
void setup()
{
// initialize the serial communication:
Serial.begin(9600);
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);
analogWrite(ledPin, 0);
}
//--------------------------------------------------------------------------
void loop() {
byte brightness = 0;
// check if data has been sent from the computer:
if (Serial.available()) {
// only look for integer values
brightness = Serial.parseInt();
analogWrite(ledPin, brightness);
}
}
}
//--------------------------------------------------------------------------
what I send is a number from 0 to 255 and NL (/n)
As far as I understand parseInt it should only read the value and not the NewLine command.
If i send the value without new line it takes forever till I see a change on my output.
I hope somebody can help me and that I sent you all you need to help me.
What is sending the serial data? If it's the Serial Monitor, why not send something back to indicate what you got?
Have you verified that simply ramping up from 0 to 255 results in adequate/proper brightness? In other words, are you solving a hardware problem or a software problem?
As far as I understand parseInt it should only read the value and not the NewLine command.
If i send the value without new line it takes forever till I see a change on my output.
It's the new line value that tells parseInt() that the end of the numeric value has arrived. Without it, parseInt() waits for a while, expecting that more data is coming.
Ramping up and down works fine.
The brightness changes as expected.
But when I stop sending a value I expect from the code that the LED output will hold the last status until a new vale is written.
Veryfied by code.
As far as I remember from debugging I got a 3 back.
If I send a 3 it matches the brightness the LED will jump to when I stop sendig data.
With a PortMonitor I checked that there are no other things been sent after.
I'll add code to write the vales back and send a screenshot with the results.
If I send a 3 it matches the brightness the LED will jump to when I stop sendig data.
Since the brightness of the LED is a function of the value in the second argument, and those values range from 0 to 255, 3 IS pretty dim. Actually, I'm surprised you can see anything.
void loop() {
byte brightness = 0;
// check if data has been sent from the computer:
if (Serial.available()) {
// only look for integer values
brightness = Serial.parseInt();
analogWrite(ledPin, brightness);
Serial.println(brightness);
}
}
Testing with SerialMonitor:
setting: "No line ending"
result: LED stays at the last sent value.
I have a delay from sending till I see the output.
I only receive what I sent in SerialMonitor
setting: "Newline"
result: LED goes off after a while
Serial Monitor returns the value and after that also a 0
trying with my PC application I have the exact same result
when I stop sending data a second later I receive a 0
result: LED goes off after a while
Serial Monitor returns the value and after that also a 0
This sounds like the behavior I'd expect from a carriage return/line feed setting. Are you sure that you are sending only one character (carriage return OR line feed, not both)?
yes I'm sure I tried with SerialMonitor and my application.
I get the same result when I send with NewLine or Carriage Return or both I have the problem.
When I send it without a control I takes some time to react but the output stays with the last value.
I added 255 to the value I send and substract it in my Arduino Code.
Additional I check for return value 0 and do not send it.
if (Serial.available()) {
// only look for integer values
brightness = Serial.parseInt();
if (brightness != 0){
analogWrite(ledPin, brightness-255);
Serial.println(brightness);
}
}
The LED stays on but the values returned are not quite what I expect.
I always have an offset between what I send and what I receive.
I send 258 and get 2 back but I expect 3
I send 255 and get 255 back but I expect 0
I send 355 and get 99 back but I expect 100
I send 510 and get 254 bach but I expect 255
I send 256 and get nothing back
Any idea?
The reason to send that is cause I found in stream.cpp
// returns the first valid (long) integer value from the current position.
// initial characters that are not digits (or the minus sign) are skipped
// function is terminated by the first character that is not a digit.
long Stream::parseInt()
{
return parseInt(NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
}
// as above but a given skipChar is ignored
// this allows format characters (typically commas) in values to be ignored
long Stream::parseInt(char skipChar)
{
boolean isNegative = false;
long value = 0;
int c;
c = peekNextDigit();
// ignore non numeric leading characters
if(c < 0)
return 0; // zero returned if timeout
do{
if(c == skipChar)
; // ignore this charactor
else if(c == '-')
isNegative = true;
else if(c >= '0' && c <= '9') // is c a digit?
value = value * 10 + c - '0';
read(); // consume the character we got with peek
c = timedPeek();
}
while( (c >= '0' && c <= '9') || c == skipChar );
if(isNegative)
value = -value;
return value;
}
So if I have Serial.available I do parseInt but then I think I run into the timeout and get 0 back.
Shouldn't there be a -1 if there is a timeout or fault as return value?
Or ist integer unsigned?