Post by RC Tractor Guy on Jul 8, 2015 21:52:29 GMT
This code is for a controller with two joysticks and two sliding potentiometers as well as a few push buttons. It still needs some work but it is working for me as is.
// Define variables
int Left_Joy_X = 0x00;
int Left_Joy_Y = 0x00;
int Left_Pot = 0x00;
int Right_Joy_X = 0x00;
int Right_Joy_Y = 0x00;
int Right_Pot = 0x00;
int Left_Joy_X_Old = 0x00;
int Left_Joy_Y_Old = 0x00;
int Left_Pot_Old = 0x00;
int Right_Joy_X_Old = 0x00;
int Right_Joy_Y_Old = 0x00;
int Right_Pot_Old = 0x00;
int Push_Buttons = 0x00;
int Data_Check_Timer = 0;
int DIP1 = LOW;
int DIP2 = LOW;
int DIP3 = LOW;
int DIP4 = LOW;
int DIP5 = LOW;
int Left_Joy_Button = LOW;
int Right_Joy_Button = LOW;
int Menu = 0;
int Indicators = 0;
int Head_Lights = 0;
int Work_Lights = 0;
int Indicators_Old = 0;
int Head_Lights_Old = 0;
int Work_Lights_Old = 0;
#include <SPI.h>
#include "RF24.h"
RF24 radio(8,10);
// Controller Address
const uint64_t pipe = 0xE8E8F0F0E2LL;
/*-----( Declare Variables )-----*/
uint8_t command[16]; // 2 element array of unsigned 8-bit type, holding Joystick readings
void setup()
{
pinMode(2, INPUT_PULLUP); // Dip switch 1
pinMode(3, INPUT_PULLUP); // Dip switch 2
pinMode(4, INPUT_PULLUP); // Dip switch 3
pinMode(5, INPUT_PULLUP); // Dip switch 4
pinMode(6, INPUT_PULLUP); // Dip switch 5
pinMode(7, INPUT_PULLUP); // Left joystick button
pinMode(9, INPUT_PULLUP); // Right joystick button
command[0] = 0x4A;
command[1] = 0x44;
command[2] = 0x38;
command[3] = 0x33;
//command[4]=0;
//command[5]=0;
//command[6]=0;
//command[7]=0;
//command[8]=0;
//command[9]=0;
command[10]=0;
command[11]=0;
//command[12]=0;
command[13]=0;
command[14]=0;
// initialize the serial communication:
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe);
}
void loop() {
Check_Vehicle_ID();
Check_Buttons();
Check_Pots();
Regular_Data_Check();
delay(100);
}
void Check_Vehicle_ID(){
// Check Dip Switches
DIP1=digitalRead(2);
DIP2=digitalRead(3);
DIP3=digitalRead(4);
DIP4=digitalRead(5);
DIP5=digitalRead(6);
if(DIP1==LOW && DIP2==LOW && DIP3==LOW && DIP4==LOW && DIP5==LOW){
command[0] = 0x4A; // J
command[1] = 0x44; // D
command[2] = 0x38; // 8
command[3] = 0x33; // 3
}
if(DIP1==HIGH && DIP2==LOW && DIP3==LOW && DIP4==LOW && DIP5==LOW){
command[0] = 0x4D; // M
command[1] = 0x38; // 8
command[2] = 0x36; // 6
command[3] = 0x38; // 8
}
}
void Check_Buttons(){
Push_Buttons = analogRead(A6); // read the value from the sensor
if (Push_Buttons < 150){ // Button 1
if (command[10] == 0 || command[10] == 2){
command[10] = 1;
}
else {
command[10] = 0;
}
while(Push_Buttons < 150){
Push_Buttons = analogRead(A6);
}
}
if (Push_Buttons > 150 && Push_Buttons < 380){ // Button 2
if(command[11] == 0){
command[11] = 1;
}
else{
command[11] = 0;
}
while(Push_Buttons > 150 && Push_Buttons < 380){
Push_Buttons = analogRead(A6);
}
}
if (Push_Buttons > 380 && Push_Buttons < 640){ // Button 3
if (command[10] == 0 || command[10] == 1){
command[10] = 2;
}
else {
command[10] = 0;
}
while(Push_Buttons > 380 && Push_Buttons < 640){
Push_Buttons = analogRead(A6);
}
}
if (Push_Buttons > 640 && Push_Buttons < 900){ // Button 4
if(command[14] == 0){
command[14] = 1;
}
else{
command[14] = 0;
}
while(Push_Buttons > 640 && Push_Buttons < 650){
Push_Buttons = analogRead(A6);
}
}
// Check Joystick Switches
Left_Joy_Button = digitalRead(7);
Right_Joy_Button = digitalRead(9);
if (Left_Joy_Button==LOW && Right_Joy_Button==LOW)
{
if(command[12]!=3){
command[12]=3; // Turn on Hazard Lights
}
else{
command[12]=0; // Turn off Indicators
}
// Wait for switch to be released
while (Left_Joy_Button == LOW || Right_Joy_Button==LOW)
{
Left_Joy_Button = digitalRead(7);
Right_Joy_Button = digitalRead(9);
}
Send_Data();
}
else if (Right_Joy_Button==LOW)
{
if (command[12] != 1){
command[12]=1; // Command to turn on right indicator
}
else {
command[12]=0; // Turn off Indicators
}
// Wait for switch to be released
while (Right_Joy_Button == LOW)
{
Right_Joy_Button = digitalRead(9);
}
Send_Data();
}
else if (Left_Joy_Button==LOW)
{
if (command[12] != 2){
command[12]=2; // Command to turn on right indicator
}
else {
command[12]=0; // Turn off Indicators
}
// Wait for switch to be released
while (Left_Joy_Button == LOW)
{
Left_Joy_Button = digitalRead(7);
}
Send_Data();
}
}
void Check_Pots(){
Left_Joy_X = analogRead(A3);
Left_Joy_X= map(Left_Joy_X, 0, 1024, 255, 0);
Left_Joy_Y = analogRead(A2);
Left_Joy_Y= map(Left_Joy_Y, 0, 1024, 255, 0);
Left_Pot = analogRead(A4);
Left_Pot= map(Left_Pot, 0, 1024, 255, 0);
Right_Joy_X = analogRead(A1);
Right_Joy_X= map(Right_Joy_X, 0, 1024, 255, 0);
Right_Joy_Y = analogRead(A0);
Right_Joy_Y= map(Right_Joy_Y, 0, 1024, 0, 255);
Right_Pot = analogRead(A5);
Right_Pot= map(Right_Pot, 0, 1024, 255, 0);
if(Left_Joy_X != Left_Joy_X_Old || Left_Joy_Y != Left_Joy_Y_Old || Left_Pot != Left_Pot_Old || Right_Joy_X != Right_Joy_X_Old || Right_Joy_Y != Right_Joy_Y_Old || Right_Pot != Right_Pot_Old){
command[4]=Left_Joy_X;
command[5]=Left_Joy_Y;
command[6]=Left_Pot;
command[7]=Right_Joy_X;
command[8]=Right_Joy_Y;
command[9]=Right_Pot;
Send_Data();
Left_Joy_X_Old=Left_Joy_X;
Left_Joy_Y_Old=Left_Joy_Y;
Left_Pot_Old=Left_Pot;
Right_Joy_X_Old=Right_Joy_X;
Right_Joy_Y_Old=Right_Joy_Y;
Right_Pot_Old=Right_Pot;
}
}
// Function to send the data
void Send_Data(){
radio.write(command, sizeof(command));
Data_Check_Timer = 0;
}
// Send data in regular intervals to prevent tractor loosing control
void Regular_Data_Check(){
Data_Check_Timer=Data_Check_Timer+1;
if(Data_Check_Timer=1024){
Send_Data();
}
}