My set up,
I have a Teensy 3.1 on a sparkfun adapater shield (https://www.sparkfun.com/products/13288) with a Centipede I2C I/O Shiled on top, (http://macetech.com/store/index.php?main_page=product_info&products_id=23) I have 64 switch inputs going to the Centipede and 13 Switch inputs to the Teensy. All works perfect for about 4-8mins then the Teensy freezes up an no more switch inputs work. WIN7 device manager shows the Teensy still attached. If I cycle the power to the Teensy by unplugging the USB, it works fine again for another 4-8mins. Below is the code installed on the Teensy. Not sure if it will help. Thought or ideas appreciated.
Rob
I have a Teensy 3.1 on a sparkfun adapater shield (https://www.sparkfun.com/products/13288) with a Centipede I2C I/O Shiled on top, (http://macetech.com/store/index.php?main_page=product_info&products_id=23) I have 64 switch inputs going to the Centipede and 13 Switch inputs to the Teensy. All works perfect for about 4-8mins then the Teensy freezes up an no more switch inputs work. WIN7 device manager shows the Teensy still attached. If I cycle the power to the Teensy by unplugging the USB, it works fine again for another 4-8mins. Below is the code installed on the Teensy. Not sure if it will help. Thought or ideas appreciated.
Rob
Code:
//NAV 1 & 2, COM 1 & 2, Transponder/ATC Radio code
#include <Wire.h>
#include <Centipede.h>
Centipede CS;
//Transponder Inputs
const int digit1A = 55;
const int digit1B = 54;
const int digit1C = 53;
const int digit2A = 52;
const int digit2B = 51;
const int digit2C = 50;
const int digit3A = 49;
const int digit3B = 48;
const int digit3C = 56;
const int digit4A = 57;
const int digit4B = 58;
const int digit4C = 59;
const int ident = 60;
const int xpdrON = 61;
//NAV1 inputs all CS inputs
const int tensA = 2;
const int unitsA = 12;
const int unitsB = 3;
const int unitsC = 11;
const int unitsD = 4;
const int unitsE = 10;
const int pointTensA = 5;
const int pointTensB = 9;
const int pointTensC = 6;
const int pointTensD = 8;
const int pointTensE = 7;
const int pointHundredsA = 24; //Teensy Input
//NAV2 inputs all Teensy inputs
const int tens2A = 7;
const int units2A = 8;
const int units2B = 9;
const int units2C = 10;
const int units2D = 11;
const int units2E = 12;
const int pointTens2A = 1;
const int pointTens2B = 2;
const int pointTens2C = 3;
const int pointTens2D = 4;
const int pointTens2E = 5;
const int pointHundreds2A = 6;
//COM 1 inputs CS inputs
const int CtensA = 47;
const int CtensB = 32;
const int CtensC = 46;
const int CunitsA = 45;
const int CunitsB = 34;
const int CunitsC = 44;
const int CunitsD = 35;
const int CunitsE = 43;
const int CpointTensA = 36;
const int CpointTensB = 42;
const int CpointTensC = 37;
const int CpointTensD = 41;
const int CpointTensE = 38;
const int CpointHundredsA = 40;
const int CpointHundredsB = 39;
const int CpointHundredsC = 31;
const int CpointHundredsD = 16;
const int CpointHundredsE = 30;
//COM 2 inputs CS inputs
const int C2tensA = 17;
const int C2tensB = 29;
const int C2tensC = 18;
const int C2unitsA = 28;
const int C2unitsB = 19;
const int C2unitsC = 27;
const int C2unitsD = 20;
const int C2unitsE = 26;
const int C2pointTensA = 21;
const int C2pointTensB = 25;
const int C2pointTensC = 22;
const int C2pointTensD = 24;
const int C2pointTensE = 23;
const int C2pointHundredsA = 15;
const int C2pointHundredsB = 0;
const int C2pointHundredsC = 14;
const int C2pointHundredsD = 1;
const int C2pointHundredsE = 13;
FlightSimInteger NavFrequencyHz;
FlightSimInteger Nav2FrequencyHz;
FlightSimInteger ComFrequencyHz;
FlightSimInteger Com2FrequencyHz;
FlightSimInteger xpdrCode;
FlightSimInteger xpdrMode;
FlightSimInteger xpdrIdent;
//Build NAV2 Frequency
int get2Frequency()
{
int freq = 10000;
//Deal with tens
if (!digitalRead(tens2A))
freq += 1000;
//Deal with units.
//First, get our switch states and store them.
bool ua = !digitalRead(units2A);
bool ub = !digitalRead(units2B);
bool uc = !digitalRead(units2C);
bool ud = !digitalRead(units2D);
bool ue = !digitalRead(units2E);
//Lets check that exactly two switches are on.
//if ((ua + ub + uc + ud + ue) != 2)
//return -1;
if (ua)
{
if (ub)
freq += 100;
if (uc)
freq += 200;
if (ud)
freq += 800;
if (ue)
freq += 900;
}
if (ub)
{
if (uc)
freq += 300;
if (ud)
freq += 400;
if (ue)
freq += 000; //This doesn't do anything, but is here for completeness.
}
if (uc)
{
if (ud)
freq += 500;
if (ue)
freq += 600;
}
if (ud)
{
if (ue)
freq += 700;
}
//Deal with point units in exactly the same way.
bool pua = !digitalRead(pointTens2A);
bool pub = !digitalRead(pointTens2B);
bool puc = !digitalRead(pointTens2C);
bool pud = !digitalRead(pointTens2D);
bool pue = !digitalRead(pointTens2E);
if (pua)
{
if (pub)
freq += 10;
if (puc)
freq += 20;
if (pud)
freq += 80;
if (pue)
freq += 90;
}
if (pub)
{
if (puc)
freq += 30;
if (pud)
freq += 40;
if (pue)
freq += 00; //This doesn't do anything, but is here for completeness.
}
if (puc)
{
if (pud)
freq += 50;
if (pue)
freq += 60;
}
if (pud)
{
if (pue)
freq += 70;
}
//Last of all, your point hundreds.
if (!digitalRead(pointHundreds2A))
freq += 5;
return freq;
}
// Get NAV1 Frequency
int getFrequency()
{
int freq = 10000;
//Deal with tens
if (!CS.digitalRead(tensA))
freq += 1000;
//Deal with units.
//First, get our switch states and store them.
bool ua = !CS.digitalRead(unitsA);
bool ub = !CS.digitalRead(unitsB);
bool uc = !CS.digitalRead(unitsC);
bool ud = !CS.digitalRead(unitsD);
bool ue = !CS.digitalRead(unitsE);
if (ua)
{
if (ub)
freq += 100;
if (uc)
freq += 200;
if (ud)
freq += 800;
if (ue)
freq += 900;
}
if (ub)
{
if (uc)
freq += 300;
if (ud)
freq += 400;
if (ue)
freq += 000; //This doesn't do anything, but is here for completeness.
}
if (uc)
{
if (ud)
freq += 500;
if (ue)
freq += 600;
}
if (ud)
{
if (ue)
freq += 700;
}
//Deal with point units in exactly the same way.
bool pua = !CS.digitalRead(pointTensA);
bool pub = !CS.digitalRead(pointTensB);
bool puc = !CS.digitalRead(pointTensC);
bool pud = !CS.digitalRead(pointTensD);
bool pue = !CS.digitalRead(pointTensE);
if (pua)
{
if (pub)
freq += 10;
if (puc)
freq += 20;
if (pud)
freq += 80;
if (pue)
freq += 90;
}
if (pub)
{
if (puc)
freq += 30;
if (pud)
freq += 40;
if (pue)
freq += 00; //This doesn't do anything, but is here for completeness.
}
if (puc)
{
if (pud)
freq += 50;
if (pue)
freq += 60;
}
if (pud)
{
if (pue)
freq += 70;
}
//Last of all, your point hundreds.
if (!digitalRead(pointHundredsA))
freq += 5;
return freq;
}
// Build Com 1 Frequency
int getCFrequency()
{
int cfreq = 10000;
// Get switch states and store for ctens
bool cta = !CS.digitalRead(CtensA);
bool ctb = !CS.digitalRead(CtensB);
bool ctc = !CS.digitalRead(CtensC);
//Deal with ctens
if (cta)
{
if (ctb)
cfreq += 1000;
if (ctc)
cfreq += 2000;
}
if (ctb)
{
if (ctc)
cfreq += 3000;
}
//Deal with units.
//First, get our switch states and store them.
bool cua = !CS.digitalRead(CunitsA);
bool cub = !CS.digitalRead(CunitsB);
bool cuc = !CS.digitalRead(CunitsC);
bool cud = !CS.digitalRead(CunitsD);
bool cue = !CS.digitalRead(CunitsE);
//Lets check that exactly two switches are on.
if (cua)
{
if (cub)
cfreq += 100;
if (cuc)
cfreq += 200;
if (cud)
cfreq += 800;
if (cue)
cfreq += 900;
}
if (cub)
{
if (cuc)
cfreq += 300;
if (cud)
cfreq += 400;
if (cue)
cfreq += 000; //This doesn't do anything, but is here for completeness.
}
if (cuc)
{
if (cud)
cfreq += 500;
if (cue)
cfreq += 600;
}
if (cud)
{
if (cue)
cfreq += 700;
}
//Deal with point units in exactly the same way.
bool cpua = !CS.digitalRead(CpointTensA);
bool cpub = !CS.digitalRead(CpointTensB);
bool cpuc = !CS.digitalRead(CpointTensC);
bool cpud = !CS.digitalRead(CpointTensD);
bool cpue = !CS.digitalRead(CpointTensE);
//Lets check that exactly two switches are on.
if (cpua)
{
if (cpub)
cfreq += 10;
if (cpuc)
cfreq += 20;
if (cpud)
cfreq += 80;
if (cpue)
cfreq += 90;
}
if (cpub)
{
if (cpuc)
cfreq += 30;
if (cpud)
cfreq += 40;
if (cpue)
cfreq += 00; //This doesn't do anything, but is here for completeness.
}
if (cpuc)
{
if (cpud)
cfreq += 50;
if (cpue)
cfreq += 60;
}
if (cpud)
{
if (cpue)
cfreq += 70;
}
//Last of all, your point hundreds.
bool cpha = !CS.digitalRead(CpointHundredsA);
bool cphb = !CS.digitalRead(CpointHundredsB);
bool cphc = !CS.digitalRead(CpointHundredsC);
bool cphd = !CS.digitalRead(CpointHundredsD);
bool cphe = !CS.digitalRead(CpointHundredsE);
//Lets check that exactly two switches are on.
if (cphc)
{
if (cpha)
cfreq += 2;
if (cphd)
cfreq += 5;
}
if (cphe)
{
if (cphb)
cfreq += 0;
if (cphd)
cfreq += 7;
}
return cfreq;
}
//Build COM2 Frequency
int getC2Frequency()
{
int cfreq = 10000;
// Get switch states and store for ctens
bool cta = !CS.digitalRead(C2tensA);
bool ctb = !CS.digitalRead(C2tensB);
bool ctc = !CS.digitalRead(C2tensC);
//Deal with ctens
if (cta)
{
if (ctb)
cfreq += 1000;
if (ctc)
cfreq += 2000;
}
if (ctb)
{
if (ctc)
cfreq += 3000;
}
//Deal with units.
//First, get our switch states and store them.
bool cua = !CS.digitalRead(C2unitsA);
bool cub = !CS.digitalRead(C2unitsB);
bool cuc = !CS.digitalRead(C2unitsC);
bool cud = !CS.digitalRead(C2unitsD);
bool cue = !CS.digitalRead(C2unitsE);
//Lets check that exactly two switches are on.
if (cua)
{
if (cub)
cfreq += 100;
if (cuc)
cfreq += 200;
if (cud)
cfreq += 800;
if (cue)
cfreq += 900;
}
if (cub)
{
if (cuc)
cfreq += 300;
if (cud)
cfreq += 400;
if (cue)
cfreq += 000; //This doesn't do anything, but is here for completeness.
}
if (cuc)
{
if (cud)
cfreq += 500;
if (cue)
cfreq += 600;
}
if (cud)
{
if (cue)
cfreq += 700;
}
//Deal with point units in exactly the same way.
bool cpua = !CS.digitalRead(C2pointTensA);
bool cpub = !CS.digitalRead(C2pointTensB);
bool cpuc = !CS.digitalRead(C2pointTensC);
bool cpud = !CS.digitalRead(C2pointTensD);
bool cpue = !CS.digitalRead(C2pointTensE);
//Lets check that exactly two switches are on.
if (cpua)
{
if (cpub)
cfreq += 10;
if (cpuc)
cfreq += 20;
if (cpud)
cfreq += 80;
if (cpue)
cfreq += 90;
}
if (cpub)
{
if (cpuc)
cfreq += 30;
if (cpud)
cfreq += 40;
if (cpue)
cfreq += 00; //This doesn't do anything, but is here for completeness.
}
if (cpuc)
{
if (cpud)
cfreq += 50;
if (cpue)
cfreq += 60;
}
if (cpud)
{
if (cpue)
cfreq += 70;
}
//Last of all, your point hundreds.
bool cpha = !CS.digitalRead(C2pointHundredsA);
bool cphb = !CS.digitalRead(C2pointHundredsB);
bool cphc = !CS.digitalRead(C2pointHundredsC);
bool cphd = !CS.digitalRead(C2pointHundredsD);
bool cphe = !CS.digitalRead(C2pointHundredsE);
//Lets check that exactly two switches are on.
if (cphc)
{
if (cpha)
cfreq += 2;
if (cphd)
cfreq += 5;
}
if (cphe)
{
if (cphb)
cfreq += 0;
if (cphd)
cfreq += 7;
}
return cfreq;
}
//Function to build the ATC code
int getAtcFreq()
{
int freq = 0;
int digit1 = 0;
int digit2 = 0;
int digit3 = 0;
int digit4 = 0;
bool a1 = !CS.digitalRead(digit1A);
bool b1 = !CS.digitalRead(digit1B);
bool c1 = !CS.digitalRead(digit1C);
digit1 = ((a1 + b1 * 2 + c1 * 4) * 1000);
bool a2 = !CS.digitalRead(digit2A);
bool b2 = !CS.digitalRead(digit2B);
bool c2 = !CS.digitalRead(digit2C);
digit2 = ((a2 + b2 * 2 + c2 * 4) * 100);
bool a3 = !CS.digitalRead(digit3A);
bool b3 = !CS.digitalRead(digit3B);
bool c3 = !CS.digitalRead(digit3C);
digit3 = ((a3 + b3 * 2 + c3 * 4) * 10);
bool a4 = !CS.digitalRead(digit4A);
bool b4 = !CS.digitalRead(digit4B);
bool c4 = !CS.digitalRead(digit4C);
digit4 = (a4 + b4 * 2 + c4 * 4);
freq = (digit1 + digit2 + digit3 + digit4);
return freq;
}
void setup()
{
Wire.begin();
CS.initialize();
Serial.begin(38400);
CS.portMode(0, 0b1111111111111111); // 0 = output, 1 = input
CS.portPullup(0, 0b1111111111111111); // 0 = no pullup, 1 = pullup
CS.portMode(1, 0b1111111111111111); // 0 = output, 1 = input
CS.portPullup(1, 0b1111111111111111); // 0 = no pullup, 1 = pullup
CS.portMode(2, 0b1111111111111111); // 0 = output, 1 = input
CS.portPullup(2, 0b1111111111111111); // 0 = no pullup, 1 = pullup
CS.portMode(3, 0b1111111111111111); // 0 = output, 1 = input
CS.portPullup(3, 0b1111111111111111); // 0 = no pullup, 1 = pullup
pinMode(tens2A, INPUT_PULLUP);
pinMode(units2A, INPUT_PULLUP);
pinMode(units2B, INPUT_PULLUP);
pinMode(units2C, INPUT_PULLUP);
pinMode(units2D, INPUT_PULLUP);
pinMode(units2E, INPUT_PULLUP);
pinMode(pointTens2A, INPUT_PULLUP);
pinMode(pointTens2B, INPUT_PULLUP);
pinMode(pointTens2C, INPUT_PULLUP);
pinMode(pointTens2D, INPUT_PULLUP);
pinMode(pointTens2E, INPUT_PULLUP);
pinMode(pointHundredsA, INPUT_PULLUP);
pinMode(pointHundreds2A, INPUT_PULLUP);
NavFrequencyHz = XPlaneRef("sim/cockpit2/radios/actuators/nav1_frequency_hz");
ComFrequencyHz = XPlaneRef("sim/cockpit2/radios/actuators/com1_frequency_hz");
Nav2FrequencyHz = XPlaneRef("sim/cockpit2/radios/actuators/nav2_frequency_hz");
Com2FrequencyHz = XPlaneRef("sim/cockpit2/radios/actuators/com2_frequency_hz");
xpdrCode = XPlaneRef("sim/cockpit2/radios/actuators/transponder_code");
xpdrMode = XPlaneRef("sim/cockpit/radios/transponder_mode"); //(off=0,stdby=1,on=2,test=3)"
xpdrIdent = XPlaneRef("sim/cockpit/radios/transponder_id");
}
void loop()
{
FlightSim.update();
NavFrequencyHz = getFrequency();
ComFrequencyHz = getCFrequency();
Nav2FrequencyHz = get2Frequency();
Com2FrequencyHz = getC2Frequency();
xpdrCode = getAtcFreq();
if (CS.digitalRead(61) == LOW) {
xpdrMode = 2;
} else {
xpdrMode = 1;
}
//Serial.println(NavFrequencyHz);
//Serial.println(Nav2FrequencyHz);
}