Friday 29 October 2010


control 4 leds, 3 are fading on PWM pins and one is turned on and off from a button, all connections have small resistors:


int brightness = 0; // how bright the LED isint fadeAmount = 5; // how many points to fade the LED byconst int buttonPin = 7;int buttonState = 0; const int ledPin = 8; void setup() { // declare pin 9 to be an output: pinMode(9, OUTPUT); pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT); }
void loop() {
// set the brightness of pin 9:pwm
analogWrite(9, brightness); analogWrite(10, brightness); analogWrite(11, brightness); buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { digitalWrite(ledPin, HIGH); delay(1000); } // change the brightness for next time through the loop: brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade: if (brightness == 0 brightness == 255) { fadeAmount = -fadeAmount ; } else { // turn LED off: digitalWrite(ledPin, LOW); } // wait for 30 milliseconds to see the dimming effect delay(30); }

No comments:

Post a Comment