/*
Programm: D.S.P Right Console
Author: CMD_B34R
Date: 15 Sep 2016
This programm has been written to allow for keyboard emulation via switches, buttons, and rotary encoders.
An Arduino Pro Micro (clone) and multipule MCP23017(s) IC(s) are being used to allow for I/O port expantion.
*/
/*
THIS SECTION WILL DETAIL BUTTION / PIN LAYOUT FOR BUTTON TRACKING (TRACE MATRIX)
MCP # PIN # | PROGRAMM FUNCTION | IN/OUT | SWITCH TYPE | KEYBOARD CHAR
MCP 1 PIN 0 | FLOOD LIGHTS ON | INPUT | TOGGLE SWITCH | a
MCP 1 PIN 1 | FLOOD LIGHTS OFF | INPUT | TOGGLE SWITCH | a
MCP 1 PIN 2 | ORBIT LINES TOGGLE | INPUT | TOGGLE SWITCH | b
MCP 1 PIN 3 | ORBIT LINES TOGGLE | INPUT | TOGGLE SWITCH | b
MCP 1 PIN 4 | ROTATION ADJ TOGGLE | INPUT | TOGGLE SWITCH | c
MCP 1 PIN 5 | ROTATION ADJ TOGGLE | INPUT | TOGGLE SWITCH | c
MCP 1 PIN 6 | LANDING GEAR UP | INPUT | TOGGLE SWITCH | d
MCP 1 PIN 7 | LANDING GEAR DOWN | INPUT | TOGGLE SWITCH | d
MCP 1 PIN 8 | CARGO SCOOP CLOSE | INPUT | TOGGLE SWITCH | e
MCP 1 PIN 9 | CARGO SCOOP OPEN | INPUT | TOGGLE SWITCH | e
MCP 1 PIN 10 | CARGO EJECT DUMP | INPUT | TOGGLE SWITCH | f
MCP 1 PIN 10 | CARGO EJECT DUMP | INPUT | TOGGLE SWITCH | f <<<= tie switch 11 into switch 10
MCP 1 PIN 11 | RADAR INCEASE | INPUT | PUSH BUTTON | g
MCP 1 PIN 12 | RADAR DECREASE | INPUT | PUSH BUTTON | h
MCP 1 PIN 13 | PTT | INPUT | PUSH BUTTON | i
MCP 1 PIN 14 | ABORT | INPUT | PUSH BUTTON | j
MCP 1 PIN 15 | EJECT | INPUT | PUSH BUTTON | k
MCP 2 PIN 0 | INDICATION LED | OUTPUT | N/A | N/A
MCP 2 PIN 1 | INDICATION LED | OUTPUT | N/A | N/A
MCP 2 PIN 2 | INDICATION LED | OUTPUT | N/A | N/A
MCP 2 PIN 3 | INDICATION LED | OUTPUT | N/A | N/A
MCP 2 PIN 4 | INDICATION LED | OUTPUT | N/A | N/A
MCP 2 PIN 5 | INDICATION LED | OUTPUT | N/A | N/A
MCP 2 PIN 6 | INDICATION LED | OUTPUT | N/A | N/A
MCP 2 PIN 7 | INDICATION LED | OUTPUT | N/A | N/A
*/
#include <Keyboard.h> // This lib will handdle the keyboard emulation
#include <Wire.h> // This lib will handdle the I2C communication
#include "Adafruit_MCP23017.h" // This lib will handdle the MCP23017
Adafruit_MCP23017 mcp1; // defines MCP23017 IC 1 (Per the "Adafruit_MCP23017.h" lib)
Adafruit_MCP23017 mcp2; // defines MCP23017 IC 2 (Per the "Adafruit_MCP23017.h" lib)
int led; // a variable to hold the id of the LED that will be toggled
int MCP1Array[17] = {}; // an array to hold the values from the 1st MC23017
int ALEDArray[] = {1,1,1,0,1,1,0,0}; // an array to hold the state A values for the 2nd MC23017 (Senario A aka starting state)
int BLEDArray[] = {0,0,0,1,0,0,1,1}; // an array to hold the state B values for the 2nd MC23017 (Senario B aka alt state)
int CLEDArray[] = {1,1,1,0,1,1,0,0}; // an array to hold the Current LED State for the 2nd MC23017 (starting values listed)
char KeyArray1[] = "abcdefghijklmopq"; // an array to hold the keystoke char(s)
void setup()
{
Serial.begin(9600); // Start serial com for debuging
Keyboard.begin(); // Starts the keyboard emulation
mcp1.begin(0); // Starts / IDs 1st MCP23017 Chip with Address 0
mcp2.begin(1); // Starts / IDs 2nd MCP23017 Chip with Address 1
for(int i = 0; i<15; i++)
{
mcp1.pinMode(i, INPUT); // Sets pins 1-16 on the MCP1 as an input
mcp1.pullUp(i, HIGH); // Turn on a 100K pullup internally
}
for(int i = 0; i<8; i++)
{
mcp2.pinMode(i, OUTPUT); // Sets pins 1-9 on the MCP2 as an output
}
}
void loop()
{
for (int PIN = 0; PIN<=15; PIN++) // for loop to investigate the state of the 1st MCP23017 chip pins 0 to 15
{
if(MCP1Array[PIN] != mcp1.digitalRead(PIN)) // if the MCP1Array dose NOT = the acutal (read) state of the MCP23017 then...
{
MCP1Array[PIN]= mcp1.digitalRead(PIN); // update the array with the current value(s)
if (MCP1Array[PIN]== 0) // If (PIN) == 0 then the button at that pin location was pressed/toggled...
{
char k = KeyArray1[PIN]; // char k will = the char value (at position [PIN]) from KeyArray1
Keyboard.write(k); // and then keyboard.write the value (at position [PIN]) from KeyArray1
Serial.println (""); // used for debuging
Serial.print ("You pressed the button at PIN # "); // used for debuging
Serial.println (PIN); // used for debuging
if (PIN<=6) // if the button was pressed at PIN# 0 to PIN# 6 then...
{
if ( PIN == 0) // if PIN location was PIN # 0 then...
{
led = 0
Serial.print ("PIN was == 0")
}
else if ( PIN % 2 == 0) // if PIN location was is even # pin then... (pin / 2 should have no remander if PIN was even)
{
led = (PIN/2); // led = pin /2 (a variable to hold the id of the LED that will be toggled)
Serial.println ("PIN was even"); // used for debuging
}
else if ( PIN%2 !=0) // if PIN location was is odd # pin then...
{
led = ((PIN - 1) / 2); // then ((pin -1) / 2) = led (a variable to hold the id of the LED that will be toggled)
Serial.println ("PIN was odd"); // used for debuging
}
Serial.print ("led ="); // used for debuging
Serial.println (led); // used for debuging
if (CLEDArray[led] == ALEDArray[led]) // if CLEDArray index posision [led] == ALEDArray index position [led] then...
{
CLEDArray[led] = BLEDArray[led]; // then toggle CLEDArray index led from ALEDArray to BLEDArray
}
else // if CLEDArray index posision [led] == BLEDArray index position [led] then...
{
CLEDArray[led] = ALEDArray[led]; // toggle CLEDArray index [led] from BLEDArray to ALEDArray
}
Serial.print("This is the state of the CLEDArray: "); // used for debuging
for (int L=0; L<=7; L++) // used for debuging
{
Serial.print(CLEDArray[L]); // used for debuging
Serial.print(" |"); // used for debuging
}
mcp2.digitalWrite(led,CLEDArray[led]); // This will write the values from the CLEDArray index [led] as an output to the 2nd MCP 23017 Chip turning on or off the LED attached to that pin (the 2nd mcp chip is used as a LED driver)
}
if (PIN>=7) // if the button was pressed at PIN# 7 to PIN# 16 then...
{
switch (PIN) // This switch case will handle the LEDs that require "Special" conditions
{
case 7: // if pin 7 was pressed turn led 4 on and led 3 off and write the value to the 2nd MCP23017 chip
CLEDArray[4] = 1;
mcp2.digitalWrite(4, CLEDArray[4]);
CLEDArray[3] = 0;
mcp2.digitalWrite(3, CLEDArray[3]);
Serial.print (CLEDArray[4]); // used for debugging
Serial.print (" "); // used for debugging
Serial.println (CLEDArray[3]); // used for debugging
break;
case 8: // if pin 9 was pressed turn led 6 on and led 5 off and write the value to the 2nd MCP23017 chip
CLEDArray[6] = 0;
mcp2.digitalWrite(6, CLEDArray[6]);
CLEDArray[5] = 1;
mcp2.digitalWrite(5, CLEDArray[5]);
Serial.print (CLEDArray[5]); // used for debugging
Serial.print (" "); // used for debugging
Serial.print (CLEDArray[6]); // used for debugging
break;
case 9: // if pin 9 was pressed turn led 6 on and led 5 off and write the value to the 2nd MCP23017 chip
CLEDArray[6] = 1;
mcp2.digitalWrite(6, CLEDArray[6]);
CLEDArray[5] = 0;
mcp2.digitalWrite(5, CLEDArray[5]);
Serial.print (CLEDArray[5]); // used for debugging
Serial.print (" "); // used for debugging
Serial.print (CLEDArray[6]); // used for debugging
break;
case 10 : // if pin 11 was pressed blink led 7 and write the value to the 2nd MCP23017 chip
CLEDArray[7] = 1;
mcp2.digitalWrite(7, CLEDArray[7]);
Serial.println(CLEDArray[7]); // used for debugging
delay (1000);
CLEDArray[7] = 0;
mcp2.digitalWrite(7, CLEDArray[7]);
Serial.println(CLEDArray[7]); // used for debugging
break;
}
}
}
}
}
}