Skip to content

Instantly share code, notes, and snippets.

@eroeber
Created October 2, 2018 02:19
Show Gist options
  • Save eroeber/7a325c9568124f10b3d39f84fd68a7b9 to your computer and use it in GitHub Desktop.
Save eroeber/7a325c9568124f10b3d39f84fd68a7b9 to your computer and use it in GitHub Desktop.
// object lab 3 part 1
int pot = 0; // stores potentiometer value reading
int photoValue = 0; // stores light sensor value
int potPin = A0; // potentiometer analog input
int photoPin = A1; // light sensor analog input
int yellowLED = 9; // pwm pin
int yellow2 = 6; // pwm pin
int brightness = 0; // value from potentiometer to hold LED brightness
int bright2 = 0; // value from light sensor to hold LED 2 brightness
void setup() {
// set up serial comm
Serial.begin(9600);
pinMode(yellowLED, OUTPUT);
pinMode(yellow2, OUTPUT);
}
void loop() {
// read pot and light sensors
pot = analogRead(potPin); // reads potentiometer
photoValue = analogRead(photoPin); // reads light sensor
// change LED brightness based on potentiometer value
// write to LED
// use map() to convert 0 - 1023 to 0 - 255
// map(valueToMap, fromLow, fromHigh, toLow, toHigh)
brightness = map(pot, 0, 1023, 0, 255);
analogWrite(yellowLED, brightness);
analogWrite(yellow2, brightness);
//change LED2 brightness based on light sensor value
if(photoValue < 600){
bright2 = map(photoValue, 43, 796, 0, 255);
analogWrite(yellow2, bright2);
}
// Serial.print("potentiometer value: ");
// Serial.print(pot);
// Serial.print("lightVal: ");
// Serial.println(photoValue);
// Serial.print(" LED brightness: ");
// Serial.println(brightness);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment