I'm trying to do PWM using an RGB LED connected to digital pins (I cannot change the wiring). I'm basically trying to turn the LED on and off very quickly, slowly increasing the time in-between on and off stages with a for loop. Here's what I've got so far:
int redPin = 4;
int greenPin = 3;
int bluePin = 2;
void setup(){
Serial.begin(9600);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
for(float i = 0; i <= 10; i += .1) {
digitalWrite(bluePin, HIGH);
delayMicroseconds(i);
digitalWrite(bluePin, LOW);
delayMicroseconds(i);
}
}
Right now the LED is not changing in brightness. How might I get this PWM method to work?
Here's the arduino.cc example code I tried out originally:
int ledPin = 2; // LED connected to digital pin 2
void setup() {
pinMode(2, OUTPUT);
}
void loop() {
// fade in from min to max in increments of 5 points:
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(100);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(100);
}
}
The LED turns on for a couple seconds than off for about twice as long. Since I'm using digital pins I tried the same with digitalWrite to no avail. Any tips?