Last active
September 29, 2015 20:39
-
-
Save jessherzog/2097a1b8d60c3eae58c7 to your computer and use it in GitHub Desktop.
potentiometer / fade / blink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//create variables for pins | |
const int blueLed = 11; | |
const int whiteLed = 12; | |
const int potPin = A0; | |
// Time code | |
unsigned long previousMillis = 0; | |
const long interval = 10; | |
// LED fading variables | |
boolean fadeUp = true; | |
int fadeLevel = 0; | |
//LED blinking variables | |
int whiteLedState = LOW; | |
//analog sensor variables | |
int potVar = 0; | |
//state machine variables | |
int stateVar; | |
void setup() { | |
// set up pins | |
pinMode(potPin, INPUT); | |
pinMode(blueLed, OUTPUT); | |
pinMode(whiteLed, OUTPUT); | |
Serial.begin(9600); | |
} | |
void loop() { | |
unsigned long currentMillis = millis(); // What time is it? | |
potVar = analogRead(potPin); // read potentiometer | |
if (fadeLevel == 255) { // when at max potent lvl | |
whiteLedState = HIGH; // blink | |
delay(10); | |
whiteLedState = HIGH; // three | |
delay(10); | |
whiteLedState = HIGH; // times | |
} else { | |
whiteLedState = LOW; // or else off | |
} | |
potVar = analogRead(fadeLevel); | |
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(30); | |
} | |
// 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); | |
if (currentMillis - previousMillis >= interval) { | |
previousMillis = currentMillis; | |
if (fadeUp == true) { | |
if (fadeLevel != 255) fadeLevel++; | |
} else { | |
if (fadeLevel != 0) fadeLevel--; | |
} | |
} | |
if ( potVar == 0) { | |
fadeUp = true; | |
} else { | |
fadeUp = false; | |
} | |
digitalWrite(whiteLed, whiteLedState); | |
analogWrite(blueLed, fadeValue); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment