Last active
September 29, 2015 18:54
-
-
Save mbrav/3bcd82e50756ad446af6 to your computer and use it in GitHub Desktop.
An Arduino program that allows the user to fade the led either by using a potentiometer or a photoresistor. To switch between the two modes a push button must be pressed. The sketch also goes through a completely automated calibration process in the setup() that sets the sensor values automatically.
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
// Michael Braverman | |
// Lab Class | |
// 29 September 2015 | |
// Pins | |
const int ledPin = 11; | |
const int buttonPin = 12; | |
const int lightPin = A0; | |
const int potPin = A1; | |
// States and numbers of the sensors and buttons | |
int lightVar; | |
int potVar; | |
boolean buttonState; | |
boolean switchState; | |
boolean pressed = false; | |
// Variables for the photoresistor | |
int light = 9999; | |
int dark; | |
// Variables for the potentiometer | |
int highPotValue = 9999; | |
int lowPotValue; | |
// Variables for fading | |
boolean fadeUp = true; | |
int fadeLevel = 0; | |
// Timer for the setup | |
int setupTimer = 0; | |
// Option for testing | |
boolean TEST = false; | |
void setup() { | |
pinMode(ledPin, OUTPUT); | |
pinMode(13, OUTPUT); | |
pinMode(lightPin, INPUT); | |
pinMode(potPin, INPUT); | |
pinMode(buttonPin, INPUT_PULLUP); | |
Serial.begin(115200); | |
Serial.println("-------------------------------------------------"); | |
Serial.println("Wave your hand over the sensor to calibrate"); | |
// Calibrate the photoresistor | |
while (setupTimer < 20000) { | |
lightVar = analogRead(lightPin); | |
if (light > lightVar) { | |
light = lightVar; | |
} | |
if (dark < lightVar) { | |
dark = lightVar; | |
} | |
// Make the LED Flash | |
if(setupTimer % 1000 == 0) { | |
switchState = !switchState; | |
} | |
digitalWrite(13, switchState); | |
setupTimer++; | |
} | |
Serial.println("LIGHT SENSOR CALIBRATED!"); | |
Serial.print("Light = "); | |
Serial.println(light); | |
Serial.print("Dark = "); | |
Serial.println(dark); | |
Serial.println("Now rotate the pot from left to right to calibrate"); | |
// Calibrate the potentiometer | |
setupTimer = 0; | |
while (setupTimer < 30000) { | |
potVar = analogRead(potPin); | |
if (highPotValue > potVar) { | |
highPotValue = potVar; | |
} | |
if (lowPotValue < potVar) { | |
lowPotValue = potVar; | |
} | |
// Make the LED Flash a bit faster this time | |
if(setupTimer % 500 == 0) { | |
switchState = !switchState; | |
} | |
digitalWrite(13, switchState); | |
setupTimer++; | |
} | |
Serial.println("POTENTIOMETER CALIBRATED!"); | |
Serial.print("Light = "); | |
Serial.println(lowPotValue); | |
Serial.print("Dark = "); | |
Serial.println(highPotValue); | |
Serial.println("STARTING THE PROGRAM!"); | |
delay(500); | |
} | |
void loop() { | |
lightVar = analogRead(lightPin); | |
potVar = analogRead(potPin); | |
buttonState = digitalRead(buttonPin); | |
// Button code | |
if (buttonState == HIGH && pressed == false ) { | |
if (switchState == LOW) { | |
switchState = HIGH; | |
} | |
else { | |
switchState = LOW; | |
} | |
pressed = true; | |
delay(20); | |
} | |
if (buttonState == LOW && pressed == true ) { | |
pressed = false; | |
delay(20); | |
} | |
if (switchState) { | |
// Trigger if the light sensor senses a value higher than | |
// half distance between the darkest and the ligthest value | |
if (lightVar <= light + (dark - light)/2) { | |
fadeUp = true; | |
} else { | |
fadeUp = false; | |
} | |
} else { | |
// Trigger if the potentiometer senses a value higher than | |
// half distance between the lowPotValue and the highPotValue | |
if (potVar <= lowPotValue + (highPotValue - lowPotValue)/2) { | |
fadeUp = true; | |
} else { | |
fadeUp = false; | |
} | |
} | |
// Fading code | |
if (fadeUp == true) { | |
if (fadeLevel != 255) { | |
fadeLevel++; | |
} | |
} else { | |
if (fadeLevel != 0) { | |
fadeLevel--; | |
} | |
} | |
// Set the outputs to their according values | |
analogWrite(ledPin, fadeLevel); | |
digitalWrite(13, switchState); | |
// If not testing, still allow some delay to occur | |
if (TEST) { | |
Serial.print("Light:"); | |
Serial.print(lightVar); | |
Serial.print("\tPot: "); | |
Serial.print(potVar); | |
Serial.print("\tButton: "); | |
Serial.print(buttonState); | |
Serial.print("\tFade State:"); | |
Serial.print(fadeUp); | |
Serial.print("\tTigger Brightness:"); | |
Serial.print(light + (dark - light)/2); | |
Serial.println("\t"); | |
} else { | |
delay(5); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment