Lab 2 Part 2
This second part was using another type of analog switch to control leds. I used a photocell and when you put your hand over it switches between the lights. The hardest part was figuring out how to get the arduino to read the photocell. I referenced week 4’s lab. What I figured out was on the board first to run the positive —>photocell(+)—> photocell(-)—>Arduino analog in—>220Ohm resister—>Ground. The analog in reads the resistance off the photocell. Another issue I ran into was dealing with the photocell reading to make it smoother because of the fluctuations in light.
This is the code I used for this.
int potPin = 0; // analog pin that pot attaches too “blue wire”
int potValue = 0; //value initial
int led = 9; // displays what the pot is doing
int led2 = 10;
int x = 0;
int y = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
potValue = analogRead(potPin); //read pot value
x = potValue/4;// sets the value arrange between 0 and 255
if (x < 10){
// makes for smoother range control
x = 0;
}
x = map(x,0, 150, 0, 255);
y = -(x – 150);
if (y < 20){
// makes for smoother range control
y = 0;
}
y = map(y,0, 150, 0, 255);
analogWrite(led, x); //pwm led w/ pot value
analogWrite(led2, y); //pwm led w/ pot value
//Serial.println(x);//print value
Serial.println(y);//print value
delay(10);
}
Physcomp-Lab 2 part2 from Zeven Rodriguez on Vimeo.