Apa saja alat yang dibutuhkan diantaranya adalah:
1. 2 buah arduino uno
2. 2 buah modul nRF24L01
3. Driver motor dc l293d
4. 2 buah motor dc gearbox + ban
5. Baterai 12v + holder
Program Arduino Transmitter
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
int datain;
int maju;
int mundur;
int kanan;
int kiri;
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
void setup(void){
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe);
pinMode(13,OUTPUT);
pinMode(7,INPUT);
pinMode(6,INPUT);
pinMode(5,INPUT);
pinMode(4,INPUT);
digitalWrite(7,HIGH);
digitalWrite(6,HIGH);
digitalWrite(5,HIGH);
digitalWrite(4,HIGH);
}
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
int datain;
int maju;
int mundur;
int kanan;
int kiri;
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
void setup(void){
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe);
pinMode(13,OUTPUT);
pinMode(7,INPUT);
pinMode(6,INPUT);
pinMode(5,INPUT);
pinMode(4,INPUT);
digitalWrite(7,HIGH);
digitalWrite(6,HIGH);
digitalWrite(5,HIGH);
digitalWrite(4,HIGH);
}
void loop(void){
maju = digitalRead(6);
mundur = digitalRead(4);
kiri = digitalRead(5);
kanan = digitalRead(7);
if(maju == 0){
msg[0] = 1;
radio.write(msg, 1);
//Serial.println(msg[0]);
}
else if(mundur == 0){
msg[0] = 4;
radio.write(msg, 1);
//Serial.println(msg[0]);
}
else if(kiri == 0){
msg[0] = 3;
radio.write(msg, 1);
//Serial.println(msg[0]);
}
else if(kanan == 0){
msg[0] = 2;
radio.write(msg, 1);
//Serial.println(msg[0]);
}
else{
msg[0] = 5;
radio.write(msg, 1);
//Serial.println(msg[0]);
}
}
maju = digitalRead(6);
mundur = digitalRead(4);
kiri = digitalRead(5);
kanan = digitalRead(7);
if(maju == 0){
msg[0] = 1;
radio.write(msg, 1);
//Serial.println(msg[0]);
}
else if(mundur == 0){
msg[0] = 4;
radio.write(msg, 1);
//Serial.println(msg[0]);
}
else if(kiri == 0){
msg[0] = 3;
radio.write(msg, 1);
//Serial.println(msg[0]);
}
else if(kanan == 0){
msg[0] = 2;
radio.write(msg, 1);
//Serial.println(msg[0]);
}
else{
msg[0] = 5;
radio.write(msg, 1);
//Serial.println(msg[0]);
}
}
Program Arduino Receiver / Slave
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
//motor kiri
int mtr1 = 2;
int mtr2 = 3;
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
//motor kiri
int mtr1 = 2;
int mtr2 = 3;
//motor kanan
int mtr3 = 4;
int mtr4 = 5;
void setup(void){
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();
pinMode(mtr1, OUTPUT);
pinMode(mtr2, OUTPUT);
pinMode(mtr3, OUTPUT);
pinMode(mtr4, OUTPUT);
}
void loop(void){
if (radio.available()){
bool done = false;
while (!done){
done = radio.read(msg, 1);
Serial.println(msg[0]);
if (msg[0] == 1){
delay(10);
digitalWrite(mtr1, HIGH);
digitalWrite(mtr2, LOW);
digitalWrite(mtr3, HIGH);
digitalWrite(mtr4, LOW);
//delay(200);
}
else if (msg[0] == 2){
delay(10);
digitalWrite(mtr1, HIGH);
digitalWrite(mtr2, LOW);
digitalWrite(mtr3, LOW);
digitalWrite(mtr4, LOW);
//delay(200);
}
else if (msg[0] == 3){
delay(10);
digitalWrite(mtr1, LOW);
digitalWrite(mtr2, LOW);
digitalWrite(mtr3, HIGH);
digitalWrite(mtr4, LOW);
//delay(200);
}
else if (msg[0] == 4){
delay(10);
digitalWrite(mtr1, LOW);
digitalWrite(mtr2, HIGH);
digitalWrite(mtr3, LOW);
digitalWrite(mtr4, HIGH);
//delay(200);
}
else {
delay(10);
digitalWrite(mtr1, LOW);
digitalWrite(mtr2, LOW);
digitalWrite(mtr3, LOW);
digitalWrite(mtr4, LOW);
//delay(200);
}
delay(10);
}
}
else{
Serial.println("No radio available");
}
}
Selamat mencoba, semoga berhasil....
0 komentar:
Post a Comment