-
Notifications
You must be signed in to change notification settings - Fork 0
/
move.ino
166 lines (124 loc) · 6.15 KB
/
move.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/****************************************************************************************************************
* Program for a simple light Seeking Robot.
* created by: Anuj
* (c)copyright
*
* Robotic base should include differential drive.
*
* Physical setup:
* Two LDRs connected to ground and Analog pin 1(Right) and Analog pin 2(left)
* base of NPN Transistors on pins 11(right) and 12(left).
*
*
******************************************************************************************************************/
#include <Wire.h>
#include <BH1750.h>
#include <Servo.h>
BH1750 lightMeter;
int i = 0;
int flag; // 0 -> front , 1 -> left
// Create a servo object
Servo Servo1;
// Declare the Servo pin
int servoPin = 9;
const int RightMotorIN3 = 5; // This pin is used to enable or disable the Right motor. Connected to the base of an NPN transistor.
const int RightMotorIN4 = 4; // This pin is used to enable or disable the Right motor. Connected to the base of an NPN transistor.
const int LeftMotorIN1 = 7; // This pin is used to enable or disable the Left motor. Connected to the base of an NPN transistor.
const int LeftMotorIN2 = 6; // This pin is used to enable or disable the Left motor. Connected to the base of an NPN transistor.
const int FrontSensor = 11;
const int BackSensor = 8 ;
const int ENA = 10;
const int ENB = 3;
// Variable definitions
int degreeMax;
int maxLX;
int SensorDifference; // This value is used to determine the difference between the Left and Right
// the setup() method runs once when the program is run. When the
// Arduino is reset, the setup() will be executed once again.
void setup() {
analogWrite(ENA, 255);
analogWrite(ENB, 255);
pinMode(LeftMotorIN1, OUTPUT); // Defines this pin as an output. The Arduino will write values to this pin.
pinMode(LeftMotorIN2, OUTPUT); // Defines this pin as an output. The Arduino will write values to this pin.
pinMode(RightMotorIN3, OUTPUT); // Defines this pin as an output. The Arduino will write values to this pin.
pinMode(RightMotorIN4, OUTPUT); // Defines this pin as an output. The Arduino will write values to this pin.
//for bh1750
pinMode(FrontSensor, OUTPUT); // gy-front
pinMode(BackSensor, OUTPUT); // gy-back
// We need to attach the servo to the used pin number
Servo1.attach(servoPin);
pinMode(FrontSensor, INPUT); // Defines this pin as an input. The Arduino will read values from this pin.
pinMode(BackSensor, INPUT); // Defines this pin as an input. The Arduino will read values from this pin.
Serial.begin(9600); // Enables a serial connection through the Arduino to either USB or UART (pins 0&1). Note that the baud rate is set to 9600
Serial.println(" \nBeginning Light Seeking Behavior"); // Placed at the very end of void Setup() so that it is runs once, right before the void Loop()
Serial.println(F("BH1750 Test begin"));
// Initialize the I2C bus (BH1750 library doesn't do this automatically)
Wire.begin();
// On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3);
//Wire.begin(D4, D3);
//Uno, Ethernet A4 (SDA), A5 (SCL)
lightMeter.begin();
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop() {
maxLX = 0 ;
for(int j = 0 ; j < 10 ; j++)
{
Servo1.write(j*18);
delay(100);
digitalWrite(FrontSensor, LOW); // sets the digital_out_3 on -> front
digitalWrite(BackSensor, HIGH); // sets the digital_out_5 off -> back
uint16_t luxb = lightMeter.readLightLevel();
delay(200);
digitalWrite(FrontSensor, HIGH); // sets the digital_out_3 on -> front
digitalWrite(BackSensor, LOW); // sets the digital_out_5 off -> back
uint16_t luxf = lightMeter.readLightLevel();
Serial.print("Light: ");
Serial.print(luxf);
Serial.println(" lx");
Serial.print(luxb);
Serial.println(" lx");
Serial.println("now change...............");
if(luxb > maxLX)
{
maxLX = luxb;
degreeMax = j*18;
flag = 1;
}
if(luxf > maxLX)
{
maxLX = luxf;
degreeMax = j*18;
flag = 0;
}
delay(1000);
}
if ( flag == 1 )
{
degreeMax = degreeMax + 180;
}
// This section of the sketch is what actually interperets the data and then runs the motors accordingly.
if (degreeMax >= 90 && degreeMax <180 ) { // This is interpreted as if the Left sensor reads more light than the Right Sensor, Do this:
digitalWrite(RightMotorIN3, HIGH); // This is used to turn Left. Notice the
digitalWrite(RightMotorIN4, LOW);
digitalWrite(LeftMotorIN1, LOW); // opposite motor runs to turn Left.
digitalWrite(LeftMotorIN2, HIGH);
Serial.println("Left"); // This prints Left when the robot would actually turn Left.
}
if (degreeMax <90) { // This is interpreted as if the Left sensor reads less light than the Right Sensor, Do this:
digitalWrite(RightMotorIN3, LOW); // This is used to turn Right. Notice the
digitalWrite(RightMotorIN4, HIGH);
digitalWrite(LeftMotorIN1, HIGH); // opposite motor runs to turn Right.
digitalWrite(LeftMotorIN2, LOW);
Serial.println("Right"); // This prints Right when the robot would actually turn Right.
}
else if (degreeMax == 90 ) { // This is interpreted as if the difference between the two sensors is under 125 (Experiment to suit our sensors), Do this:
digitalWrite(RightMotorIN3, HIGH);
digitalWrite(RightMotorIN4, LOW); // This is used to go straight. Notice
digitalWrite(LeftMotorIN1, HIGH);
digitalWrite(LeftMotorIN2, LOW); // both motors are enabled to go forward.
Serial.println("Forward"); // This prints Forward when the robot would actually go forward.
}
Serial.print("\n");
}