I want to control a leds intensity with analogWrite(10, variable) over serial. Here's my code (Slightly modified from the "Dimmer" exemple):
const int ledPin = 10;
byte brightness; //Is it ok if I use int instead of byte?
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
if (Serial.available()) {
brightness = Serial.read();
analogWrite(ledPin, 0);
analogWrite(ledPin, brightness);
Serial.println(brightness); // This is for testing
}
}
The problem is that the led as a constant brightness, even if I send "0" over serial.
Thanks in advance !
P.S. I'm not using any resistor. I've tried to make it dim by itself (see "Fading" exemple) and it worked perfectly. And yes i'm using a PWM pin. I'm using an Arduino Duemilanove
well... what I would do would to be start over. Use the 'Fading' demo you used, make sure that works, and then modify it until you get what you want. This assures you are using the correct pin, your code is good, and you haven't missed anything. Sorry I couldn't be more help!
I don't think your if statement is using the Serial.avalible command correctly. See the examble in the below reference if (Serial.available() > 0) { http://www.arduino.cc/en/Tutorial/Foundations
An LED connected to pin 9. use appropriate resistor as needed. For most common LEDs, you can usually do without the resistor, as the current output of the digital I/O pins is limited.
its limited by the fact that if you get near or over 40ma its going to die, there is no protection on any pin of the arduino, where ever you got that quote from is wrong
I know 0=48 so x-48 (x being an ASCII value) = Decimal value. That's obvious
its limited by the fact that if you get near or over 40ma its going to die, there is no protection on any pin of the arduino, where ever you got that quote from is wrong
I know 0=48 so x-48 (x being an ASCII value) = Decimal value.
Today, you remember that '0' = 48. But, 2 months from now, will you remember why toy are subtracting 48 from the value? Subtracting '0' results in the same value being subtracted, but, makes it much more obvious what is being subtracted and makes it more likely that you will remember why.
Today, you remember that '0' = 48. But, 2 months from now, will you remember why toy are subtracting 48 from the value? Subtracting '0' results in the same value being subtracted, but, makes it much more obvious what is being subtracted and makes it more likely that you will remember why.
True, I agree. But I knew about ASCII before, I just didn't though about it. Now I know that serial sends ASCII value, that's all I needed. Thanks a lot everybody. It's been really helpful.