int pilotLeftPin = 2;
int pilotRightPin = 3;
int pilotSelectPin = 4;
int copilotLeftPin = 5;
int copilotRightPin = 6;
int copilotSelectPin = 7;
int pilotFirstPin;
int pilotSecondPin;
int copilotFirstPin;
int copilotSecondPin;
int pilotCounter = 0;
int copilotCounter = 0;
unsigned long currentTime;
unsigned long previousTime;
int countDelay = 1000;
//int pilotStickValue = 0;
//int copilotStickValue = 0;
int bankNumber; //absolute value of difference between pilotStickValue and copilotStickValue
int bankDifference; //value of copilotStickValue minus pilotStickValue
void setup() {
pinMode(pilotLeftPin, INPUT_PULLUP);
pinMode(pilotRightPin, INPUT_PULLUP);
pinMode(pilotSelectPin, INPUT_PULLUP);
pinMode(copilotLeftPin, INPUT_PULLUP);
pinMode(copilotRightPin, INPUT_PULLUP);
pinMode(copilotSelectPin, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
sidestick();
}
void sidestick() {
static unsigned long currentTime;
static unsigned long previousTime;
static uint8_t pilotFirstPin;
static uint8_t pilotSecondPin;
static uint8_t copilotFirstPin;
static uint8_t copilotSecondPin;
static uint8_t pilotCounter = 0;
static uint8_t copilotCounter = 0;
enum class stickState: uint8_t {
IDLE,
PILOT1ST,
COPILOT1ST,
BANKCALC,
// TRANSMITANDRESET,
};
static stickState currState = stickState::IDLE;
switch (currState) {
case stickState::IDLE:
displayState("IDLE state");
if((digitalRead(pilotLeftPin) == LOW) && (pilotCounter == 0)) {
pilotFirstPin = pilotLeftPin;
pilotSecondPin = pilotRightPin;
previousTime = millis();
currState = stickState::PILOT1ST;
}
else if((digitalRead(pilotRightPin) == LOW) && (pilotCounter == 0)) {
pilotFirstPin = pilotRightPin;
pilotSecondPin = pilotLeftPin;
previousTime = millis();
currState = stickState::PILOT1ST;
}
else if((digitalRead(copilotLeftPin) == LOW) && (copilotCounter == 0)) {
copilotFirstPin = copilotLeftPin;
copilotSecondPin = copilotRightPin;
previousTime = millis();
currState = stickState::COPILOT1ST;
}
else if((digitalRead(copilotRightPin) == LOW) && (copilotCounter == 0)) {
copilotFirstPin = pilotRightPin;
copilotSecondPin = pilotLeftPin;
previousTime = millis();
currState = stickState::COPILOT1ST;
}
break;
case stickState::PILOT1ST:
displayState("PILOT1ST state");
digitalRead(pilotFirstPin);
digitalRead(pilotSecondPin);
digitalRead(pilotSelectPin);
while(digitalRead(pilotFirstPin) == LOW) {
currentTime = millis();
if((currentTime - previousTime) >= countDelay){
pilotCounter = pilotCounter + 1;
if(pilotCounter > 6) {
pilotCounter = 6;
}
Serial.println(pilotCounter);
previousTime = millis();
}
}
while(digitalRead(pilotSecondPin) == LOW) {
currentTime = millis();
if((currentTime - previousTime) >= countDelay){
pilotCounter = pilotCounter - 1;
if(pilotCounter < 1) {
pilotCounter = 1;
}
Serial.println(pilotCounter);
previousTime = currentTime;
}
}
if((digitalRead(pilotSelectPin) == LOW) && (copilotCounter >> 0)) {
currState = stickState::BANKCALC;
}
else if((digitalRead(pilotSelectPin) == LOW) && (copilotCounter == 0)) {
currState = stickState::IDLE;
}
break;
case stickState::COPILOT1ST:
displayState("COPILOT1ST state");
digitalRead(copilotFirstPin);
digitalRead(copilotSecondPin);
digitalRead(copilotSelectPin);
while(digitalRead(copilotFirstPin) == LOW) {
currentTime = millis();
if((currentTime - previousTime) >= countDelay){
copilotCounter = copilotCounter + 1;
if(copilotCounter > 6) {
copilotCounter = 6;
}
Serial.println(copilotCounter);
previousTime = millis();
}
}
while(digitalRead(copilotSecondPin) == LOW) {
currentTime = millis();
if((currentTime - previousTime) >= countDelay){
copilotCounter = copilotCounter - 1;
if(copilotCounter < 1) {
copilotCounter = 1;
}
Serial.println(copilotCounter);
previousTime = currentTime;
}
}
if((digitalRead(copilotSelectPin) == LOW) && (pilotCounter >> 0)) {
currState = stickState::BANKCALC;
}
else if((digitalRead(copilotSelectPin) == LOW) && (pilotCounter == 0)) {
currState = stickState::IDLE;
}
break;
case stickState::BANKCALC:
displayState("BANKCALC state");
if(copilotCounter >> pilotCounter) {
bankDifference = (copilotCounter - pilotCounter);
bankNumber = abs(bankDifference);
Serial.println(bankNumber);
Serial.println("To the RIGHT");
pilotCounter = 0;
copilotCounter = 0;
currState = stickState::IDLE;
}
else if(pilotCounter >> copilotCounter) {
bankDifference = (copilotCounter - pilotCounter);
bankNumber = abs(bankDifference);
Serial.println(bankNumber);
Serial.println("To the LEFT");
pilotCounter = 0;
copilotCounter = 0;
currState = stickState::IDLE;
}
else if(pilotCounter == copilotCounter) {
bankDifference = (copilotCounter - pilotCounter);
bankNumber = abs(bankDifference);
Serial.println(bankNumber);
Serial.println("No Change");
pilotCounter = 0;
copilotCounter = 0;
currState = stickState::IDLE;
}
break;
default:
Serial.println("Default switch case reached - Error");
}
}
void displayState(String currState) {
static String prevState = "";
if (currState != prevState) {
Serial.println(currState);
prevState = currState;
}
}