I will try and explain this the best I can.
I have a working program where I use an hall effect type joystick which basically has an potentiometer which just give 0-5V(speed) output and a 2 switches which control backwards and forwards and send the data out over an NRF24L01. I've added a kind of fail safe on power up so if the joystick is active it does not do anything.
Now I want to add another button so when you press it you have 5 seconds to active the forward or backwards motion, But while the joystick is active the loop does not time out if not used in 5 seconds.
Which would be the best approach for this ?
This is the code where I would like to place somesort of while loop to send out the data whithin the 5 seconds but still keep sending it out while the switches are on but end the while loop once switch is deactivated.
//############ drvie backwards/lift down function######################
if (REV_DN_DAT_DAT == LOW) {
data.DRV_BCK = 4;
lcd.setCursor(0, 1);
lcd.print("BACKWARD");
}
else {
data.DRV_BCK = 0;
lcd.setCursor(0, 1);
lcd.print(" ");
}
//############ Steer left function######################
if (sl_dat == LOW) {
data.Steer_left = 5;
}
else {
data.Steer_left = 0;
}
//############ steer right function######################
if (sr_dat == LOW) {
data.Steer_right = 6;
}
else {
data.Steer_right = 0;
lcd.setCursor(0, 1);
lcd.print(" ");
}
Just need some pointers and advice, Thanks
This is the full code of my program
#include <Wire.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
#define FWD_UP 3 //forward signal from joystick
#define REV_DN 4 //reverse signal from joystick
#define SL_input 5 //steer left input
#define SR_input 6 //sterr right input
#define Lift_input 7 //left select drive default
#define E_stopin 8
#define Error_led A3
boolean FWD_UP_DAT = 0; //set the forward signal to 0
boolean REV_DN_DAT_DAT = 0;//set backwards siganl to 0
boolean sl_dat = 0; //set steer left signal to 0
boolean sr_dat = 0; //set sterr right to 0
boolean lift_dat = 0; //set lift to 0
byte E_stopped = 0;
const uint64_t pipeOut = 0xE8E8F0F0E1LL; //IMPORTANT: The same as in the receiver
float input_voltage = 0.0;
RF24 radio(9, 10); // select CSN pin
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change:
const long interval = 250;
// The sizeof this struct should not exceed 32 bytes
// This gives us up to 32 8 bits channals
struct MyData {
byte E_stop; //throttle signal
int throttle; //throttle signal
byte Steer_left;//sterr left
byte Steer_right; //Steer right signal
byte DRV_FWD; //drive forwards
byte DRV_BCK; //drive backwards
byte Lift; //left signal
};
MyData data;
void resetData()
{
//This are the start values of each channal
// Throttle is 0 in order to stop the motors
data.E_stop = 0;
data.throttle = 0;
data.Steer_left = 0; //Steer left signal
data.Steer_right = 0;
data.DRV_FWD = 0;
data.DRV_BCK = 0;
data.Lift = 0;
}
void setup()
{
Serial.begin(115200);
lcd.begin(16, 2);
pinMode(Error_led, OUTPUT);
digitalWrite(Error_led, LOW);
pinMode(E_stopin, INPUT_PULLUP);
pinMode(FWD_UP, INPUT_PULLUP);
pinMode( REV_DN, INPUT_PULLUP);
pinMode(SL_input, INPUT_PULLUP);
pinMode(SR_input, INPUT_PULLUP);
pinMode(Lift_input, INPUT_PULLUP);
//########################################################################################################################################
// Detect if any of the buttons are active on power up,Failsafe and do nothing until the fault is rectifed #
//########################################################################################################################################
while ((digitalRead(FWD_UP) == LOW) || (digitalRead( REV_DN) == LOW) || (digitalRead(SR_input) == LOW) || (digitalRead(SL_input) == LOW)) {
Error();
}
radio.begin();
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.openWritingPipe(pipeOut);
resetData();
lcd.clear();
digitalWrite(Error_led, LOW);
}
void loop()
{
E_stopped = digitalRead(E_stopin); //Steer left signal
sl_dat = digitalRead(SL_input); //Steer left signal
sr_dat = digitalRead(SR_input); //Steer right signal
FWD_UP_DAT = digitalRead(FWD_UP); //Drive forward signal
REV_DN_DAT_DAT = digitalRead( REV_DN); //Drive Reverse signal
lift_dat = digitalRead(Lift_input); //Select lift drive default
//############ E-Stop function######################
if ( E_stopped == LOW) {
data.E_stop = 1;
}
else {
data.E_stop = 0;
}
//############ lift/drive function######################
if (lift_dat == LOW) {
data.Lift = 2;
lcd.setCursor(5, 0);
lcd.print("LIFT MODE");
}
else {
data.Lift = 0;
lcd.setCursor(5, 0);
lcd.print("DRIVE MODE ");
}
//############ drvie forwards/lift up function######################
if (FWD_UP_DAT == LOW) {
data.DRV_FWD = 3;
lcd.setCursor(0, 1);
lcd.print("FORWARD");
}
else {
data.DRV_FWD = 0;
lcd.setCursor(0, 1);
lcd.print(" ");
}
//############ drvie backwards/lift down function######################
if (REV_DN_DAT_DAT == LOW) {
data.DRV_BCK = 4;
lcd.setCursor(0, 1);
lcd.print("BACKWARD");
}
else {
data.DRV_BCK = 0;
lcd.setCursor(0, 1);
lcd.print(" ");
}
//############ Steer left function######################
if (sl_dat == LOW) {
data.Steer_left = 5;
}
else {
data.Steer_left = 0;
}
//############ steer right function######################
if (sr_dat == LOW) {
data.Steer_right = 6;
}
else {
data.Steer_right = 0;
lcd.setCursor(0, 1);
lcd.print(" ");
}
//############ Display data on the LCD debugging ###########
lcd.setCursor(0, 0);
lcd.print(data.throttle);
lcd.print(" ");
data.throttle = mapJoystickValues( analogRead(A0), 0, 507, 1023, true );
input_voltage = (data.throttle * 5.0) / 1024.0;
lcd.setCursor(8, 1);
lcd.print(input_voltage);
lcd.print(" V ");
//############ Send data out to RX module ##################
radio.write(&data, sizeof(MyData));
}
/// Returns a corrected value for a joystick position that takes into account
// the values of the outer extents and the middle of the joystick range.
int mapJoystickValues(int val, int lower, int middle, int upper, bool reverse)
{
val = constrain(val, lower, upper);
if ( val < middle )
val = map(val, lower, middle, 0, 1023);
else
val = map(val, middle, upper, 1023, 0);
return ( reverse ? 1023 - val : val );
}
void Error() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
digitalWrite(Error_led, !digitalRead(Error_led));
}
}