|
我是用APC220當作傳輸模組
現在卡在不知道如何傳送/接收訊號
以下為程式碼
控制端
// 使用 Servo 函式庫來控制motor
#include <Servo.h>
// 使用 Servo 函式庫來控制,新增4個馬達
Servo motor1,motor2,motor3,motor4;
// 定義硬體實際腳位
#define up_button 8
#define down_button A1
#define left_button 9
#define right_button 12
#define stick_button A0
#define level_stick A2
#define vertical_stick A3
// 起始設定
void setup ()
{
Serial.begin (9600); // 鮑率,bit/s
// 設定各腳位模式
pinMode (left_button, INPUT);
pinMode (right_button, INPUT);
pinMode (up_button, INPUT);
pinMode (down_button, INPUT);
pinMode (stick_button , INPUT);
pinMode (level_stick , INPUT);
pinMode (vertical_stick, INPUT);
// 設定各馬達之腳位
motor1.attach(3);
motor2.attach(5);
motor3.attach(6);
motor4.attach(9);
}
// 設定變數來決定電流以控制馬達
int i = 0;
// 持續動作
void loop ()
{
// 宣告變數為各腳位讀入
int left_state = digitalRead (left_button);
int right_state = digitalRead (right_button);
int up_state = digitalRead (up_button);
int down_state = digitalRead (down_button);
int stick_state = digitalRead (stick_button);
int level_value = analogRead (level_stick);
int vertical_value = analogRead (vertical_stick);
// 控制電流增加,馬達加速
while(!(up_state) && i <= 180)
{
i+=5;
break;
}
// 控制電流減少,馬達減速
while(!(down_state) && i > 0)
{
i-=5;
break;
}
motor1.write(i);
motor2.write(i);
motor3.write(i);
motor4.write(i);
Serial.println(i );
Serial.print("Vertical: ");
Serial.println(vertical_value);
Serial.print("Level: ");
Serial.println(level_value);
/*
Serial.print ("up:");
Serial.print (up_state);
Serial.print (" down:");
Serial.print (down_state);
Serial.print (" left:");
Serial.print (left_state);
Serial.print (" right:");
Serial.print (right_state);
Serial.print (" vertical:");
Serial.print (vertical_value);
Serial.print (" level:");
Serial.print (level_value);
Serial.print (" stick:");
Serial.println (stick_state);
*/
delay (500);
}
接收端
void setup(){
Serial.begin (9600); // 鮑率,bit/s
}
// 連接 pin 腳
// 控制速度(0~180)
void loop(){
}
該在哪增加些讀取/寫入資料 |
|