Arduino
RF24
ArduDrone
#include <SPI.h>
#include "RF24.h"
#include <Streaming.h>
RF24 rf24(9,10); // CE腳, CSN腳
const byte addr[] = "00001";
short msg[]={0,0,0,0,0,0,0,0,0,0};
void setup() {
Serial.begin(9600);
rf24.begin();
rf24.setChannel(83);
rf24.setPALevel(RF24_PA_MIN);
rf24.setDataRate(RF24_2MBPS);
rf24.openWritingPipe(addr);
rf24.stopListening();
Serial.println("sending");
pinMode(A0,INPUT);
pinMode(A1,INPUT);
pinMode(A2,INPUT);
pinMode(A3,INPUT);
pinMode(2,INPUT_PULLUP);
pinMode(3,INPUT_PULLUP);
pinMode(4,INPUT_PULLUP);
pinMode(5,INPUT_PULLUP);
pinMode(6,INPUT_PULLUP);
pinMode(7,INPUT_PULLUP);
}
void printData(){
for(int i=0;i<10;i++){
Serial << msg[i] << ' ';
}Serial << endl;
}
void loop() {
msg[0]=analogRead(A0), msg[1]=analogRead(A1), msg[2]=analogRead(A2), msg[3]=analogRead(A3);
msg[4] = !digitalRead(2), msg[5] = !digitalRead(3), msg[6] = !digitalRead(5), msg[7] = !digitalRead(4);
msg[8] = digitalRead(6), msg[9] = digitalRead(7);
printData();
rf24.write(&msg, sizeof(msg));
delay(50);
}
#include <Streaming.h>
#include <SPI.h>
#include "RF24.h"
RF24 rf24(8, 7); // CE腳, CSN腳
#include <Servo.h>
Servo FL, FR, BL, BR; // 10 9 6 5
#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
const byte addr[] = "00001";
const byte pipe = 1; // 指定通道編號
short msg[] = {0,0,0,0,0,0,0,0,0,0};
short Speed[4] = {0,0,0,0};
short ad[4] = {0,0,0,0}; // --
int volume = 0; //初始速度
void setup() {
Serial.begin(9600);
rf24.begin();
rf24.setChannel(83); // 設定頻道編號
rf24.setPALevel(RF24_PA_HIGH);
rf24.setDataRate(RF24_2MBPS);
rf24.openReadingPipe(pipe, addr); // 開啟通道和位址
rf24.startListening(); // 開始監聽無線廣播
Serial.println("nRF24L01 ready!");
FL.attach(10), FR.attach(9), BL.attach(6), BR.attach(5);
FL.write(0), FR.write(0), BR.write(0), BL.write(0); //motor initailize & reset
delay(100);
mpu.begin(0x68, 6);
}
void printData(){
Serial << "Data: ";
for(int i=0;i<10;i++){
Serial << msg[i] << ' ';
}Serial << endl;
}
void printSpeed(){
Serial << "Speed: ";
for(int i=0;i<4;i++){
Serial << Speed[i] << ' ';
}Serial << endl;
}
void updown(short i){
for(int j=0;j<4;j++)
Speed[j] = i;
}
void backforth(short i){
short tmp = map(abs(i-498), 0, 512, 0, 10); //move volume
if((i-498)>5){ //move forth
Speed[2] += tmp; Speed[3] += tmp;
Serial << "forward" << endl;
}else if((i-498)<-5){ //move back
Speed[0] += tmp; Speed[1] += tmp;
Serial << "back" << endl;
}
}
void turnLR(short i){
short tmp = map(abs(i-521), 0, 512, 0, 10); //move volume
if((i-521)<-5){ //turn left
Speed[1] += tmp; Speed[2] += tmp;
Serial << "left" << endl;
}else if((i-521)>5){ //move right
Speed[0] += tmp; Speed[3] += tmp;
Serial << "right" << endl;
}
}
void tiltLR(char i){
if(i == 'L'){
Speed[1] += 10; Speed[3] += 10;
Serial << "tilt left" <<endl;
}else if(i == 'R'){
Speed[0] += 10; Speed[2] += 10;
Serial << "tilt right" << endl;
}
}
void balance(int x, int y){
int n=2;
if(x>5){
x-=5;
Speed[0]-=n*x;
Speed[2]-=n*x;
Speed[1]+=n*x;
Speed[3]+=n*x;
}
if(x<-5){
x+=5;
Speed[0]-=n*x;
Speed[2]-=n*x;
Speed[1]+=n*x;
Speed[3]+=n*x;
}
if(y>5){
y-=5;
Speed[0]-=n*y;
Speed[1]-=n*y;
Speed[2]+=n*y;
Speed[3]+=n*y;
}
if(y<-5){
y+=5;
Speed[0]-=n*y;
Speed[1]-=n*y;
Speed[2]+=n*y;
Speed[3]+=n*y;
}
}
void loop() {
mpu.read();
int sx=0, sy=0, sz=0;
for(int i=0;i<25;i++){
sx+=mpu.Xaxis();
sy+=mpu.Yaxis();
sz+=mpu.Zaxis();
delay(2);
}
sx/=25; sy/=25; sz/=25;
bool fg = 0;
if (rf24.available(&pipe)) {
rf24.read(&msg, sizeof(msg));
volume = map(msg[0], 0, 1023, 0, 180); //油門大小
updown(volume);
if(abs(msg[2]-498)>=10){ backforth(msg[2]); fg = 1; } //前後
if(abs(msg[3]-521)>=10){ turnLR(msg[3]); fg = 1; } //左右轉
if(msg[4]){ tiltLR('R'); }else if(msg[5]){ tiltLR('L'); fg = 1; }
if(fg == 0){
if(abs(sx)>5||abs(sy)>5)
balance(sx, sy);
}
//printSpeed();
//printData();
//Serial << mpu.Xaxis() << ' ' << mpu.Yaxis() << ' ' << mpu.Zaxis() << endl;
Serial<<sx<<' '<<sy<<' '<<sz<<endl;
FL.write(Speed[0]-ad[0]), FR.write(Speed[1]-ad[1]), BL.write(Speed[2]-ad[2]), BR.write(Speed[3]-ad[3]); //speed write
//delay(10);
}
}
以下是Arduino中會用到的基本語法(用的是arduino的寫法),要看完整的Cpp教學請上Google自行查詢或參考ref。Arduino的程式是建立在Cpp之上,但並不完全包含所有函數,如有Cpp中能用但Arduino中無法include的東西(如<<和vector),請見下面的擴充函式庫連結,使用方法會在libraries(函式庫)的使用中說明
Apr 8, 2025https://docs.google.com/document/d/1h3spMYwUqSE39_D_gq_owMljiNo6SCMz/edit?usp=drivesdk&ouid=102811827965553629250&rtpof=true&sd=true
Jan 22, 2024Enigma(物理)
Dec 11, 2023簡化過程
Nov 28, 2023or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up