一、首先看一下我们最新的主控板。
如果想详细看的话可以去我们的网站上看看
http://edu.kenrobot.com/
二、原始有点问题的代码
[mw_shl_code=c,true]//管脚定义:
//利用rosys控制板设计一个机器人,
//预期功能:眨眼(两个LED模块)、摇头(舵机模块)、头顶的风扇(直流电机)
#include <Servo.h>
#include "RoBuzzer.h" // 该文件里已经包含了Servo.h
const int leftEye = 2; //左眼
const int soundPin = A0; //声音传感器
const int rightEye = 8; //右眼
const int servoPin = 9; //舵机
const int LED = 10; // 另外一个小灯,姑且叫做胸灯吧
const int motor = 11; // 直流电机
int pos = 0;
int value; //声音
int brightness = 0; // LED的亮度
int fadeAmount = 5; // 每次的增加值
void Blink(); //声明机器人眨眼函数
void ShakeHead();//声明机器人摇头函数
void rotatingFan();//声明机器人头顶风扇旋转函数
Servo myservo; //初始化一个舵机类。
RoBuzzer buzzer(3); //蜂鸣器模块接口
void setup()
{
//myservo.write(102); // 将机器人头部转正
pinMode(motor, OUTPUT); //设置接口为输出接口
pinMode(soundPin, INPUT); //声音输出口
pinMode(LED,OUTPUT);
//myservo.attach(servoPin); //设置舵机连接的引脚
Serial.begin(9600); //设置串口波特率为9600
}
void loop()
{
int value = analogRead(soundPin); //设置变量,读取数据
Serial.print("value:");
Serial.println(brightness);
if (value > 500)
{
digitalWrite(LED, HIGH); // 打开胸灯
buzzer.tone(NOTE_G5, 150);
buzzer.tone(NOTE_G4, 150);
buzzer.tone(NOTE_G6, 150);
buzzer.tone(NOTE_G5, 150);
buzzer.tone(NOTE_G5, 150);
buzzer.tone(NOTE_G4, 150);
buzzer.tone(NOTE_G6, 150);
Blink(); //眨眼
digitalWrite(leftEye, HIGH); // 打开左眼
digitalWrite(rightEye, HIGH); // 打开右眼
analogWrite(motor, 250); //打开风扇
ShakeHead();
analogWrite(motor, 0); //关闭风扇
digitalWrite(leftEye, LOW); // 关左眼
digitalWrite(rightEye, LOW); // 关右眼
myservo.detach(); // 释放
}
analogWrite(LED, brightness);
brightness = brightness + fadeAmount;
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount;
}
delay(5);
}
//风扇旋转
void rotatingFan()
{
for (int value = 300; value >= 200; value -= 5)
{
analogWrite(motor, value); //PWM调速
delay(20);
}
analogWrite(motor, 0); //关闭风扇
}
//定义机器人摇头子函数
// 转头范围90°,从左边转到右边,再从右边转到左边
void ShakeHead()
{
myservo.attach(servoPin); //设置舵机连接的引脚
for (pos = 102; pos <= 160; pos += 1)
{
myservo.write(pos);
delay(15);
}
for (pos = 160; pos >= 60; pos -= 1)
{
myservo.write(pos);
delay(15);
}
for (pos = 60; pos <= 102; pos += 1)
{
myservo.write(pos);
delay(15);
}
myservo.detach();
}
//定义机器人眨眼子函数
//眨眼前先打开胸灯,闪烁后关闭胸灯。
void Blink()
{
//digitalWrite(LED, HIGH); //打开胸灯
int i = 1; //定义闪烁次数
while (i)
{
digitalWrite(leftEye, HIGH);
digitalWrite(rightEye, HIGH);
delay(800);
digitalWrite(leftEye, LOW);
digitalWrite(rightEye, LOW);
delay(500);
digitalWrite(leftEye, HIGH);
digitalWrite(rightEye, HIGH);
delay(1000);
digitalWrite(leftEye, LOW);
digitalWrite(rightEye, LOW);
delay(1000);
i--;
}
//digitalWrite(LED, LOW);//关闭胸灯
}
//定义机器人摇头子函数2
// 快速摇头
void ShakeHead2()
{
for (pos = 102; pos <= 140; pos += 3)
{
myservo.write(pos);
delay(20);
}
for (pos = 140; pos <= 170; pos += 3)
{
myservo.write(pos);
delay(20);
}
for (pos = 170; pos >= 140; pos -= 1)
{
myservo.write(pos);
delay(20);
}
for (pos = 140; pos >= 102; pos -= 1)
{
myservo.write(pos);
delay(20);
}
for (pos = 102; pos <= 60; pos -= 3)
{
myservo.write(pos);
delay(20);
}
for (pos = 60; pos <= 20; pos -= 3)
{
myservo.write(pos);
delay(20);
}
for (pos = 20; pos <= 60; pos += 1)
{
myservo.write(pos);
delay(20);
}
for (pos = 60; pos <= 102; pos += 1)
{
myservo.write(pos);
delay(20);
}
}[/mw_shl_code]
一些管脚的定义参见上面的函数
三、下面是另一个版本,可以实现呼吸灯的功能,主要是一些定时器的问题相互干涉,
[mw_shl_code=c,true]int sp1 = 9; //定义舵机接口数字接口7
int pulsewidth;//定义脉宽变量
int val;
int val1;
int myangle1;
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978
int led = 1;
int fuhao = 1;
int T = 0;
//2#leye
//3#buzzer
//11#motor
//10#led
//9#servo
//8#reye
void setup() {
skycity();
sick();
// blinkeyes(3);
Serial.begin(9600);
T = millis();
}
void loop() {
if (analogRead(A0) > 500)
{
buzzer(50);
}
if (( millis() - T ) > 30)
{
if (led == 150 || led == 0) fuhao = -fuhao;
led = led + fuhao;
Serial.println(led);
analogWrite(10, led);
T = millis();
}
}
///////////// 子函数 ///////////////////
///////////// 子函数 ///////////////////
///////////// 子函数 ///////////////////
void servopulse(int sp1, int val1) //定义一个脉冲函数
{
myangle1 = map(val1, 0, 180, 500, 2480);
digitalWrite(sp1, HIGH); //将舵机接口电平至高
delayMicroseconds(myangle1);//延时脉宽值的微秒数
digitalWrite(sp1, LOW); //将舵机接口电平至低
delay(20 - val1 / 1000);
}
void sick(void)//摇头
{ pinMode(sp1, OUTPUT); //设定舵机接口为输出接口
int pos = 90;
for (pos = 90; pos <= 150; pos += 1)
{
for (int i = 0; i <= 2; i++) //给予舵机足够的时间让它转到指定角度
{
servopulse(sp1, pos); //引用脉冲函数
}
}
for (pos = 150; pos >= 30; pos -= 1)
{
for (int i = 0; i <= 2; i++) //给予舵机足够的时间让它转到指定角度
{
servopulse(sp1, pos); //引用脉冲函数
}
}
for (pos = 30; pos <= 90; pos += 1)
{
for (int i = 0; i <= 2; i++) //给予舵机足够的时间让它转到指定角度
{
servopulse(sp1, pos); //引用脉冲函数
}
}
}
void blinkeyes(int a)
{
while (a--) {
digitalWrite(8, 1);
digitalWrite(2, 1);
delay(700);
digitalWrite(8, 0);
digitalWrite(2, 0);
delay(700);
}
}
void buzzer(int a )
{
int melody1[] = {
NOTE_C4, NOTE_C4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_A4, NOTE_G4, 0, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_C4, 0, NOTE_G4, NOTE_G4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_D4, 0, NOTE_G4, NOTE_G4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_D4, 0, NOTE_C4, NOTE_C4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_A4, NOTE_G4, 0, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_C4, 0
};
for (int thisNote = 0; thisNote < 48; thisNote++) {
tone(3, melody1[thisNote], a);
delay(a);
noTone(3);
delay((1.3 * a));
}
}
void skycity()
{ //第一句
int melody[] = {
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_B4, NOTE_C5, NOTE_E5, NOTE_B4
};
int noteDurations[] = {
8, 8, 3, 8, 4, 4, 4
};
for (int thisNote = 0; thisNote < 7; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(3, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(3);
}
//第二句
int melody2[] = {
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_B4, NOTE_C5, NOTE_E5, NOTE_B4
};
int noteDurations2[] = {
8, 8, 3, 8, 4, 4, 4
};
for (int thisNote2 = 0; thisNote2 < 7; thisNote2++) {
int noteDuration2 = 1000 / noteDurations2[thisNote2];
tone(3, melody2[thisNote2], noteDuration2);
int pauseBetweenNotes2 = noteDuration2 * 1.30;
delay(pauseBetweenNotes2);
noTone(3);
}
//第三句
int melody3[] = {
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_B4, NOTE_C5, NOTE_E5, NOTE_B4
};
int noteDurations3[] = {
8, 8, 3, 8, 4, 4, 4
};
for (int thisNote3 = 0; thisNote3 < 7; thisNote3++) {
int noteDuration3 = 1000 / noteDurations3[thisNote3];
tone(3, melody3[thisNote3], noteDuration3);
int pauseBetweenNotes3 = noteDuration3 * 1.30;
delay(pauseBetweenNotes3);
noTone(3);
}
//第四句
int melody4[] = {
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_B4, NOTE_C5, NOTE_E5, NOTE_B4
};
int noteDurations4[] = {
8, 8, 3, 8, 4, 4, 4
};
for (int thisNote4 = 0; thisNote4 < 7; thisNote4++) {
int noteDuration4 = 1000 / noteDurations4[thisNote4];
tone(3, melody4[thisNote4], noteDuration4);
int pauseBetweenNotes4 = noteDuration4 * 1.30;
delay(pauseBetweenNotes4);
noTone(3);
}
}
[/mw_shl_code]
|