giorgi877ჩავაგდე რამდენიმეჯერ გადმოვიწერე.
ეს კოდია:
/*
NRF24L01 Module Demo Controller Code
Pin Assignment
D0
D1
D2
D3
D4
D5
D6
D7 Joystick Push Button
D8 NRF24 CE
D9
D10 NRF24 CSN
D11 NRF24 MOSI
D12 NRF24 MISO
D13 NRF24 SCK
A0 Joystick Y Axis
A1 Joystick X Axis
A2
A3
A4
A5
*/
// Define variables
int Right_Joy_X = 0x00;
int Right_Joy_Y = 0x00;
int Right_Joy_X_Old = 0x00;
int Right_Joy_Y_Old = 0x00;
int Right_Joy_Button = LOW;
int Head_Lights = 0;
// Include Libraries
#include <SPI.h>
#include <RH_NRF24.h>
RH_NRF24 nrf24; // Create NRF24 object
uint8_t command[11]; // Create array for data to be sent
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN]; // Create an array buffer for the NRF24 module
void setup()
{
pinMode(7, INPUT_PULLUP); // Right joystick button
// initialize the radio module
nrf24.init();
nrf24.setChannel(1);
nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm);
command[0] = 0x4A;
command[1] = 0x44;
command[2] = 0x38;
command[3] = 0x33;
command[4] = 0x06;
command[5] = 0x01;
}
void loop() {
Check_Vehicle_ID();
Check_Buttons();
Check_Pots();
}
void Check_Vehicle_ID(){
command[0] = 0x4A; // J
command[1] = 0x44; // D
command[2] = 0x38; // 8
command[3] = 0x33; // 3
}
void Check_Buttons(){
// Check Joystick Push Button
Right_Joy_Button = digitalRead(7);
if (Right_Joy_Button==LOW)
{
if (Head_Lights == 0){
Head_Lights = 1;
command[10]=1;
}
else if (Head_Lights == 1){
Head_Lights = 0;
command[10]=0;
}
// Wait for switch to be released
while (Right_Joy_Button == LOW)
{
Right_Joy_Button = digitalRead(7);
}
Send_Data();
}
}
void Check_Pots(){
Right_Joy_X = analogRead(A1);
Right_Joy_X= map(Right_Joy_X, 0, 1024, 0xFF, 0x00);
Right_Joy_Y = analogRead(A0);
Right_Joy_Y= map(Right_Joy_Y, 0, 1024, 0xFF, 0x00);
// Only send data if something has changed, to try an minimise interference
if(Right_Joy_X != Right_Joy_X_Old || Right_Joy_Y != Right_Joy_Y_Old){
command[8]=Right_Joy_X;
command[9]=Right_Joy_Y;
// Send the data
Send_Data();
// Store the sent values
Right_Joy_X_Old=Right_Joy_X;
Right_Joy_Y_Old=Right_Joy_Y;
}
}
// Function to send the data
void Send_Data(){
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
nrf24.send(command, sizeof(command));
nrf24.waitPacketSent();
}
This post has been edited by kutxa on 6 Feb 2017, 23:44
მიმაგრებული სურათი