I am having trouble with what seems like a basic program and circuit. When I enter analogWrite(redLED,255); the code works fine. But any number other than 0 or 255, the code breaks down and the LEDs don't turn on at all.
const int redLED = 9; //define redLED's pin#
const int yellowLED = 10; //define yellowLED's pin#
int offTime = 500; //define how long the LED is off
int onTime = 500; //define how long the LED is on
int numBlinks = 3; //define number of blinks
void setup() {
Serial.begin(9600); //turn on serial port
pinMode(redLED, OUTPUT); //define redLED pin as an output
pinMode(yellowLED, OUTPUT); //define yellowLED pin as an output
}
void loop() {
int j = 1; //define variable "j"
while(j<=numBlinks){ //start while loop
Serial.print("You are on Blink #: ");
Serial.println(j);
analogWrite(redLED,255); //apply 5v to LED
delay(onTime); //keep LED on for onTime ms
analogWrite(redLED,0); //aply 0v to LED
delay(offTime); //keep LED off for offTime ms
j = j + 1;
}
int i = 1; //define variable "i"
while(i<=numBlinks){ //start while loop
Serial.print("You are on low level Blink #: ");
Serial.println(i);
analogWrite(yellowLED,1); //apply 1v to LED
delay(onTime);
analogWrite(yellowLED,0);
delay(offTime);
i = i + 1;
}
}
I initially had analogWrite(yellowLED,51); which is 1V, and I just forgot to change the comment.
Yes, pin 10 is pwm (on the Arduino board it is labelled as 10~)
an analog write level of "1" is not 1V. It's a duty cycle of 1/255. you alternate be 0 and 1 which isn't much of a difference.
on my board where setting the output pin LOW turns the LED on, an analogWrite value of 0 turns the LED on, but i needed to use a value of 220 to make it dim without turning it of. It was still fairly bright with lower values.
const int yellowLED = 10; //define yellowLED's pin#
Is pin 10 a PWM capable pin ?
The following diagram (Fig-1) depicts the correspondences among DPins and PWMs of UNO; where, DPin-10 definitely carries PWM signal in response to analogwWrite(10, arg2); command.
Figure-1:
The following diagram (Fig-1) depicts the correspondences among DPins and PWMs of UNO; where, DPin-10 definitely caccries PWM signal in response to analogwWrite() command.
Do we actually know which Arduino board the OP is using ?
UKHeliBob:
Do we actually know which Arduino board the OP is using ?
mau_kama:
I initially had analogWrite(yellowLED,51); which is 1V, and I just forgot to change the comment.
Yes, pin 10 is pwm (on the Arduino board it is labelled as 10~)