Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int ledPin = 3;
- const int button = 7;
- const int maxPWM = 255;
- const int minPWM = 0;
- int fadeDirection = 1;
- int buttonState = 0;
- int lastButtonState = 0;
- int brightness = 0;
- int counter = 1;
- byte fade = 5;
- int fadeInterval = 50;
- unsigned long lastMillis;
- unsigned long currentMillis;
- void setup() {
- pinMode(ledPin, OUTPUT);
- pinMode(button, INPUT);
- Serial.begin(9600);
- }
- void LightLoop(){
- currentMillis = millis();
- if (currentMillis - lastMillis >= fadeInterval){
- if (fadeDirection == 1) {
- brightness = brightness + fade;
- if (brightness >= maxPWM) {
- brightness = maxPWM;
- fadeDirection = 0;
- }
- }
- else {
- brightness = brightness - fade;
- if (brightness <= minPWM){
- brightness = minPWM;
- fadeDirection = 1;
- }
- }
- }
- analogWrite(ledPin, brightness);
- }
- void loop() {
- buttonState = digitalRead(button);
- delay(200);
- if (counter <= 1) {
- LightLoop();
- if (buttonState != lastButtonState){
- Serial.println("Push");
- fadeDirection = 0;
- counter ++;
- }
- }
- else {
- LightLoop();
- if (buttonState != lastButtonState){
- Serial.println("Push");
- fadeDirection = 1;
- counter = 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement