//How many buttons I'm using, must equal amount of values in following array
#define NUM_BUTTONS 17
// Rotary Encoder Inputs
#define CRSinputCLK 10 // heading
#define CRSinputDT 11
#define HEADinputCLK 13 // course
#define HEADinputDT 16
#define FLCSPDinputCLK 32 // vspeed
#define FLCSPDinputDT 33
#define VSPEEDinputCLK 17 // alt select
#define VSPEEDinputDT 18
#define ALTSELinputCLK 19 // flc change
#define ALTSELinputDT 20
//Which pins I have attached to my buttons
int buttonList[NUM_BUTTONS] = {0,1,2,3,4,5,6,7,8,9,12,21,22,23,24,25,31};
int CRScounter = 0;
int CRScurrentStateCLK;
int CRSpreviousStateCLK;
int HEADcounter = 0;
int HEADcurrentStateCLK;
int HEADpreviousStateCLK;
int FLCSPDcounter = 0;
int FLCSPDcurrentStateCLK;
int FLCSPDpreviousStateCLK;
int VSPEEDcounter = 0;
int VSPEEDcurrentStateCLK;
int VSPEEDpreviousStateCLK;
int ALTSELcounter = 0;
int ALTSELcurrentStateCLK;
int ALTSELpreviousStateCLK;
String CRSencdir ="";
String HEADencdir ="";
String FLCSPDencdir ="";
String VSPEEDencdir ="";
String ALTSELencdir ="";
//Led intensity, so super bright LEDS aren't shining in our eyes
#define INTENSITY 200
void setup() {
//This makes it so the states are sent by us manually
Joystick.useManualSend(true);
//Declare button pins as input with the internal pullup resistor on
for (int i = 0; i < NUM_BUTTONS; i++) {
pinMode(buttonList[i], INPUT_PULLUP);
}
//Declare our LED pins as outputs
pinMode(26, OUTPUT);
pinMode(27, OUTPUT);
pinMode(28, OUTPUT);
pinMode(29, OUTPUT);
pinMode(30, OUTPUT);
// Set encoder pins as inputs
pinMode (CRSinputCLK,INPUT);
pinMode (CRSinputDT,INPUT);
pinMode (HEADinputCLK,INPUT);
pinMode (HEADinputDT,INPUT);
pinMode (FLCSPDinputCLK,INPUT);
pinMode (FLCSPDinputDT,INPUT);
pinMode (VSPEEDinputCLK,INPUT);
pinMode (VSPEEDinputDT,INPUT);
pinMode (ALTSELinputCLK,INPUT);
pinMode (ALTSELinputDT,INPUT);
// Setup Serial Monitor
Serial.begin (9600);
// Read the initial state of inputCLK
// Assign to previousStateCLK variable
CRSpreviousStateCLK = digitalRead(CRSinputCLK);
HEADpreviousStateCLK = digitalRead(HEADinputCLK);
FLCSPDpreviousStateCLK = digitalRead(FLCSPDinputCLK);
VSPEEDpreviousStateCLK = digitalRead(VSPEEDinputCLK);
ALTSELpreviousStateCLK = digitalRead(ALTSELinputCLK);
}
void loop() {
//Not applicable Read our analogue pots
//Removed because NA Remember that the analogue pin numbers are different than the digital ones!
//Removed because not applicable Joystick.sliderLeft(analogRead(7));
//Removed because not applicable Joystick.sliderRight(analogRead(8));
//Read our button states
for (int i = 0; i < NUM_BUTTONS; i++) {
if (digitalRead(buttonList[i]) == HIGH) { //Check to see if pin is HIGH
Joystick.button(i + 1, 0); //If pin is HIGH, button isn't pressed, so send 0
} else {
Joystick.button(i + 1, 1); //If pin is LOW, button is pressed, so send 1
}
}
//Special case for LED status lights
//Check status of button and change LED accordingly
if (digitalRead(21) == LOW) //Check if button is pressed/switch flipped
analogWrite(26, INTENSITY); //Set corresponding LED pin to intensity level
else
analogWrite(26, 0); //Otherwise turn off
if (digitalRead(22) == LOW) //Same for other pins
analogWrite(27, INTENSITY);
else
analogWrite(27, 0);
if (digitalRead(23) == LOW)
analogWrite(28, INTENSITY);
else
analogWrite(28, 0);
if (digitalRead(24) == LOW)
analogWrite(29, INTENSITY);
else
analogWrite(29, 0);
if (digitalRead(25) == LOW)
analogWrite(30, INTENSITY);
else
analogWrite(30, 0);
Joystick.send_now(); //Send control states
delay(5); //Slow things down a bit
// THE HEADING BUG
// Read the current state of CRSinputCLK
CRScurrentStateCLK = digitalRead(CRSinputCLK);
// If the previous and the current state of the inputCLK are different then a pulse has occured
if (CRScurrentStateCLK != CRSpreviousStateCLK){
// If the inputDT state is different than the inputCLK state then
// the encoder is rotating counterclockwise
if (digitalRead(CRSinputDT) != CRScurrentStateCLK) {
CRScounter --;
CRSencdir ="CCW";
} else {
// Encoder is rotating clockwise
CRScounter ++;
CRSencdir ="CW";
}
Serial.print("Heading Direction: ");
Serial.print(CRSencdir);
Serial.print(" -- Value: ");
Serial.println(CRScounter);
}
// Update previousStateCLK with the current state
CRSpreviousStateCLK = CRScurrentStateCLK;
// THE COURSE BUG
// Read the current state of HEADinputCLK
HEADcurrentStateCLK = digitalRead(HEADinputCLK);
// If the previous and the current state of the inputCLK are different then a pulse has occured
if (HEADcurrentStateCLK != HEADpreviousStateCLK){
// If the inputDT state is different than the inputCLK state then
// the encoder is rotating counterclockwise
if (digitalRead(HEADinputDT) != HEADcurrentStateCLK) {
HEADcounter ++;
HEADencdir ="CW";
} else {
// Encoder is rotating clockwise
HEADcounter --;
HEADencdir ="CCW";
}
Serial.print("Course Direction: ");
Serial.print(HEADencdir);
Serial.print(" -- Value: ");
Serial.println(HEADcounter);
}
// Update previousStateCLK with the current state
HEADpreviousStateCLK = HEADcurrentStateCLK;
// THE VSPEED BUG
// Read the current state of FLCSPDinputCLK
FLCSPDcurrentStateCLK = digitalRead(FLCSPDinputCLK);
// If the previous and the current state of the inputCLK are different then a pulse has occured
if (FLCSPDcurrentStateCLK != FLCSPDpreviousStateCLK){
// If the inputDT state is different than the inputCLK state then
// the encoder is rotating counterclockwise
if (digitalRead(FLCSPDinputDT) != FLCSPDcurrentStateCLK) {
FLCSPDcounter --;
FLCSPDencdir ="CCW";
} else {
// Encoder is rotating clockwise
FLCSPDcounter ++;
FLCSPDencdir ="CW";
}
Serial.print("Vspeed Direction: ");
Serial.print(FLCSPDencdir);
Serial.print(" -- Value: ");
Serial.println(FLCSPDcounter);
}
// Update previousStateCLK with the current state
FLCSPDpreviousStateCLK = FLCSPDcurrentStateCLK;
// THE ALTITUDE SELECT KNOB
// Read the current state of VSPEEDinputCLK
VSPEEDcurrentStateCLK = digitalRead(VSPEEDinputCLK);
// If the previous and the current state of the inputCLK are different then a pulse has occured
if (VSPEEDcurrentStateCLK != VSPEEDpreviousStateCLK){
// If the inputDT state is different than the inputCLK state then
// the encoder is rotating counterclockwise
if (digitalRead(VSPEEDinputDT) != VSPEEDcurrentStateCLK) {
VSPEEDcounter --;
VSPEEDencdir ="CCW";
} else {
// Encoder is rotating clockwise
VSPEEDcounter ++;
VSPEEDencdir ="CW";
}
Serial.print("Alt Select Direction: ");
Serial.print(VSPEEDencdir);
Serial.print(" -- Value: ");
Serial.println(VSPEEDcounter);
}
// Update previousStateCLK with the current state
VSPEEDpreviousStateCLK = VSPEEDcurrentStateCLK;
// THE FLCSPEED Knob
// Read the current state of ALTSELinputCLK
ALTSELcurrentStateCLK = digitalRead(ALTSELinputCLK);
// If the previous and the current state of the inputCLK are different then a pulse has occured
if (ALTSELcurrentStateCLK != ALTSELpreviousStateCLK){
// If the inputDT state is different than the inputCLK state then
// the encoder is rotating counterclockwise
if (digitalRead(ALTSELinputDT) != ALTSELcurrentStateCLK) {
ALTSELcounter --;
ALTSELencdir ="CCW";
} else {
// Encoder is rotating clockwise
ALTSELcounter ++;
ALTSELencdir ="CW";
}
Serial.print("FLCSpeed Direction: ");
Serial.print(ALTSELencdir);
Serial.print(" -- Value: ");
Serial.println(ALTSELcounter);
}
// Update previousStateCLK with the current state
ALTSELpreviousStateCLK = ALTSELcurrentStateCLK;