In this post, snippets from the Remote Control Unit. Original runs to 1149 lines, so I won't be posting much of that.
Pretty sure it's not a problem with the RCU, but including it for the sake of completion.
Pre-setup
//WIRELESS
#include <SPI.h>
#include <RF24.h>
const byte thisSlaveAddress[5] = {'R','x','A','A','A'};
RF24 radio(9,10); // first is pin number for CE pin, 2nd is for CSN pin
//int sendArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} ; // this must match dataToSend in the TX. name of array to be sent
// used to send data
int sendArray[10]; // Hopefully creates an array full of zero's.
int receivedArray[2]; // used to store data received.
bool rslt = false; // used to store success or fail of wireless transmission.
// Global, because is used in menu too.
bool sent = false; // Same as above, but used to mark when a transmission has been sent.
//for time delay on wireless
unsigned long prevMillisW = 0;
int intervalMillisW = 1000;
// MENU
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Delay ints
unsigned long prevMillis = 0;
int intervalMillis = 0;
int sentTrue = 0;
int sentFalse = 0; // these two are used in the wireless conenction, to count how many
// missed and received pings it's god
// used to see what level the menu's at.
int Menu = 100;
bool newLoop = true; // used to make the program write some things only the first time they're created
Setup
void setup()
{
lcd.begin(20, 4); // start the library
radio.begin(); // starts radio
radio.openWritingPipe(thisSlaveAddress);
bool newData = false; // used to print new data if true.
// Modifications to increase reliability
radio.setDataRate( RF24_250KBPS );
//radio.setPALevel (RF24_PA_MAX);
radio.setRetries (10,15);
radio.enableAckPayload();
radio.enableDynamicPayloads();
Serial.begin(9600); // for debugging purposes only
}
Loop
void loop()
{
// below just for bug-testing the ack program
if (receivedArray[0] != 0)
{
Serial.println(receivedArray[0]);
}
// LOADS MENU SCRIPT, IF PREVIOUS LOOP DIDN'T ASN'T FOR DELAY
if ((millis() - prevMillis) >= intervalMillis)
{
menu();
}
// LOADS WIRELESS SCRIPT, ONCE PER SECOND. (THIS MAY END UP BEING SUBJECT TO CHANGE)
if ((millis() - prevMillisW) >= intervalMillisW)
{
wireless();
prevMillisW = millis();
}
if (radio.isAckPayloadAvailable())
{
radio.read(&receivedArray, sizeof(receivedArray));
}
}
possibly relevant Functions
void wireless()
{
rslt = radio.write(&sendArray, sizeof(sendArray));
sent = true;
}
void firstRun(String _tempText, String _tempText2, String _tempText3, String _tempText4) // first time a menu runs, prints the text. This one is for menu - single line, no printed variables.
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(_tempText);
lcd.setCursor(0,1);
lcd.print(_tempText2);
lcd.setCursor(0,2);
lcd.print(_tempText3);
lcd.setCursor(0,3);
lcd.print(_tempText4);
lcd_key = 5; // sets the 'key pressed' to 'none', so it doesnt set off other keys
// for this cycle
newLoop = false;
//delay(400);
}
int arrowKey(int _menu) // short code for navigating to new menu location
{
int newMenu = _menu;
newLoop = true;
//delay (500); // this delay is needed so you don't cycle through every menu option
// going down or up.
// experimental code below, to replay the 'delay'. Tested. Works.
// Global variables can be modified in functions.
prevMillis = millis();
intervalMillis = 500;
return newMenu;
}
Menu function snippets - First part, as well as part of menu that deals with the remote program.
Most relevant part is at the end - everything where Menu == 420.
int menu()
{
Thrust = analogRead(slidingSwitch);
Thrust = map(Thrust, 0, 1024, 0, 9);
prevMillis = millis();
intervalMillis = 10;
// read the buttons
adc_key_in = analogRead(keyPad); // read the value from the sensor
if (adc_key_in > 1000)
{
lcd_key = 5; // None
}
if (adc_key_in < 750)
{
lcd_key = 4; // Select
}
if (adc_key_in < 520)
{
lcd_key = 0; // Right
}
if (adc_key_in < 350)
{
lcd_key = 2; // Down
}
if (adc_key_in < 200)
{
lcd_key = 1; // Up
}
if (adc_key_in < 100)
{
lcd_key = 3; // Left
}
// 0 = Right 1 = Up 2 = Down 3 = Left 4 = Select 5 = None
// Root menu 0 - Callibrate
if (Menu == 100)
{
if (newLoop == true) // if this is the first loop in here
{
firstRun("Wireless Connection", "Status", "", "");
}
if (lcd_key == 2)
{
// what to do if button is Down
Menu = arrowKey(200);
}
if (lcd_key == 0)
{
// what to do if button is Right
Menu = arrowKey(110);
}
}
(Menu 110 to 320 cut)
if (Menu == 410) // Choose a remote program to activate
{
if (newLoop == true)
{
firstRun("Pre-Programmed 1 N/A", "Pre-Programmed 2 N/A", "Pre-Programmed 3 N/A", "Self-Programmed N/A");
lcd.setCursor(0,0);
lcd.blink();
sendArray[0] = 0; // resets to zero, in case it misses it earlier
sendArray[1] = 0;
}
prevMillis = millis();
intervalMillis = 200;
sent = false;
(cut 'if press left, up or right, down' if statements)
if (lcd_key == 4) // select
{
sendArray[0] = 6; // tells it ot execture program
sendArray[1] = 1; // tells it which program to execute (the first)
Menu = arrowKey(420);
Wait2 = ((Ten * 10) + Digit);
Serial.println(Wait2);
}
}
(cut menus 411, 412, 413)
if (Menu == 420) // Choose a remote program to activate
{
if (sent == true)
{
sendArray[0] = 0; // This line prevents the RU from trying to re-activate the same program every time
// it receives the wireless array.
}
if (newLoop == true)
{
firstRun("Program Activated", "Press '<' to cancel", "or wait till end", "");
lcd.setCursor(0,0);
lcd.noBlink();
intervalMillisW = 100;
}
if (receivedArray[0] == 1) // If it's received a 'commandline complete' signal from the car
{
lcd.setCursor(0,3);
lcd.print("Line Fin - Next Line");
}
if (receivedArray[0] == 0) // resets line
{
lcd.setCursor(0,3);
lcd.print(" ");
}
if (receivedArray[0] == 2) // Command Line has Ended
{
Menu = arrowKey(410);
lcd.setCursor(0,3);
lcd.print("Should Have Ended ");
intervalMillisW = 1000;
}
if (lcd_key == 3)// left
{
Menu = arrowKey(410);
sendArray[1] = 404;
intervalMillisW = 1000;
}
}