Skip to content

Instantly share code, notes, and snippets.

@jonathanprozzi
Created March 25, 2017 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathanprozzi/d7e921101d884841746aeb33dbafb468 to your computer and use it in GitHub Desktop.
Save jonathanprozzi/d7e921101d884841746aeb33dbafb468 to your computer and use it in GitHub Desktop.
Basic analog input sketch for a photoresistor.
int ledPin=9; //Variable to store pin of LED
int potentPin=A0; //Variable to store pin of photoresistor
int potentValue=0; //Variable to store last known value of photoresistor
int brightnessValue=0; //Variable to store LED brightness
void setup() {
// put your setup code here, to run once:
pinMode(ledPin, OUTPUT); //Setup LED pin for output
}
void loop() {
// put your main code here, to run repeatedly:
potentValue=analogRead(potentPin); //Read the value of the photoresistor pin
brightnessValue=map(potentValue,0,1023,0,255); //Map the photoresistor value to a brightness
analogWrite(ledPin,brightnessValue); //Set the brightness of the ledPin
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment