嘗試看看更改第34,36行的數字看看
delay(...);
/*
Blink
Turns an LED on for one second, then off for one second, repeatedly.
Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
the correct LED pin independent of which board is used.
If you want to know what pin the on-board LED is connected to on your Arduino
model, check the Technical Specs of your board at:
https://www.arduino.cc/en/Main/Products
modified 8 May 2014
by Scott Fitzgerald
modified 2 Sep 2016
by Arturo Guadalupi
modified 8 Sep 2016
by Colby Newman
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Blink
*/
// the setup function runs once when you press reset or power the board
// 單行註解
/*
多行註解
多行註解
多行註解
*/
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
delay(1000);
Servo MyServo;
MyServo.attach(pin, 500, 2400);
MyServo.write(pos);
範例下載
#include <Wire.h>
#include <Servo.h>
#define DataPin 8
Servo MyServo;
MyServo.attach(DataPin, 500, 2400);
MyServo.write(90);
MyServo.write(...);
MyServo.write(...);
delay(...);
MyServo.write(...);
delay(...);
SoftwareSerial BTSerial(RXD, TXD);
BTSerial.begin(38400);
BTSerial.available();
Serial.read();
BTSerial.write(...);
範例下載
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11);
void setup() {
pinMode(9, OUTPUT);
digitalWrite(9, HIGH);
Serial.begin(9600);
BTSerial.begin(38400);
}
void loop() {
if (BTSerial.available())
Serial.write(BTSerial.read());
if (Serial.available())
BTSerial.write(Serial.read());
}
... | Command |
---|---|
查詢address | AT+ADDR? |
查詢名字 | AT+NAME? |
更改名字為XXX | AT+NAME=XXX |
查詢配對密碼 | AT+PSWD? |
更改配對密碼為XXX | AT+PSWD=XXX |
#define KEY 10
#define RXD 11
#define TXD 12
#include
#include
#include
// Servo motor
#define Servo_1_Data 5
#define Servo_2_Data 6
Servo Servo_1, Servo_2;
int pos_1 = 0, pos_2 = 0;
SoftwareSerial BT(RXD, TXD);
void setup() {
BT.begin(38400);
Serial.begin(9600);
//setting motor
pinMode(KEY, OUTPUT);
digitalWrite(KEY, LOW);
//testing motor
Servo_1.attach(Servo_1_Data,500,2400);
Servo_2.attach(Servo_2_Data,500,2400);
Servo_1.write(90);
Servo_2.write(90);
delay(3000);
}
void loop() {
int cmd[3],ptr=0;
int table[5][3]={
{128,128,128}, //2
{248,128,128}, //3
{120,248,128}, //5
{128,248,128}, //6
{248,248,128} //7
};
while(1){
while(BT.available()>0){;
cmd[ptr] = BT.read();
ptr++;
}
if(ptr == 3){
for(int i=0;i<5;i++){
int flag = 1;
for(int j=0;j<3; .j++){
if(table[i][j]!=cmd[j]){
flag = 0;
break;
}
}
if(flag > 0){
if(i == 0){ //set to 180
Servo_1.write(180);
Serial.println(2);
}
else if(i == 1){ //set to 0
Servo_1.write(0);
Serial.println(3);
}
else if(i == 2){ //set to 180
Servo_2.write(180);
Serial.println(5);
}
else if(i == 3){ //set to 0
Servo_2.write(0);
Serial.println(6);
}
else if(i == 4){ //set both to 90
Servo_2.write(90);
Servo_1.write(90);
Serial.println(7);
}
else{
Serial.println("ERROR");
}
}
}
ptr = 0;
}
}
}