SHED-Article2SOFTWARE-Lighting Control
Jump to navigation
Jump to search
SHED-Article2SOFTWARE-Lighting Control
(Copy the text in the box below and Paste it into a blank Arduino IDE window)
/* SHED Magazine Arduino Sketch: Lighting Control - Reads voltage on Analog Pin 0 and displays value - Reads voltage on Analog Pin 1 and displays value - Compares values and turns on light if darker than setting - SEE the comments after "//" on each line below - CONNECTIONS: - Potentiometer from +5 to Ground, center to pin A0 - Photoresistor from +5V to pin A1, 10K from A1 to Gnd. - Relay may be connected to Pin 13 or other if changed. - V1.01 09/11/12 Questions: terry@yourduino.com */ /*-----( Import needed libraries )-----*/ //none /*-----( Declare Constants and Pin Numbers )-----*/ #define potPin A0 // In separate group of pins #define photoPin A1 // In separate group of pins #define ledPin 13 // The onboard LED /*-----( Declare objects )-----*/ //none /*-----( Declare Variables )-----*/ int potValue; // Hold the values read in int photoValue; boolean lightsOn; // Should Lights should be turned on? // Value can be "true" or "false" void setup() /****** SETUP: RUNS ONCE ******/ { pinMode(ledPin, OUTPUT); lightsOn = false; // Start with Lights off Serial.begin(9600); //Start sending to "Serial Monitor" Serial.println("SHED Magazine: Lighting Control."); }//--(end setup )--- void loop() /****** LOOP: RUNS CONSTANTLY ******/ { /*-----( Read Sensors )-------------------*/ potValue = analogRead(potPin); //Read value photoValue = analogRead(photoPin); //Read value Serial.print("POT VALUE = "); Serial.print(potValue,DEC); // Print value Serial.print(" PHOTORESISTOR VALUE = "); Serial.println(photoValue,DEC); // Print value /*-----( Make Decisions )-------------------*/ if (photoValue > potValue) { lightsOn = true; } else { lightsOn = false; } /*-----( Take Actions )-------------------*/ if (lightsOn = true) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } delay(1000); // Wait 1 second }//--(end main loop )--- /*-----( Declare User-written Functions )-----*/ //none //*********( THE END )***********