Using Arduino The purpose of this project is to use two light sensors (photoresi
ID: 3349314 • Letter: U
Question
Using Arduino
The purpose of this project is to use two light sensors (photoresistors) to measure light intensity in two opposite positions (0 & 180°) and indicate the position with a servomotor. The materials that will be used are: two light sensors, one Arduino micro-controller, two LEDs, and one servo motor. Each LED will indicate when a photoresistor is illuminated (positioned on opposite sides of the servomotor), while the servomotor moves to the illuminated side. In addition, when the intensity of the two photoresistors is almost the same, the servo motor must be placed in a central position (90°), and both LEDs must be turned off.Explanation / Answer
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
int ldr1=A0; //Photoresistor1 at analog 0 pin
int ldr2=A1;
int led1=4; //led1 at pin 4
int led2=5; //led2 at pin 5
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(led1,OUTPUT);
}
void loop() {
if(analogRead(ldr1)>>700)
{
myservo.write(0); // tell servo to go to position 0 degree
delay(15); // waits 15ms for the servo to reach the position
digitalWrite(led1,HIGH);
digitalWrite(led2,LOW); //turn ON led1
}
if(analogRead(ldr2)>>700)
{
myservo.write(180); // tell servo to go to position 180 degree
delay(15); // waits 15ms for the servo to reach the position
digitalWrite(led2,HIGH); //turn ON led2
digitalWrite(led1,LOW);
}
else if (analogRead(ldr1)<<600 && analogRead(ldr2)<<600)
{
myservo.write(90); // tell servo to go to position 90 degree
delay(15); // waits 15ms for the servo to reach the position
digitalWrite(led1,LOW);
digitalWrite(led2,LOW); //turn off led
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.