Created
March 28, 2018 07:34
-
-
Save minerscale/80da47185d5df3bc0c6ea3c054f0af07 to your computer and use it in GitHub Desktop.
Arduino light sensor code.
This file contains hidden or 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
// The Threshold for turning on the light between 0 and 1024 | |
// 0 means the light is always off and 1024 means it's always on. | |
// 900 seems to be a good value. | |
#define THRESHOLD 900 | |
// Digital pins: | |
#define RED_PIN 9 | |
#define GREEN_PIN 10 | |
#define BLUE_PIN 11 | |
// Analogue pins: | |
#define LIGHT_SENSOR_PIN 0 | |
void setup(){ | |
// Set the red, green and blue pins to OUTPUT. | |
pinMode(RED_PIN, OUTPUT); | |
pinMode(GREEN_PIN, OUTPUT); | |
pinMode(BLUE_PIN, OUTPUT); | |
// Write the highest value to the pins of the light | |
// because the polarity is oddly inverted. | |
analogWrite(RED_PIN, 255); | |
analogWrite(GREEN_PIN, 255); | |
analogWrite(BLUE_PIN, 255); | |
} | |
int lightSensorValue = 0; | |
int isOn = 0; | |
void loop(){ | |
// Read the light sensor's values into a variable. | |
lightSensorValue = analogRead(LIGHT_SENSOR_PIN); | |
// Test if the light is dim enough and set isOn accordingly | |
isOn = 0; | |
if (lightSensorValue > THRESHOLD){ | |
isOn = 1; | |
}else{ | |
isOn = 0; | |
} | |
// Write all 3 pins to the same value | |
// Making it black or white. | |
digitalWrite(RED_PIN, isOn); | |
digitalWrite(GREEN_PIN, isOn); | |
digitalWrite(BLUE_PIN, isOn); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment