創客教育社

活動剪影秀

  • slider image 1
  • slider image 2

2017/4/28

搜尋

QRCode

QR Code

網站列表

萌典查生字

查生字

Dr.eye 英漢字典

查單字

維基百科查詢

查百科

推文工具

懸浮微粒 (pm2.5) 預報

懸浮微粒 (pm2.5) 預報

空氣品質 (PSI) 預報

空氣品質 (PSI) 預報

線上聊天室

倒數計時

2016 聖誕節

標籤

Bcar_turn-第二關

最終版:

#include <AFMotor.h> // 利用AF_Motor函式庫
#include <Servo.h>
char command;
int power1 = 150;
int power2 = 255;
int angle1 = 110;
int angle2 = 70;
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
Servo leftservo;
Servo rightservo;

void stop() {
  Serial.print("Stop !");
  rightservo.write(93);
  leftservo.write(93);
  motor1.run(RELEASE);
  motor2.run(RELEASE);
}

void forward() {
  Serial.print("FORWARD...");
  motor1.setSpeed(power2);
  motor2.setSpeed(power2);
  motor1.run(BACKWARD);
  motor2.run(FORWARD);
}

void backward() {
  Serial.print("BACKWARD...");
  motor1.setSpeed(power2);
  motor2.setSpeed(power2);
  motor1.run(FORWARD);
  motor2.run(BACKWARD);
}

void turnLeft() {
  Serial.print("TURN LEFT...");
  motor1.setSpeed(power2);
  motor2.setSpeed(power2);
  motor1.run(FORWARD);
  motor2.run(FORWARD);
}

void turnRight() {
  Serial.print("TURN RIGHT...");
  motor1.setSpeed(power2);
  motor2.setSpeed(power2);
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
}

void openclips() {
 leftservo.write(angle1);
}

void closeclips() {
leftservo.write(angle2);
}



void setup() {
  // put your setup code here, to run once:
  Serial.begin(38400);
  leftservo.attach(10);
  stop();  
}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available() > 0) {
    command = Serial.read();

    switch (command) {
      case 'f':
        forward();
        break;
      case 'b':
        backward();
        break;
      case 'l':
        turnLeft();
        break;
      case 'r':
        turnRight();
        break;
      case 'x':
        openclips();
        break;
      case 'c':
        closeclips();
        break;
      case 's':
        stop();
        break;
    }

  }

}

舊版:

#include <AFMotor.h> // 利用AF_Motor函式庫
#include <Servo.h>
char command;
int power1 = 150;
int power2 = 255;
int angle1 = 110;
int angle2 = 70;
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
Servo leftservo;
Servo rightservo;
void stop() {
  rightservo.write(93);
  leftservo.write(93);
  motor1.run(RELEASE);
  motor2.run(RELEASE);
}
void forward() {
  motor1.setSpeed(power2);
  motor2.setSpeed(power2);
  motor1.run(FORWARD);
  motor2.run(FORWARD);
}
void backward() {
  motor1.setSpeed(power2);
  motor2.setSpeed(power2);
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
}
void turnLeft() {
  motor1.setSpeed(power2);
  motor2.setSpeed(power2);
  motor1.run(BACKWARD);
  motor2.run(FORWARD);
}
void turnRight() {
  motor1.setSpeed(power2);
  motor2.setSpeed(power2);
  motor1.run(FORWARD);
  motor2.run(BACKWARD);
}
void openclips() {
 leftservo.write(angle1);
}
void closeclips() {
leftservo.write(angle2);
}
void setup() {
  // put your setup code here, to run once:
  Serial.begin(38400);
  leftservo.attach(10);
  stop();
}
void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available() > 0) {
    command = Serial.read();
    switch (command) {
      case 'f':
        forward();
        break;
      case 'b':
        backward();
        break;
      case 'l':
        turnLeft();
        break;
      case 'r':
        turnRight();
        break;
      case 'x':
        openclips();
        break;
      case 'c':
        closeclips();
        break;
      case 's':
        stop();
        break;
    }
  }
}

 

  •  
    1) R4M Bcar組裝說明-第2關.pdf