在arduio软件上编辑迎宾机器人的程序,出现错误,
代码:
#include
#include
Servo myServo;
myServo.attach(11);
SoftwareSerial Serial1(10,4);
int b = 0;
void mp3_volume(uint8_t level){
uint8_t sum = level + 0xBE;
uint8_t volume [5] = {0xAA,0x13,0x01,level,sum};
Serial1.write(volume,5);
}
void mp3_play_chapter(uint16_t chapter){
uint8_t chapter_h = chapter >> 8;
uint8_t chapter_l = chapter;
uint8_t sum = 0xB3 + chapter_h + chapter_l;
uint8_t play_chapter[6] = {0xAA,0x07,0x02,chapter_h,chapter_l,sum};
Serial1.write(play_chapter,6);
}
void setup(){
Serial1.begin(9600);
Serial.begin(9600);
pinMode(2,OUTPUT);
pinMode(3,INPUT);
servo.write(90);
delay(100);
}
void loop(){
chaoshengbuo();
Serial.println(a);
if(a >= 10 && a < 15){
if(b > a){
myServo.write(180);
mp3_volume(30);
mp3_play_chapter(26);
delay(2000);
}
}
else if(a >= 15 && a < 20){
if(b < a){
myServo.write(180);
mp3_volume(30);
mp3_play_chapter(50);
mp3_play_chapter(25);
delay(1500);
mp3_play_chapter(59);
}
}
}
void chaoshengbuo(){
digitalWrite(2,LOW);
delayMicroseconds(2);
digitalWrite(2,HIGH);
delayMicroseconds(10);
digitalWrite(2,LOW);
int a = pulseIn(3,HIGH)/58.0;
}
报错:
Arduino:1.8.5 (Windows 10), 开发板:"Arduino/Genuino Uno"
sketch_sep24a:4: error: 'myServo' does not name a type
myServo.attach(11);
^
C:\Users\dell\Desktop\sketch_sep24a\sketch_sep24a.ino: In function 'void setup()':
sketch_sep24a:24: error: 'servo' was not declared in this scope
servo.write(90);
^
C:\Users\dell\Desktop\sketch_sep24a\sketch_sep24a.ino: In function 'void loop()':
sketch_sep24a:29: error: 'a' was not declared in this scope
Serial.println(a);
^
exit status 1
'myServo' does not name a type
在文件 -> 首选项开启
“编译过程中显示详细输出”选项
这份报告会包含更多信息。
第4行,是运行语句,不能写在函数外。
24行,servo没有定义过,你是想用你你前面定义的那个myServo吧?