If you're seeing this message, it means we're having trouble loading external resources on our website.

如果你被网页过滤器挡住,请确保域名*.kastatic.org*.kasandbox.org 没有被阻止.

主要内容

蜘蛛编程

教Spider机器人如何跳舞

现在是教Spider机器人跳舞的时候了
1. 去这个网页 http://arduino.cc/en/main/software ,并下载你电脑系统对应的Arduino的软件。
2. 启动Arduino,点击文件-新建。
Arduino新菜单
3. 点击工具并选择 Arduino Nano w/ ATmega328
Arduino 下一个图像
4.将USB线连接电脑和你的Spider机器人的Arduino上
Arduino 下一个图像
5. 选择如下端口:
Arduino 下一个图像
6. 复制下列跳舞的代码(选择全部,并在Windows系统上使用Control + C按键,或在Mac上使用Command + C按键)
// DANCE CODE for Spider by Karl Wendt 1/15/13  
// Define the pin outs  
int Red_Eye_Left= 12; // LED connected to pin 12  
int Red_Eye_Right= 4; // LED connected to pin 4  
int Green_Eye_Left= 8; // LED connected to pin 8  
int Green_Eye_Right= 3; // LED connected to pin 3  
int Blue_Eye_Left= 7; // LED connected to pin 7  
int Blue_Eye_Right= 2; // LED connected to pin 2  
int Left_Reverse = 6; // Right motor control 1 connected to pin 6  
int Left_Forward = 9; // Right motor control 2 connected to pin 9  
int Right_Forward = 10; // Left motor control 1 connected to pin 10  
int Right_Reverse = 11; // Left motor control 2 connected to pin 11 
// Set the mode of the pin (output or input)  
void setup() {
pinMode(Red_Eye_Left, OUTPUT); //sets the digital pin as output  
    pinMode(Red_Eye_Right, OUTPUT); //sets the digital pin as output  
    pinMode(Green_Eye_Left, OUTPUT); //sets the digital pin as output  
    pinMode(Green_Eye_Right, OUTPUT); //sets the digital pin as output  
    pinMode(Blue_Eye_Left, OUTPUT); //sets the digital pin as output  
    pinMode(Blue_Eye_Right, OUTPUT); //sets the digital pin as output  
    pinMode(Left_Forward, OUTPUT); // sets L1 the digital pin as output  
    pinMode(Left_Reverse, OUTPUT); // sets L2 the digital pin as output  
    pinMode(Right_Forward, OUTPUT); // sets L3 the digital pin as output  
    pinMode(Right_Reverse, OUTPUT); // sets L4 the digital pin as output  
}  
// Create your code loop  
void loop() {  
    digitalWrite(Red_Eye_Left, HIGH); //turns on left eye red LED  
    digitalWrite(Red_Eye_Right, HIGH); //turns on right eye red LED  
    delay(500); //run for half a second  
    digitalWrite(Red_Eye_Left, LOW); //turns off left eye red LED  
    digitalWrite(Red_Eye_Right, LOW); //turns off right eye red LED  
    digitalWrite(Green_Eye_Left, HIGH); //turns on left eye green LED  
    delay(500); //run for half a second  
    digitalWrite(Green_Eye_Left, LOW); //turns off left eye green LED  
    digitalWrite(Blue_Eye_Left, HIGH); //turns on left eye blue LED  
    delay(500); //run for half a second  
    digitalWrite(Blue_Eye_Left, LOW); //turns on left eye blue LED  
    delay(500); //run for half a second  
    digitalWrite(Green_Eye_Right, HIGH); //turns on right eye green LED  
    delay(500); //run for half a second  
    digitalWrite(Green_Eye_Right, LOW); //turns off right eye green LED  
    digitalWrite(Blue_Eye_Right, HIGH); //turns on right eye blue LED  
    delay(500); //run for half a second  
    digitalWrite(Blue_Eye_Right, LOW); //turns off right eye blue LED  
    delay(500); //run for half a second  
    //Make new colors by combining LED lights  
    digitalWrite(Blue_Eye_Left, HIGH); //turns on left eye blue LED  
    digitalWrite(Blue_Eye_Right, HIGH); //turns on right eye blue LED  
    digitalWrite(Red_Eye_Left, HIGH); //turns on left eye red LED  
    digitalWrite(Red_Eye_Right, HIGH); //turns on right eye red LED  
    delay(1000); //run for a quarter second  
    digitalWrite(Blue_Eye_Left, LOW); //turns on left eye blue LED  
    digitalWrite(Blue_Eye_Right, LOW); //turns on right eye blue LED  
    digitalWrite(Green_Eye_Left, HIGH); //turns on left eye green LED  
    digitalWrite(Green_Eye_Right, HIGH); //turns on right eye green LED  
    digitalWrite(Red_Eye_Left, HIGH); //turns on left eye red LED  
    digitalWrite(Red_Eye_Right, LOW); //turns on right eye red LED  
    delay(1000); //run for a quarter second  
    //Turn on the motors and make Spider move  
    analogWrite(Left_Reverse, 90); //run left motor backwards at PWM speed 90  
    delay(500); //run for half a second  
    analogWrite(Left_Reverse, 0); //stop left motor  
    delay(200); //run for 2 seconds  
    analogWrite(Right_Reverse, 90); //run right motor backwards at PWM speed 90  
    delay(500); //run for half a second  
    analogWrite(Right_Reverse, 0); //stop right motor  
    analogWrite(Left_Forward, 90); //run left motor forward at PWM speed 90  
    delay(500); //wait for half a second  
    analogWrite(Left_Forward, 0); //stop left motor  
    analogWrite(Right_Forward, 90); //run right motor forward at PWM speed 90  
    delay(500); //run for half a second  
    analogWrite(Right_Forward, 0); //stop right motor  
    analogWrite(Left_Reverse, 90); //run left motor backwards at PWM speed 90  
    delay(500); //run for half a second  
    analogWrite(Left_Reverse, 0); //stop left motor  
    delay(200); //run for 2 seconds  
    analogWrite(Right_Reverse, 90); //run right motor backwards at PWM speed 90  
    delay(500); //run for half a second  
    analogWrite(Right_Reverse, 0); //stop right motor  
    analogWrite(Left_Forward, 90); //run left motor forward at PWM speed 90  
    delay(500); //wait for half a second  
    analogWrite(Left_Forward, 0); //stop left motor  
    analogWrite(Right_Forward, 90); //run right motor forward at PWM speed 90  
    delay(500); //run for half a second  
    analogWrite(Right_Forward, 0); //stop right motor  
}
7. 将代码粘贴到你刚刚新建的Arduino文件当中(在文件中单击,并使用Control + V 或者 Command + V)
8.点击水平的箭头,将代入导入Spider机器人中
Arduino 下一个图像
9. 拔出USB线
10. 转一下Spider机器人,看它表演!
11. 如果你的Spider动地不够快,你可能需要增加电动机的速度。
注意:请确认你的Spider机器人在平坦的地上(“不是在桌子上”)!它还没有被编程去避开桌子边缘,它可能会掉落摔坏。

想加入讨论吗?

尚无帖子。
你会英语吗?单击此处查看更多可汗学院英文版的讨论.