#include <SPI.h> //this librarys let microcontrollers communicate with each other quickly over short distances(MISO-send data to master from slave)(MOSI-send data from master to peripherals)(SCK-serial clock,clock pulses which synchronize data transmission generated by the master)
#include <RF24.h>
#include <nRF24L01.h> //UNO: MOSI(pin11)MISO(pin12)SCK(pin13)(CE, CSN any pin)
#include <printf.h> //MEGA: MOSI(pin51)MISO(pin50)SCK(pin52)(CE 9, CSN 53, or any pin)
#include <RF24_config.h>
//#include <LiquidCrystal.h> //LCD library: https://arduino-info.wikispaces.com/LCD-Blue-I2C
#include <Wire.h>
#include <LiquidCrystal_I2C.h> //LCD library: http://www.sunfounder.com/wiki/index.php?title=File:LiquidCrystal_I2C.zip
LiquidCrystal_I2C lcd(0x27,20,4); //setting display to 0x27 address; 20 columns, 4 rows; UNO:SDA(A4)SCL(A5); MEGA:SDA(pin20)SCL(pin21)
#define leftStick_x A0
#define leftStick_y A1
#define rightStick_x A3
#define rightStick_y A2
#define motorSpeed_Pot A5
int left_sw = 22;
int right_sw = 24;
int lifting_sw = 44; //LIFTING = 1 -> ACTIVATES leveling potentiometer | Change the pins of the first few switches
int throwing_sw = 45; //THROWING = NEED MORE INFORMATION
int climbing_sw = 43; //CLIMBING = NO NEED?
int hitting_sw = 41; //HITTING = 1 -> ACTIVATES level potentiometer
int STOP_sw = 14; //Emergency STOP; break switch for all motors; MOST IMPORTANT SWITCH! will give all ESCS neutral pulse (a stoping pulse basically)(is need if the joysticks go unstable while its running)
int levelUP_sw = 15;
int levelDOWN_sw = 16;
const int sizeOfArray = 7;
int switchModeArray[] = {left_sw, right_sw, lifting_sw, throwing_sw, climbing_sw, hitting_sw, STOP_sw};
int buttonstateArray[sizeOfArray] = {};
#define CE_PIN 8
#define CSN_PIN 53
RF24 radio(CE_PIN, CSN_PIN);
const uint64_t pipe = 0xE8E8F0F0E1LL;
struct controllerData{
byte rightESC_1;byte rightESC_2;byte rightESC_3;byte rightESC_4;byte rightESC_5;byte rightESC_6;//the right stick; up or down values
byte leftESC_1;byte leftESC_2;byte leftESC_3;byte leftESC_4;byte leftESC_5;byte leftESC_6;
byte motorSpeed_Pot;
bool levelUP_sw;
bool levelDOWN_sw;
bool left_sw;
bool right_sw;
bool lifting_sw;
bool throwing_sw;
bool climbing_sw;
bool hitting_sw;
bool STOP_sw;
}; controllerData data;
void setup(){
radio.begin(); //Begin & Link The Radios
radio.setPALevel(RF24_PA_MAX);
radio.setDataRate(RF24_250KBPS);
radio.openReadingPipe(1,pipe);
//radio.startListening();
lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight
Serial.begin(115200);
Serial.println("Starting");
for (int i=0; i<sizeOfArray; i++){
pinMode(switchModeArray[i], INPUT); //Connect all the switch pins to INPUT
}
for (int i=0; i<sizeOfArray; i++){
pinMode(switchModeArray[i], INPUT_PULLUP); //Active the pullup resistors, gives it a little bit more resistance,sometimes the arduino picks the switches as being pushed when u wave ur hand on top of the AtMEGA chip. this makes it so it hae to be be supplied with 5Volts/HIGH (being pushed) to register a 1
}
//pinMode(leftStick_x, INPUT_PULLUP);
//pinMode(leftStick_y, INPUT_PULLUP);
//pinMode(rightStick_x, INPUT_PULLUP);
//pinMode(rightStick_y, INPUT_PULLUP);
}
//This is a list of global variables needed to use the function i made called "smoothing(axis#,index#)"
//it will read and average the first "smoothingValue" it reads
#define amountToAverage 3 //this is the amount we will read and keep averaging.
#define x amountToAverage
#define numOfSticks 6 //(leftx,lefty,rightx,righy) so 4 axises we need to measure, +the motor speed potentiometer and got room to add one more pot
const int smoothingValue[numOfSticks] = {x,x,x,x,x,x}; //one function to average all axis.
int readings[numOfSticks][x]={}; //2 dimentional array:1st index is the axis#(leftx,lefty,righx,righy); 2nd index says that we want the readings to be as big as the #s we want to average
int readIndex[numOfSticks] = {0,0,0,0,0,0}; //(leftx,lefty,rightx,right) will all be 0 (had to put 4 0s for 4 axis)
int total[numOfSticks] = {0,0,0,0,0,0}; //I didnt know how to set all the elements in an array to one value :/
int average[numOfSticks] = {0,0,0,0,0,0};
int smoothing(int stick, int index){
total[index] = total[index] - readings[index][readIndex[index]]; //Subtract the last reading
readings[index][readIndex[index]] = stick; //read from the sensor,
total[index] = total[index] + readings[index][readIndex[index]]; //add the reading to the total:
readIndex[index] = readIndex[index] + 1; // go to the next position in the array:
if (readIndex[index] >= smoothingValue[index]) { // if we are reaching the end of the array...
readIndex[index] = 0; // ...go to the the beginning:
}
average[index]= total[index] / smoothingValue[index]; // calculate the average:
return(average[index]);
}
void loop(){
int left_x = analogRead(leftStick_x); int right_x = analogRead(rightStick_x); //read the uncalibrated stick
int left_y = analogRead(leftStick_y); int right_y = analogRead(rightStick_y);
int leftStick_sw = digitalRead(leftStick_sw);int rightStick_sw = digitalRead(rightStick_sw);
left_x = smoothing(left_x,0); //Joystick Calibration#1: Stabalize the inition readings
left_y = smoothing(left_y,1); //use the index 0, values for left_x...1 for left_y...etc
right_x = smoothing(right_x,2);
right_y = smoothing(right_y,3);
/////////////////////////////////////(Read All Switches: WHAT MODE IS IT IN?)/////////////////////////////////////////////////////////////////////
for (int i = 0; i < sizeOfArray; i++)
{
buttonstateArray[i] = digitalRead(switchModeArray[i]); //read all the switchbuttons
}
data.left_sw = buttonstateArray[0]; //put them in the packet so we can send them out
data.right_sw = buttonstateArray[1];
data.lifting_sw = buttonstateArray[2];
data.throwing_sw = buttonstateArray[3];
data.climbing_sw = buttonstateArray[4];
data.hitting_sw = buttonstateArray[5];
data.STOP_sw = buttonstateArray[6];
Serial.print("Buttons:");
Serial.print(data.lifting_sw);Serial.print(" ");
Serial.print(data.throwing_sw);Serial.print(" ");
Serial.print(data.climbing_sw);Serial.print(" ");
Serial.print(data.hitting_sw);Serial.print(" ");
Serial.print(" ");
int testStick_LeftX = isreverse(left_x, true);
int testStick_LeftY = isreverse(left_y, false);
int testStick_RightX = isreverse(right_x, false);
int testStick_right = isreverse(right_y, true);
Serial.print("testLeft x: "); Serial.print(testStick_LeftX);
Serial.print(" testLeft y: "); Serial.print(testStick_LeftY);
Serial.print(" | ");
Serial.print("testRight x: "); Serial.print(testStick_RightX);
Serial.print(" testRight y:"); Serial.print(testStick_right);
Serial.println(" *TEST RUNNING*");
//radio.write( &data, sizeof(unsigned long) ); //send all this data to the reciever
}
that is the code, and i have uploaded my schematic on relay#2. below i have uploaded the readings of my 2 joysticks in rest positions.one day i will read the middle position at one value, and the next day its a value that is 10-20 values above or below.
when i press a button, some readings go up a little and some go down a little.
my multimeter reads the voltage to be above 4.9volts when all 4 switches r pressed and 5.04-5,05 volts when no switches r pressed.