-
Notifications
You must be signed in to change notification settings - Fork 0
/
moveTrackxx.ino
164 lines (155 loc) · 4.08 KB
/
moveTrackxx.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
int LSPEED = 5; //左轮转速
int RSPEED = 6; //右轮转速
int LDIR = 4; //左轮方向
int RDIR = 7; //右轮方向
char onspeed =100; //启动速度
int ontime=1000; //启动时间
bool bstop=false;
int grayscalePinL=A2;
int grayscalePinM=A3;
int grayscalePinR=A4;
char start_msg[15] = {
"Start loop "};
int adc_key_val[5] ={
30, 150, 360, 535, 760 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;
void stop(void){ //停止
analogWrite (LSPEED,0);
analogWrite (RSPEED,0);
}
void advance(char a,char b){ //前进
analogWrite (LSPEED,a); //PWM调速
digitalWrite(LDIR,HIGH);
analogWrite (RSPEED,b);
digitalWrite(RDIR,HIGH);
}
void back_off (char a,char b) { //后退
analogWrite (LSPEED,a);
digitalWrite(LDIR,LOW);
analogWrite (RSPEED,b);
digitalWrite(RDIR,LOW);
}
void turn_L (char a,char b) { //左转
analogWrite (LSPEED,a);
digitalWrite(LDIR,LOW);
analogWrite (RSPEED,b);
digitalWrite(RDIR,HIGH);
}
void turn_R (char a,char b) { //右转
analogWrite (LSPEED,a);
digitalWrite(LDIR,HIGH);
analogWrite (RSPEED,b);
digitalWrite(RDIR,LOW);
}
void setup(void) {
int i;
pinMode(LDIR, OUTPUT);
pinMode(RDIR, OUTPUT);
pinMode(grayscalePinL,INPUT);
pinMode(grayscalePinM,INPUT);
pinMode(grayscalePinR,INPUT);
Serial.begin(19200); //设置串口波特率
Serial.println("w,s,a,d");
}
void loop()
{
static char x=0;
char valL,valM,valR;
if(!bstop)
{
valL=digitalRead(grayscalePinL);
valM=digitalRead(grayscalePinM);
valR=digitalRead(grayscalePinR);
if((valL==0)&&valM&&valR)
{
//speed
turn_L(onspeed,onspeed); //检测到左边传感器遇到黑线,说明小车右偏了,立即往左转一点
delay(2);
while(1){
valM=digitalRead(grayscalePinM); //循环判断中间传感器度数,
if(valM==1){
turn_L(onspeed,onspeed); //如果num2==1说明还没有转到中间位置,继续左转
delay(2);
}
else
break; //检测到num2==0说明转过头了,跳出循环,检测三个传感器的状态再做出相应动作
} //后面道理相同
}
else if(valM&&valL&&(valR==0))
{
turn_R(onspeed,onspeed);
delay(2);
while(1){
valM=digitalRead(grayscalePinM);
if(valM==1){
turn_R(onspeed,onspeed);
delay(2);}
else
break;
}
}
else
{
advance(onspeed,onspeed);
delay(2);
}
}
else
stop();
adc_key_in = analogRead(7); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey) { // if keypress is detected
delay(50); // wait for debounce time
adc_key_in = analogRead(7); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey) {
oldkey = key;
carmove(key);
}
}
//digitalWrite(13, LOW);
}
// Convert ADC value to key number
int get_key(unsigned int input)
{
int k;
for (k = 0; k < NUM_KEYS; k++)
{
if (input < adc_key_val[k])
{
return k;
}
}
if (k >= NUM_KEYS)
k = -1; // No valid key pressed
return k;
}
void carmove(char val) {
/* if(val!=-1){
switch(val){
case 0://前进
advance (onspeed,onspeed); //PWM调速
break;
case 2://后退
back_off (onspeed,onspeed);
break;
case 1://左转
turn_L (onspeed,onspeed);
break;
case 3://右转
turn_R (onspeed,onspeed);
break;
}
delay(ontime);
stop();
}
else stop();
*/
if(val!=-1)
{
bstop=!bstop;
}
}