-
Notifications
You must be signed in to change notification settings - Fork 0
/
move-part.ino
61 lines (56 loc) · 1.54 KB
/
move-part.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
int LSPEED = 5; //左轮转速
int RSPEED = 6; //右轮转速
int LDIR = 4; //左轮方向
int RDIR = 7; //右轮方向
char onspeed =100; //启动速度
int ontime=1000; //启动时间
void stop(void){ //停止
analogWrite (LSPEED,0);
analogWrite (RSPEED,0);
}
void advance(char a,char b){ //前进
//add code here
}
void back_off (char a,char b) { //后退
//add code here
}
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) { //右转
//add code here
}
void setup(void) {
int i;
pinMode(LDIR, OUTPUT);
pinMode(RDIR, OUTPUT);
Serial.begin(19200); //设置串口波特率
Serial.println("w,s,a,d");
}
void loop(void) {
if(Serial.available()>0){
char val = Serial.read();
if(val!=-1){
switch(val){
case 'w'://前进
advance (onspeed,onspeed); //PWM调速
break;
case 's'://后退
back_off (onspeed,onspeed);
break;
case 'a'://左转
turn_L (onspeed,onspeed);
break;
case 'd'://右转
turn_R (onspeed,onspeed);
break;
}
delay(ontime);
stop();
}
else stop();
}
}