My Teensy keeps locking up

Status
Not open for further replies.

raflyer

Well-known member
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

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);
}
 
can you suspect code in Centipede ? Memory leak?
eliminate all but a call to it, in a loop?
 
Last edited:
can you suspect code in Centipede ? Memory leak?
eliminate all but a call to it, in a loop?
If you don't mind me asking, what's Centipede?


On topic I'm not seeing any loops you have that could be causing issues or any interrupts. It could well be something to do with the library.
Put a load of Serial.println("Start of getAtcFreq()"); and Serial.println("End of getAtcFreq()"); all over your program. Monitor the device with a terminal. When the device freezes see what the last message you recieved was and that should give you a good pointer as to where it's crashed/froze. To check for memory leaks you could also print the currently used RAM
 
Monitoring RAM for leaks seems like a good plan - and some other minimal USB status output might show where the problem comes up, or if things start going odd before the hang. I don't see pin13 in use you could blink the light for feedback. Also the loop() is unthrottled - so the Teensy will be smashing these devices hard - how many cycles per second is this doing? How many do you need? Will added code end up slowing it down later? Perhaps using an elapsedMillis to limit the device update attempts to 10 or 100 as needed might keep the attached devices from getting overwhelmed.

From the p#1 like I find::
The Centipede Shield is an add-on PCB for standard layout (Duemilanova, Diecimila, Uno) Arduino microcontroller boards. It uses the Wire I2C interface on analog pins 4 and 5 to provide 64 general purpose I/O pins.
 
Ooop, thanks Defragster. For some reason I thought it was a memory leak detection program or something.

However thinking along those lines I have turned up:
dmalloc and mpatrol
Which is an interesting find indeed.
 
I tried adding a delay at the end of the loop, still locks up. It is real erratic, I set a timer every time I reset it. It goes 3 to as long as 18mins but majority, only last 3 to 5mins before locking up. I had a very similar code on just a Teensy for 30 Inputs and never had a problem. I think it is I2c related?? Noisey Ground messing with 2 wire com's? I am new to the I2C although I have an adafruit 16 servo board running fine on I2C and a Teensy next to 26V 400Hz power.
I also saw this on the Centipede's webpage, //TWBR = 12; // uncomment for 400KHz I2C (on 16MHz Arduinos) I wonder if the Teensy is trying to poll to fast for the Centipede?
Rob
 
Last edited:
Did you check if the Centipede class (lib) has a memory leak? Or it could overwrite some RAM variables of yours.
Best to eliminate all your code except a loop.
Then, move to hardware causes.
 
From the retailer I bought it from,
Skimming through the code, it looks like the Teensy has a compatible implementation of the Wire library, so you’re using the Arduino-targeted Centipede Library unmodified? Please correct if inaccurate.

The Centipede is an I2C device, so there’s a communication protocol happening between a couple of Teensy’s pins and the Centipede’s 64 pins. While direct port reads and writes on the Teensy can happen very fast, accesses to the Centipede shield may require awareness of when data transmission starts and stops so that none of the communication attempts step on each other.

While the library may work as-is, the Teensy Wire library might be implemented differently. The Teensy runs much, much faster than an Arduino, and I see you polling an input over I2C on every loop. It’s possible the I2C requests are happening so fast, that some buffer is filling up somewhere, or interrupts are stacking up and eventually overflowing, who knows what might be happening. In order to test this theory, I would try adding a delay in the main program loop. Perhaps see if limiting the loop execution to 100 times per second (with a 10 millisecond delay) makes the problem disappear.

I know that your final application will want to run faster than that, but if you prove that adding delays fixes the problem, then you can go on from there to figure out how to time the Centipede Shield accesses without slowing down the rest of your program.
 
I wonder if the Centipede and all that you have attached to it, needs more than the 3.3v/185mA that the Teensy can provide on the 3.3v rail. Or if it needs 5v, and you would need to do level shifting on the A4/A5 pins.
 
I'm going to add a seperate 5v supply and add a delay and see what happens

The MCP23017 chips on the Centipede Shield will operate on 3.3V, but things may go south if you start mixing 5V and 3.3V on the same I2C bus. The current limit is a more likely culprit, depending on what loads you have on the pins. If you're only reading inputs (not controlling any LEDs etc from Centipede pins) then the current should also not be a problem.

There are other reports of the Teensy's Wire library locking up after communication errors with other I2C devices. Paul's documentation points to an alternate library which may be more fault tolerant: https://www.pjrc.com/teensy/td_libs_Wire.html
 
There are other reports of the Teensy's Wire library locking up after communication errors with other I2C devices. Paul's documentation points to an alternate library which may be more fault tolerant: https://www.pjrc.com/teensy/td_libs_Wire.html
This is a very good point.

Sometimes I had issues with the I2C lines getting stuck low. So I opted for nox771's I2C library which boasts a Wire.resetBus() command. Using this on my MCU startup normally always resolves the issue. The library also allows for proper timeouts
 
What is the state of the I2C clock SCL when things hang? There's a long standing bug in the Arduino I2C library where if the I2C device doesn't respond as expected, execution just waits forever = hang.
 
OK, update to allot of testing. Ends with problem still present but a clue is given. Initial set up was run through 2 usb cables about 12ft total length to a powered hub. Switched to i2c_t3 wire library, didn't help, Tried adding a delay, didn't help, slowed Teensy down to 24Mhz, didn't help. Commented out all radios except one connected to Teensy, still locked up.

Here is the clue, I think?? Hooked up to laptop with new usb cable, it ran for 35mins with NO issues! Great I think, Bad cable, so I hook up to direct to my desktop with a new 5ft usb cable, locks up after less than 1 min to as much as 4-5mins :( So where I am at is that it worked fine on the laptop but when hooked to my desktop either directly or through a good powered hub, it stops responding. No what??

On a side note, on another Teensy in my system, I have the Adafruit 16ch 12bit I2C servo card running with no issues.

Rob
 
Maybe if you update the code as you have it showing how you implemented a throttling scheme to prevent calling too often. Using elapsedMillis to keep the loop coming and going and then just bypass the calls until they have sat long enough.

Pics are fun too.

I've found the elapsedMillis really easy to use:
Code:
elapsedMillis AutoClear;
void loop()
{
  int16_t xx, yy;
  if ( TS_GetMap( xx, yy ) ) {
    AutoClear = 0;  // Screen touched reset timer
    }
  if (AutoClear > 2000) { // Clear screen work area on no timed touch
    AutoClear = 0;
    tft.fillRect(0, BOXSIZE, 239, 125, ILI9341_BLACK);
  }
}
 
Last edited:
Rob,

for a systematical debugging, I suggest a multi-level approach:

  1. Limit your output rate. A delay is one thing, but even better to transmit data through USB only if something has changed.
  2. Find out if your problem is related to input (reading from centipede) or to output (writing to USB). Try separating reading from writing. FIRST do all the calls to the reading functions and store the result in local variables. THEN write only those values to X-Plane that actually have changed. Throw in some Serial.prints that help you to understand what's going on. In order to not overload USB output, limit the print rate with a delay. Get rid of both (Serial.println to debug reading AND delay) when everything works fine.
  3. If the problem still occurs, you can dig deeper into the section that actually is causing it.

Here's my suggestion in code:

Code:
...
void loop()
{
  bool needsUpdate = false;


  // wait a bit before next read - only to slow down serial output
  // get rid of the next two statements after debugging
  delay(500);
  Serial.print("reading...");
  
  int newNavFrequencyHz = getFrequency();
  int newComFrequencyHz = getCFrequency();
  int newNav2FrequencyHz = get2Frequency();
  int newCom2FrequencyHz = getC2Frequency();
  int newAtcFreq = getAtcFreq();
  int newXpdrMode=0;
  if (digitalRead(61) == LOW) {
    newXpdrMode = 2;
  } else {
    newXpdrMode = 1;
  }


  // get rid of the next statement after debugging
  Serial.println("done");


  if (newNavFrequencyHz != NavFrequencyHz) {
    Serial.print("New NAV1 freq: ");
    Serial.println(newNavFrequencyHz);
    NavFrequencyHz = newNavFrequencyHz;
    needsUpdate=true;
  }


  if (newComFrequencyHz != ComFrequencyHz) {
    Serial.print("New COM1 freq: ");
    Serial.println(newComFrequencyHz);
    ComFrequencyHz = newComFrequencyHz;
    needsUpdate=true;
  }


  if (newNav2FrequencyHz != Nav2FrequencyHz) {
    Serial.print("New NAV2 freq: ");
    Serial.println(newNav2FrequencyHz);
    Nav2FrequencyHz = newNav2FrequencyHz;
    needsUpdate=true;
  }


  if (newCom2FrequencyHz != Com2FrequencyHz) {
    Serial.print("New COM2 freq: ");
    Serial.println(newCom2FrequencyHz);
    Com2FrequencyHz = newCom2FrequencyHz;
    needsUpdate=true;
  }


  if (newAtcFreq != xpdrCode) {
    Serial.print("New XPDR code: ");
    Serial.println(newAtcFreq);
    xpdrCode = newAtcFreq;
    needsUpdate=true;  
  }


  if (newXpdrMode != xpdrMode) {
    Serial.print("New XPDR mode: ");
    Serial.println(newXpdrMode);
    xpdrMode = newXpdrMode;
    needsUpdate=true;  
  }


  if (needsUpdate) {
    FlightSim.update();
  }


}
 
Status
Not open for further replies.
Back
Top