/* Cessna172 Switch Panel
*/
//--------------------------------------------------------------------
#include <Joystick.h>
// Arduino input pins
#define parkingbrake_pin 2
#define pitotheat_pin 3
#define strobe_pin 4
#define nav_pin 5
#define taxi_pin 6
#define landing_pin 7
#define beacon_pin 8
#define fuelpump_pin 9
#define avionics_pin 10
#define battery_pin 11
#define alternator_pin 12
#define magoff_pin A0
#define magR_pin A1
#define magL_pin A2
#define magboth_pin A3
#define starter_pin A4
// Joystick button assignements
#define parkingbrake_ON 0
#define parkingbrake_OFF 1
#define pitotheat_ON 2
#define pitotheat_OFF 3
#define strobe_ON 4
#define strobe_OFF 5
#define nav_ON 6
#define nav_OFF 7
#define taxi_ON 8
#define taxi_OFF 9
#define landing_ON 10
#define landing_OFF 11
#define beacon_ON 12
#define beacon_OFF 13
#define fuelpump_ON 14
#define fuelpump_OFF 15
#define avionics_ON 16
#define avionics_OFF 17
#define battery_ON 18
#define battery_OFF 19
#define alternator_ON 20
#define alternator_OFF 21
#define magR_ON 22
#define magR_OFF 23
#define magL_ON 24
#define magL_OFF 25
#define starter_ON 26
#define mag_OFF 27
#define magBOTH_ON 28
boolean parkingbrake_prev;
boolean pitotheat_prev;
boolean strobe_prev;
boolean nav_prev;
boolean taxi_prev;
boolean landing_prev;
boolean beacon_prev;
boolean fuelpump_prev;
boolean avionics_prev;
boolean battery_prev;
boolean alternator_prev;
boolean magoff_prev;
boolean magR_prev;
boolean magL_prev;
boolean magboth_prev;
boolean starter_prev;
boolean temp;
boolean changed;
void setup()
{
// Configure inputs
for (int n = 2; n < 14; n++)
{
pinMode(n, INPUT_PULLUP);
}
pinMode(A0, INPUT_PULLUP);
pinMode(A1, INPUT_PULLUP);
pinMode(A2, INPUT_PULLUP);
pinMode(A3, INPUT_PULLUP);
pinMode(A4, INPUT_PULLUP);
// Initialize Joystick Library
Joystick.begin();
initialise();
}
void loop()
{
changed = 0;
temp = digitalRead(parkingbrake_pin);
if (temp != parkingbrake_prev)
{
changed = 1;
if (temp)
{
Joystick.setButton(parkingbrake_ON, 1);
Joystick.setButton(parkingbrake_OFF, 0);
}
else
{
Joystick.setButton(parkingbrake_ON, 0);
Joystick.setButton(parkingbrake_OFF, 1);
}
parkingbrake_prev = temp;
}
temp = digitalRead(pitotheat_pin);
if (temp != pitotheat_prev)
{
changed = 1;
if (temp)
{
Joystick.setButton(pitotheat_ON, 1);
Joystick.setButton(pitotheat_OFF, 0);
}
else
{
Joystick.setButton(pitotheat_ON, 0);
Joystick.setButton(pitotheat_OFF, 1);
}
pitotheat_prev = temp;
}
temp = digitalRead(strobe_pin);
if (temp != strobe_prev)
{
changed = 1;
if (temp)
{
Joystick.setButton(strobe_ON, 1);
Joystick.setButton(strobe_OFF, 0);
}
else
{
Joystick.setButton(strobe_ON, 0);
Joystick.setButton(strobe_OFF, 1);
}
strobe_prev = temp;
}
temp = digitalRead(nav_pin);
if (temp != nav_prev)
{
changed = 1;
if (temp)
{
Joystick.setButton(nav_ON, 1);
Joystick.setButton(nav_OFF, 0);
}
else
{
Joystick.setButton(nav_ON, 0);
Joystick.setButton(nav_OFF, 1);
}
nav_prev = temp;
}
temp = digitalRead(taxi_pin);
if (temp != taxi_prev)
{
changed = 1;
if (temp)
{
Joystick.setButton(taxi_ON, 1);
Joystick.setButton(taxi_OFF, 0);
}
else
{
Joystick.setButton(taxi_ON, 0);
Joystick.setButton(taxi_OFF, 1);
}
taxi_prev = temp;
}
temp = digitalRead(landing_pin);
if (temp != landing_prev)
{
changed = 1;
if (temp)
{
Joystick.setButton(landing_ON, 1);
Joystick.setButton(landing_OFF, 0);
}
else
{
Joystick.setButton(landing_ON, 0);
Joystick.setButton(landing_OFF, 1);
}
landing_prev = temp;
}
temp = digitalRead(beacon_pin);
if (temp != beacon_prev)
{
changed = 1;
if (temp)
{
Joystick.setButton(beacon_ON, 1);
Joystick.setButton(beacon_OFF, 0);
}
else
{
Joystick.setButton(beacon_ON, 0);
Joystick.setButton(beacon_OFF, 1);
}
beacon_prev = temp;
}
temp = digitalRead(fuelpump_pin);
if (temp != fuelpump_prev)
{
changed = 1;
if (temp)
{
Joystick.setButton(fuelpump_ON, 1);
Joystick.setButton(fuelpump_OFF, 0);
}
else
{
Joystick.setButton(fuelpump_ON, 0);
Joystick.setButton(fuelpump_OFF, 1);
}
fuelpump_prev = temp;
}
temp = digitalRead(avionics_pin);
if (temp != avionics_prev)
{
changed = 1;
if (temp)
{
Joystick.setButton(avionics_ON, 1);
Joystick.setButton(avionics_OFF, 0);
}
else
{
Joystick.setButton(avionics_ON, 0);
Joystick.setButton(avionics_OFF, 1);
}
avionics_prev = temp;
}
temp = digitalRead(battery_pin);
if (temp != battery_prev)
{
changed = 1;
if (temp)
{
Joystick.setButton(battery_ON, 1);
Joystick.setButton(battery_OFF, 0);
}
else
{
Joystick.setButton(battery_ON, 0);
Joystick.setButton(battery_OFF, 1);
}
battery_prev = temp;
}
temp = digitalRead(alternator_pin);
if (temp != alternator_prev)
{
changed = 1;
if (temp)
{
Joystick.setButton(alternator_ON, 1);
Joystick.setButton(alternator_OFF, 0);
}
else
{
Joystick.setButton(alternator_ON, 0);
Joystick.setButton(alternator_OFF, 1);
}
alternator_prev = temp;
}
temp = digitalRead(magoff_pin);
if (temp != magoff_prev)
{
changed = 1;
if (!temp)
{
Joystick.setButton(mag_OFF, 1);
}
else
{
Joystick.setButton(mag_OFF, 0);
}
magoff_prev = temp;
}
temp = digitalRead(magR_pin);
if (temp != magR_prev)
{
changed = 1;
if (!temp)
{
Joystick.setButton(magR_ON, 1);
}
else
{
Joystick.setButton(magR_ON, 0);
}
magR_prev = temp;
}
temp = digitalRead(magL_pin);
if (temp != magL_prev)
{
changed = 1;
if (!temp)
{
Joystick.setButton(magL_ON, 1);
}
else
{
Joystick.setButton(magL_ON, 0);
}
magL_prev = temp;
}
temp = digitalRead(magboth_pin);
if (temp != magboth_prev)
{
changed = 1;
if (!temp)
{
Joystick.setButton(magBOTH_ON, 1);
}
else
{
Joystick.setButton(magBOTH_ON, 0);
}
magboth_prev = temp;
}
temp = digitalRead(starter_pin);
if (temp != starter_prev)
{
changed = 1;
if (!temp)
{
Joystick.setButton(starter_ON, 1);
Joystick.setButton(magBOTH_ON, 1);
}
else
{
Joystick.setButton(starter_ON, 0);
Joystick.setButton(magBOTH_ON, 1);
}
starter_prev = temp;
}
// if (!changed) Joystick.sendState();
delay(40);
}
void initialise()
{
temp = digitalRead(parkingbrake_pin);
if (temp)
{
Joystick.setButton(parkingbrake_ON, 1);
Joystick.setButton(parkingbrake_OFF, 0);
}
else
{
Joystick.setButton(parkingbrake_ON, 0);
Joystick.setButton(parkingbrake_OFF, 1);
}
parkingbrake_prev = temp;
temp = digitalRead(pitotheat_pin);
if (temp)
{
Joystick.setButton(pitotheat_ON, 1);
Joystick.setButton(pitotheat_OFF, 0);
}
else
{
Joystick.setButton(pitotheat_ON, 0);
Joystick.setButton(pitotheat_OFF, 1);
}
pitotheat_prev = temp;
temp = digitalRead(strobe_pin);
if (temp)
{
Joystick.setButton(strobe_ON, 1);
Joystick.setButton(strobe_OFF, 0);
}
else
{
Joystick.setButton(strobe_ON, 0);
Joystick.setButton(strobe_OFF, 1);
}
strobe_prev = temp;
temp = digitalRead(nav_pin);
if (temp)
{
Joystick.setButton(nav_ON, 1);
Joystick.setButton(nav_OFF, 0);
}
else
{
Joystick.setButton(nav_ON, 0);
Joystick.setButton(nav_OFF, 1);
}
nav_prev = temp;
temp = digitalRead(taxi_pin);
if (temp)
{
Joystick.setButton(taxi_ON, 1);
Joystick.setButton(taxi_OFF, 0);
}
else
{
Joystick.setButton(taxi_ON, 0);
Joystick.setButton(taxi_OFF, 1);
}
taxi_prev = temp;
temp = digitalRead(landing_pin);
if (temp)
{
Joystick.setButton(landing_ON, 1);
Joystick.setButton(landing_OFF, 0);
}
else
{
Joystick.setButton(landing_ON, 0);
Joystick.setButton(landing_OFF, 1);
}
landing_prev = temp;
temp = digitalRead(beacon_pin);
if (temp)
{
Joystick.setButton(beacon_ON, 1);
Joystick.setButton(beacon_OFF, 0);
}
else
{
Joystick.setButton(beacon_ON, 0);
Joystick.setButton(beacon_OFF, 1);
}
beacon_prev = temp;
temp = digitalRead(fuelpump_pin);
if (temp)
{
Joystick.setButton(fuelpump_ON, 1);
Joystick.setButton(fuelpump_OFF, 0);
}
else
{
Joystick.setButton(fuelpump_ON, 0);
Joystick.setButton(fuelpump_OFF, 1);
}
fuelpump_prev = temp;
temp = digitalRead(avionics_pin);
if (temp)
{
Joystick.setButton(avionics_ON, 1);
Joystick.setButton(avionics_OFF, 0);
}
else
{
Joystick.setButton(avionics_ON, 0);
Joystick.setButton(avionics_OFF, 1);
}
avionics_prev = temp;
temp = digitalRead(battery_pin);
if (temp)
{
Joystick.setButton(battery_ON, 1);
Joystick.setButton(battery_OFF, 0);
}
else
{
Joystick.setButton(battery_ON, 0);
Joystick.setButton(battery_OFF, 1);
}
battery_prev = temp;
temp = digitalRead(alternator_pin);
if (temp)
{
Joystick.setButton(alternator_ON, 1);
Joystick.setButton(alternator_OFF, 0);
}
else
{
Joystick.setButton(alternator_ON, 0);
Joystick.setButton(alternator_OFF, 1);
}
alternator_prev = temp;
temp = digitalRead(magoff_pin);
if (!temp)
{
Joystick.setButton(mag_OFF, 1);
}
else
{
Joystick.setButton(mag_OFF, 0);
}
magoff_prev = temp;
temp = digitalRead(magR_pin);
if (!temp)
{
Joystick.setButton(magR_ON, 1);
}
else
{
Joystick.setButton(magR_ON, 0);
}
magR_prev = temp;
temp = digitalRead(magL_pin);
if (!temp)
{
Joystick.setButton(magL_ON, 1);
}
else
{
Joystick.setButton(magL_ON, 0);
}
magL_prev = temp;
temp = digitalRead(magboth_pin);
if (!temp)
{
Joystick.setButton(magBOTH_ON, 1);
}
else
{
Joystick.setButton(magBOTH_ON, 0);
}
magboth_prev = temp;
temp = digitalRead(starter_pin);
if (!temp)
{
Joystick.setButton(magBOTH_ON, 1);
Joystick.setButton(starter_ON, 1);
}
else
{
Joystick.setButton(magBOTH_ON, 1);
Joystick.setButton(starter_ON, 0);
}
starter_prev = temp;
}