analogWrite() on Two LEDs

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;
}
}

You have probably not understood how analogWrite works..

analogWrite(yellowLED,1);  //apply 1v to LED

Do you think that analogWrite(pin, 255) outputs 255 volts from the pin?

The voltage is always "Aref" - usually 5V or less - the value given to analogWrite determins the widths of the PWM pulses.

Pulsing a LED with analogWrite(led, 1) is barely visible, try a higher value.

const int yellowLED = 10; //define yellowLED's pin#

Is pin 10 a PWM capable pin ?

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~)

        analogWrite(yellowLED,1);   //apply 1v to LED

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.

Danois90:
Do you think that analogWrite(pin, 255) outputs 255 volts from the pin?

mau_kama:
analogWrite(redLED,255); //apply 5v to LED

UKHeliBob:

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.
pwm328.png
Figure-1:

pwm328.png

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~)

Confusing! Perhaps, Aduino UNO.

Using Arduino Mega 2560, and pin 10 is PWM capable

Using Arduino Mega 2560,

There is some sort of bug with the current AVR boards core for the MEGA and you may be seeing it.

There are some work arounds proposed in the thread.