-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfsmash.ino
112 lines (92 loc) · 1.68 KB
/
dfsmash.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
/*
3,4,5
7,8,9
*/
int ma1 = 3;
int ma2 = 4;
int mapwm = 5;
int mb1 = 8;
int mb2 = 7;
int mbpwm = 9;
int led1 = A0;
int led2 = A1;
unsigned long time;
const int high = 255;
const int normal = 130;
int mspeed;
unsigned long tnow;
void setup() {
// put your setup code here, to run once:
pinMode(ma1, OUTPUT);
pinMode(ma2, OUTPUT);
pinMode(mapwm, OUTPUT);
pinMode(mb1, OUTPUT);
pinMode(mb2, OUTPUT);
pinMode(mbpwm, OUTPUT);
pinMode(led1, INPUT);
pinMode(led2, INPUT);
Serial.begin(9600);
time = millis();
mspeed = normal;
}
void loop() {
tnow = millis();
if (tnow - time > 6000)
{
mspeed = normal;
time = tnow;
}
else if (tnow - time > 3000)
{
mspeed = high;
}
if (sensors(led1) || sensors(led2)) {
turn();
}
else {
forward();
}
}
void forward() {
//Serial.println("forward");
digitalWrite(ma1, HIGH);
digitalWrite(ma2, LOW);
digitalWrite(mb1, HIGH);
digitalWrite(mb2, LOW);
analogWrite(mapwm, mspeed - 20);
analogWrite(mbpwm, mspeed);
}
boolean sensors(int sensor) {
int value = analogRead(sensor);
Serial.println(value);
if (value > 800) {
return true;
}
else {
return false;
}
}
void reverse() {
//Serial.println("forward");
time = millis();
mspeed = normal;
digitalWrite(ma1, LOW);
digitalWrite(ma2, HIGH);
digitalWrite(mb1, LOW);
digitalWrite(mb2, HIGH);
analogWrite(mapwm, 120);
analogWrite(mbpwm, 120);
}
void turn() {
reverse();
delay(250);
//Serial.println("turning");
digitalWrite(ma1, LOW);
digitalWrite(ma2, HIGH);
digitalWrite(mb1, HIGH);
digitalWrite(mb2, LOW);
analogWrite(mapwm, 120);
analogWrite(mbpwm, 120);
delay(500);
forward();
}