• slider image 763
:::

sips - 資訊 | 2017-07-19 | 人氣:445

#include <SoftwareSerial.h>
SoftwareSerial BT(12, 13);
char ch = 'S';

const int PINFOOT7 = 7;
const int PINFOOT8 = 8;
const int PINFOOT9 = 9;
const int PINFOOT10 = 10;


void setup() {
  pinMode(PINFOOT7, OUTPUT);
  pinMode(PINFOOT8, OUTPUT);
  pinMode(PINFOOT10, OUTPUT);
  pinMode(PINFOOT9, OUTPUT);
  Serial.begin(9600);
  Serial.println("BT is ready!");
  BT.begin(9600);
}

void loop() {

  if(Serial.available()) {
    ch = Serial.read();
    BT.print(ch);
  }
  if (BT.available()) {
    ch = BT.read();
    Serial.print(ch);
  }
 
  switch(ch){
    case 'B': backward(); delay(100); break;
    case 'F': forward(); delay(100); break;
    case 'L': turnLeft(); delay(100); break;
    case 'R': turnRight(); delay(100); break;
    case 'S': motorstop(); delay(100); break;
  }
  
}

void forward() {
  digitalWrite(PINFOOT7, LOW);
  digitalWrite(PINFOOT8, HIGH);
  digitalWrite(PINFOOT10, LOW);
  analogWrite(PINFOOT9,200);    
}

void turnRight() {
  digitalWrite(PINFOOT10, LOW);
  digitalWrite(PINFOOT9, HIGH);
}

void turnLeft() {
  digitalWrite(PINFOOT7, LOW);
  digitalWrite(PINFOOT8, HIGH);
}

void motorstop() {
  digitalWrite(PINFOOT7, LOW);
  digitalWrite(PINFOOT8,  LOW);
  digitalWrite(PINFOOT10, LOW);
  digitalWrite(PINFOOT9,  LOW);
}

void backward() {   
  digitalWrite(PINFOOT7, HIGH);
  digitalWrite(PINFOOT8, LOW);
  digitalWrite(PINFOOT10, HIGH);
  digitalWrite(PINFOOT9, LOW);
}

網友個人意見,不代表本站立場,對於發言內容,由發表者自負責任。
發表者
樹狀展開
:::

宣導網連結

好站連結