Here is the code for the Master: (Don’t know why it is block formatting some of the code)
//MASTER (RECEIVE) ARDUINO COM 7
#include<BitsAndDroidsFlightConnector.h>
#include <Wire.h>
#define TAXI_LIGHTS 0x01
#define AP_MASTER_ON 0x02
#define STALL_WARNING 0x03
#define BEACON_LIGHTS 0x04
#define LANDING_LIGHTS 0x05
#define NAV1_WHOLE_INC 0x33
#define NAV1_WHOLE_DEC 0x34
#define NAV1_FRACT_INC 0x35
#define NAV1_FRACT_DEC 0x36
#define HEADING_BUG_INC 0x37
#define HEADING_BUG_DEC 0x38
//For all the other Arduinos
BitsAndDroidsFlightConnector connector(false);
// data type for button
struct Button {
uint8_t gpioSw;
uint8_t lastStateSw;
};
//data type for rotary
struct Rotary {
uint8_t gpioClk;
uint8_t gpioDt;
uint8_t lastStateClk;
uint8_t lastStateDt;
Button button;
};
bool NavMega = true;
//Just to keep track of things and in case I want to sequentially access
const uint8_t data_input[5] = {
TAXI_LIGHTS,
AP_MASTER_ON,
STALL_WARNING,
BEACON_LIGHTS,
LANDING_LIGHTS,
};
const uint8_t action_output[6] = {
NAV1_WHOLE_INC,
NAV1_WHOLE_DEC,
NAV1_FRACT_INC,
NAV1_FRACT_DEC,
HEADING_BUG_INC,
HEADING_BUG_DEC,
};
#define ROTARIES 1
Rotary rotaries[ROTARIES];
void setupRotary(Rotary *cur, uint8_t gpioClk, uint8_t gpioDt , uint8_t gpioSw) {
pinMode(gpioClk, INPUT_PULLUP);
pinMode(gpioDt, INPUT_PULLUP);
pinMode(gpioSw, INPUT_PULLUP);
cur->gpioClk = gpioClk;
cur->gpioDt = gpioDt;
cur->button.gpioSw = gpioSw;
cur->button.lastStateSw = LOW;
}
void setup() {
// Start the I2C Bus as Master
Wire.begin();
//If you’re using a Leonardo / Pro-micro this line can be left out.
Serial.begin(115200);
//If you notice some instability gradually increase this value with increments of 5.
Serial.setTimeout(15);
//Serial Monitor
//Serial.begin(9600);
// rotaries
setupRotary(&rotaries[0], 13, 12, 11);
}
void loop() {
connector.dataHandling();
loopRotaries();
loopVariables();
}
void transmit(uint8_t x){
Wire.beginTransmission(9); // transmit to device #9
Wire.write(x); // sends x
Wire.endTransmission(); // stop transmitting
}
void loopVariables(){
if (connector.getLightTaxiOn())
transmit(TAXI_LIGHTS);
if (connector.getLightBeaconOn())
transmit(BEACON_LIGHTS);
if (connector.getLightLandingOn())
transmit(LANDING_LIGHTS);
}
void loopRotaries() {
Rotary *cur;
for (int i = 0; i < ROTARIES; i++) {
cur = &rotaries[i];
// first, check state
int currentStateClk = !digitalRead(cur->gpioClk);
int currentStateDt = !digitalRead(cur->gpioDt);
int currentStateSw = digitalRead(cur->button.gpioSw);
delay(1);
if (currentStateDt != cur->lastStateDt) {
// check for change
cur->lastStateDt = currentStateDt;
}
if (currentStateClk != cur->lastStateClk) {
// check for change
if (currentStateClk == 1) {
if (cur->lastStateDt == 0) {
// turned clockwise
//Serial.println("Clockwise");
if (NavMega)
transmit(NAV1_WHOLE_INC);
else
transmit(NAV1_FRACT_INC);
} else {
// turned counter-clockwise
//Serial.println("Counterclockwise");
if (NavMega)
transmit(NAV1_WHOLE_DEC);
else
transmit(NAV1_FRACT_DEC);
}
}
cur->lastStateClk = currentStateClk;
}
if (currentStateSw != cur->button.lastStateSw && currentStateSw == HIGH) {
NavMega = !NavMega;
}
cur->button.lastStateSw = currentStateSw;
}
}