Exercise 34B - Parking control
1. INTRODUCTION
This project could be combined with mbot line follower. If you want to combine with it you have to add 3 seconds wait on the obstacle stopping condition when distance is less than 10 cm.
You can do only the connections and programming, but is interesting to build a structure to visualize better how the system works. You can view a example of the structure on the introduction video of this project and in these images.
![]()
|
![]()
|
![]()
|
![]()
|
![]()
|
|
Materials:
· Arduino UNO board
· Protoboard
· 2x LDR
· 2x 10k ohms Resistor.
· Some jumpers
· Servo motor
· Material to build the structure
2. HOW TO CONNECT?
This project join LDR light sensing with servo motor actuating. To connect it you have to connect LDR like a standard sensor (see exercise CONNECTING A LDR) on pins A0 and A1. On the other hand, you have to connect servo to pin 9 .
EXERCISE_A |
3. PROGRAMMING
To use this program you need to put LDR sensors on two sides. When the first LDR will not have light the servo rotates 90 degrees slowly, and then when you cover the second LDR sensor the servo returns to the initial position. If you add, a wall to the sensor a barrier effect will be created.
First, declare variables. Do not forget to include servo library!
#include <Servo.h>
Servo myservo;
int pos = 1;
int LDR1 = A0;
int LDR2 = A1;
int value1;
int value2;
On setup LDR pins are set as inputs.
void setup() {
Serial.begin(9600);
pinMode(LDR1, INPUT);
pinMode(LDR2, INPUT);
myservo.attach(9);
myservo.write(1);
}
On loop, LDR sensor value are read and are stored on variables value1 and value2. On Serial monitor, value1 and value2 are printed, you can use this information to calibrate the code in function of the intensity of the light that you have in your environment.
void loop() {
value1 = analogRead(LDR1);
value2 = analogRead(LDR2);
Serial.print(value1);
Serial.print("|");
Serial.println(value2);
The two conditions are very similar. The condition is only accomplished if the sensor, which you cover, reaches the minimum limit, the servo is into the final position and the value of the other sensor reaches the upper limit.
if (value1 < 780 && pos<=5 && value2 >900) {
for (pos = 1; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
if (value2 < 820 && pos>=85 && value1 >870) {
for (pos = 90; pos >= 1; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
}