-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.cpp
97 lines (65 loc) · 1.88 KB
/
code.cpp
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
int vSpeed = 255; // MAX 255
int turn_speed = 255; // MAX 255
int turn_delay = 5;
//L293 Connection
const int motorA1 = 8;
const int motorA2 = 10;
const int motorAspeed = 9;
const int motorB1 = 12;
const int motorB2 = 13;
const int motorBspeed = 11;
//Sensor Connection
const int left_sensor_pin =A0;
const int right_sensor_pin =A1;
int left_sensor_state;
int right_sensor_state;
void setup() {
pinMode(motorA1, OUTPUT);
pinMode(motorA2, OUTPUT);
pinMode(motorB1, OUTPUT);
pinMode(motorB2, OUTPUT);
Serial.begin(9600);
delay(3000);
}
void loop() {
left_sensor_state = analogRead(left_sensor_pin);
right_sensor_state = analogRead(right_sensor_pin);
if(right_sensor_state > 500 && left_sensor_state < 500)
{
Serial.println("turning right");
digitalWrite (motorA1,LOW);
digitalWrite(motorA2,HIGH);
digitalWrite (motorB1,LOW);
digitalWrite(motorB2,HIGH);
analogWrite (motorAspeed, vSpeed);
analogWrite (motorBspeed, turn_speed);
}
if(right_sensor_state < 500 && left_sensor_state > 500)
{
Serial.println("turning left");
digitalWrite (motorA1,HIGH);
digitalWrite(motorA2,LOW);
digitalWrite (motorB1,HIGH);
digitalWrite(motorB2,LOW);
analogWrite (motorAspeed, turn_speed);
analogWrite (motorBspeed, vSpeed);
delay(turn_delay);
}
if(right_sensor_state > 500 && left_sensor_state > 500)
{
Serial.println("going forward");
digitalWrite (motorA2,LOW);
digitalWrite(motorA1,HIGH);
digitalWrite (motorB2,HIGH);
digitalWrite(motorB1,LOW);
analogWrite (motorAspeed, vSpeed);
analogWrite (motorBspeed, vSpeed);
delay(turn_delay);
}
if(right_sensor_state < 500 && left_sensor_state < 500)
{
Serial.println("stop");
analogWrite (motorAspeed, 0);
analogWrite (motorBspeed, 0);
}
}