Design
Algorithm
Use the following code to set up the light sensor:
from gpiozero import LightSensor, Buzzer ldr = LightSensor(4) # alter if using a different pin while True: print(ldr.value)
Run this code, then cover the LDR with your hand and watch the value change. Try shining a strong light onto the LDR.
Raspberry/Python Code
from gpiozero import LightSensor, Buzzer
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(23,GPIO.OUT)
GPIO.setup(24,GPIO.OUT)
GPIO.setup(25,GPIO.OUT)
GPIO.setup(8,GPIO.OUT)
ldr = LightSensor(4)
ldr2 = LightSensor(17)
ldr3 = LightSensor(27)
ldr4 = LightSensor(22)
while True:
bir=ldr.value+ldr2.value
iki=ldr3.value+ldr4.value
uc=ldr.value+ldr3.value
dort=ldr2.value+ldr4.value
print("ldr= %s" %ldr.value)
print("ldr2= %s" %ldr2.value)
print("ldr3= %s" %ldr3.value)
print("ldr4= %s" %ldr4.value)
print("bir= %s" %bir)
print("iki= %s" %iki)
print("uc= %s" %uc)
print("dort= %s" %dort)
if bir>iki:
GPIO.output(23,GPIO.HIGH)
GPIO.output(25,GPIO.LOW)
time.sleep(1)
elif iki>bir:
GPIO.output(25,GPIO.HIGH)
GPIO.output(23,GPIO.LOW)
time.sleep(1)
if uc>dort:
GPIO.output(24,GPIO.HIGH)
GPIO.output(8,GPIO.LOW)
time.sleep(1)
elif dort>uc:
GPIO.output(8,GPIO.HIGH)
GPIO.output(24,GPIO.LOW)
time.sleep(1)
Arduino Code
#include <Servo.h>
Servo servo1;
Servo servo2;
int servo_position1 =0;
int servo_position2 =0;
int in_rasp1 =3;
int in_rasp2 =4;
int in_rasp3 =5;
int in_rasp4 =6;
int read1=0;
int read2=0;
int read3=0;
int read4=0;
void setup()
{
servo1.attach(9);
servo1.write(90);
servo2.attach(10);
servo2.write(90);
pinMode(in_rasp1, INPUT);
pinMode(in_rasp2, INPUT);
pinMode(in_rasp3, INPUT);
pinMode(in_rasp4, INPUT);
}
void loop()
{
read1 =digitalRead(in_rasp1)
read2 =digitalRead(in_rasp2)
read3 =digitalRead(in_rasp3)
read4 =digitalRead(in_rasp4)
if (read1==HIGH)
{
for (servo_position =0 ; servo_positiion<=90; servo_position+=1)
{
servo1.write(servo_position);
delay(10);
}
}
else (read2==HIGH)
{
for (servo_position =90 ; servo_positiion<=0; servo_position-=1)
{
servo1.write(servo_position);
delay(10);
}
}
if (read3==HIGH)
{
for (servo_position =0 ; servo_positiion<=90; servo_position+=1)
{
servo2.write(servo_position);
delay(10);
}
}
else (read4==HIGH)
{
for (servo_position =90 ; servo_positiion<=0; servo_position-=1)
{
servo2.write(servo_position);
delay(10);
}
}