
Tog till och med en video

Kod: Markera allt
boolean dimUp = true;
int dimLevel = 0;
int maxDim = 50;
int minDim = 0;
const int dimDelay = 50;
const int inputPin = 2;
const int pwmPin = 1;
void setup() {
pinMode(pwmPin, OUTPUT);
pinMode(inputPin, INPUT);
}
void loop() {
if(digitalRead(inputPin) == HIGH){
delay(500);
if(digitalRead(inputPin) == HIGH){
while(digitalRead(inputPin) == HIGH){
analogWrite(pwmPin, dimLevel);
if(dimUp == true){
dimLevel = dimLevel +1;
}
else{
dimLevel = dimLevel -1;
}
constrain(dimLevel, minDim, maxDim);
delay(dimDelay);
}
}
else{
if(dimUp == true){
analogWrite(pwmPin, maxDim);
}
else{
analogWrite(pwmPin, minDim);
}
}
dimUp = !dimUp;
delay(50);
}
}
Kod: Markera allt
/*
Fade + Button Control
Add 10k Resistor from pin 2 to Ground
Add Button from pin 2 to +5v
Add Resistor from pin 1 to LED to Ground
*/
int led = 1; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 1; // how many points to fade the LED by
int buttonPin = 2; // the pin that button is attached to
int buttonState = 0; // shows button state
// the setup routine runs once when you press reset:
void setup() {
pinMode(led, OUTPUT);
pinMode(buttonPin, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
// determines if button is High or Low
//Now the Fading Code took from Fade Example
analogWrite(led, brightness);
// will change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// will reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 7 milliseconds to see the dimming effect
delay(7);
}
}