Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: teensy 3.2 loop problem

  1. #1

    teensy 3.2 loop problem

    hi, I have a problem with teensy 3.2, after about 2 minutes of activity the loop stops I don't run the program.
    I am attaching the listing, I am at the first schedules, any advice to improve the code is welcome.
    Code:
    #include <Chrono.h>
    #include <LightChrono.h>
    #include <i2c_t3.h>
    #include <ADC.h>
    #include <FreqMeasureMulti.h>
    
    /*Pin
      0 Rx Nextion  -- Al display
      1 Tx Nextion  -- Al display
      2 In1         -- E-Stop
      3 In2         -- ScuotitoriON
      4 In3         -- AspiratoriON
      5 Freq1       -- Rpm Scuotitori
      6 Freq2       -- Rpm Aspiratori
      7 In6         -- ConvogliatoriON
      8 In5
      9 Freq3       -- Rpm ConvogliatoreDX
      10 Freq4      -- Rpm ConvogliatoreSX
      11 In4
      12 Out2       -- Buzzer
      13 Out1       -- Led ciclo
      14 Adc Batt   -- Tensione batteria
      15 An3
      16 An4
      17 An5
      18 I2C Sda    -- I2C Eprom e inclinometro
      19 I2c Scl    -- I2C Eprom e inclinometro
      20 Freq5
      21 Freq6
      22 Freq7
      23 Freq8
      24 Out3
      25 Out6
      26 An2
      27 An1
      28 Nc
      29 Nc
      30 Out4
      31 Out5
      32 Pwm for L293D Enable
      33 Nc
      A14 Dac Out 0-3v3
    */
    
    // Da usare per calibrare il massimo valore ADC ///////////////////////////////////////////////////////////////////
    #define BattCal  33577 //33577 //in mV per calibrare
    #define An1Cal   150
    #define An2Cal   250
    #define An3Cal   4096
    #define An4Cal   4096
    #define An5Cal   4096
    
    // Dichiarazione PIN ///////////////////////////////////////////////////////////////////////////////////////////
    //Frequenze IN
    #define Freq1In     5
    #define Freq2In     6
    #define Freq3In     9
    #define Freq4In    10
    #define Freq5In    20
    #define Freq6In    21
    #define Freq7In    22
    #define Freq8In    23
    
    //Digital IN
    #define In1     2
    #define In2     3
    #define In3     4
    #define In4    11
    #define In5     8
    #define In6     7
    
    //Analogic IN
    #define AdcBatt  A0 // ADC0 pin 14
    #define An3      A1 // ADC0 pin 15
    #define An4      A2 // ADC0 pin 16
    #define An5      A3 // ADC0 pin 17
    #define An2     A15 // ADC0 pin 26
    #define An1     A16 // ADC0 pin 27
    
    //Digital OUT
    #define Out2       12
    #define Out1       13
    #define Out5       25
    #define Out6       31
    #define Out3       32
    #define Out4       30
    
    
    //Variabili di sistema //////////////////////////////////////////////////////////////////////////////////////////////
    int inByte = 0;  // incoming serial byte
    float sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0, sum5 = 0, sum6 = 0, sum7 = 0, sum8 = 0;  // usata per freq
    int count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0, count7 = 0, count8 = 0;  // usata per freq
    int frequenza1 = 0, frequenza2 = 0, frequenza3 = 0, frequenza4 = 0 , frequenza5 = 0, frequenza6 = 0, frequenza7 = 0, frequenza8 = 0;  // usata per freq
    int count1zero = 0, count2zero = 0, count3zero = 0, count4zero = 0, count5zero = 0, count6zero = 0, count7zero = 0, count8zero = 0;  // usata per freq
    int AdcBattvalue = 0, An1value = 0, An2value = 0, An3value = 0, An4value = 0, An5value = 0;  // usata per analogico
    int In1value = 0, In2value = 0, In3value = 0, In4value = 0, In5value = 0, In6value = 0;   // usata per digitale
    
    // Variabili per ciclo
    elapsedMillis timeout = 0;
    elapsedMillis analogicprint = 0;
    elapsedMillis digitalprint = 0;
    elapsedMillis ledciclo = 0;
    
    
    // Measure 8 frequencies at the same time! :-) ///////////////////////////////////////////////////////
    FreqMeasureMulti freq1;
    FreqMeasureMulti freq2;
    FreqMeasureMulti freq3;
    FreqMeasureMulti freq4;
    FreqMeasureMulti freq5;
    FreqMeasureMulti freq6;
    FreqMeasureMulti freq7;
    FreqMeasureMulti freq8;
    
    
    ADC *adc = new ADC();; // adc object
    Chrono Contaore; // dichiarazione Contaore
    
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    void setup() {
    
      Wire.begin(); // inizializa i2c
    
      //Input Analogici
      pinMode(AdcBatt, INPUT);
      pinMode(An1, INPUT);
      pinMode(An2, INPUT);
      pinMode(An3, INPUT);
      pinMode(An4, INPUT);
      pinMode(An5, INPUT);
    
      //Input Digitali
      pinMode(In1, INPUT);
      pinMode(In2, INPUT);
      pinMode(In3, INPUT);
      pinMode(In4, INPUT);
      pinMode(In5, INPUT);
      pinMode(In6, INPUT);
    
      //Output
      pinMode(Out1, OUTPUT);
      pinMode(Out2, OUTPUT);
      pinMode(Out3, OUTPUT);
      pinMode(Out4, OUTPUT);
      pinMode(Out5, OUTPUT);
      pinMode(Out6, OUTPUT);
    
      Serial1.begin(115200); // seriale a 115200 baud per display
    
      freq1.begin(Freq1In);
      freq2.begin(Freq2In);
      freq3.begin(Freq3In);
      freq4.begin(Freq4In);
      freq5.begin(Freq5In);
      freq6.begin(Freq6In);
      freq7.begin(Freq7In);
      freq8.begin(Freq8In);
    
      ///// ADC0 ////
      // reference can be ADC_REFERENCE::REF_3V3, ADC_REFERENCE::REF_1V2 (not for Teensy LC) or ADC_REFERENCE::REF_EXT.
      //adc->setReference(ADC_REFERENCE::REF_1V2, ADC_0); // change all 3.3 to 1.2 if you change the reference to 1V2
      adc->setReference(ADC_REFERENCE::REF_3V3, ADC_0); // change all 3.3 to 1.2 if you change the reference to 1V2
    
      adc->setAveraging(10); // set number of averages
      adc->setResolution(12); // set bits of resolution
    
      // it can be any of the ADC_CONVERSION_SPEED enum: VERY_LOW_SPEED, LOW_SPEED, MED_SPEED, HIGH_SPEED_16BITS, HIGH_SPEED or VERY_HIGH_SPEED
      // see the documentation for more information
      // additionally the conversion speed can also be ADACK_2_4, ADACK_4_0, ADACK_5_2 and ADACK_6_2,
      // where the numbers are the frequency of the ADC clock in MHz and are independent on the bus speed.
      adc->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED); // change the conversion speed
      // it can be any of the ADC_MED_SPEED enum: VERY_LOW_SPEED, LOW_SPEED, MED_SPEED, HIGH_SPEED or VERY_HIGH_SPEED
      adc->setSamplingSpeed(ADC_SAMPLING_SPEED::VERY_HIGH_SPEED); // change the sampling speed
    
      // always call the compare functions after changing the resolution!
      //adc->enableCompare(1.0/3.3*adc->getMaxValue(ADC_0), 0, ADC_0); // measurement will be ready if value < 1.0V
      //adc->enableCompareRange(1.0*adc->getMaxValue(ADC_0)/3.3, 2.0*adc->getMaxValue(ADC_0)/3.3, 0, 1, ADC_0); // ready if value lies out of [1.0,2.0] V
    
      // If you enable interrupts, notice that the isr will read the result, so that isComplete() will return false (most of the time)
      //adc->enableInterrupts(ADC_0);
    
    
      ////// ADC1 /////
    #if ADC_NUM_ADCS>1
      adc->setAveraging(10, ADC_1); // set number of averages
      adc->setResolution(12, ADC_1); // set bits of resolution
      adc->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED, ADC_1); // change the conversion speed
      adc->setSamplingSpeed(ADC_SAMPLING_SPEED::VERY_HIGH_SPEED, ADC_1); // change the sampling speed
    
      adc->setReference(ADC_REFERENCE::REF_3V3, ADC_1);
    
      // always call the compare functions after changing the resolution!
      //adc->enableCompare(1.0/3.3*adc->getMaxValue(ADC_1), 0, ADC_1); // measurement will be ready if value < 1.0V
      //adc->enableCompareRange(1.0*adc->getMaxValue(ADC_1)/3.3, 2.0*adc->getMaxValue(ADC_1)/3.3, 0, 1, ADC_1); // ready if value lies out of [1.0,2.0] V
    
    
      // If you enable interrupts, note that the isr will read the result, so that isComplete() will return false (most of the time)
      //adc->enableInterrupts(ADC_1);
    
    #endif
    
      delay(500);
    
      // Fa la lettura delle ore e dei minuti e le invia al display
      byte minuti = i2c_eeprom_read_byte(0x50, 0);
      Serial1.print("Cal.minuti.val=");
      Serial1.print(minuti, DEC);
      Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
      Serial1.write(0xff);
      Serial1.write(0xff);
      byte lowByte = i2c_eeprom_read_byte(0x50, 1);
      byte highByte = i2c_eeprom_read_byte(0x50, 2);
      int   ore = ((lowByte << 0) & 0xFF) + ((highByte << 8) & 0xFF00);
      Serial1.print("Cal.ore.val=");
      Serial1.print(ore, DEC);
      Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
      Serial1.write(0xff);
      Serial1.write(0xff);
    
    }
    
    
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    void loop() {
    
    
    
      // Use Chrono as a metronome with an interval of 1000 ms :
      if (Contaore.hasPassed(60000) ) { // elapsed(60000) returns 1 if 60000ms=1minuto have passed.
        Contaore.restart();  // restart the Chrono
        hourmeter(); // va al contaore
    
      }
    
      // if we get a valid byte, read analog ins:
      if (Serial1.available() > 0) {
        // get incoming byte:
        inByte = Serial1.read();
        switch (inByte) {
          case '1':
            Out2On();
          case '2':
            Out2Off();
          case '3':
          //
          case '4':
          //
          case '5':
          //
          case '6':
          //
          case '7':
          //
          case '8':
          //
          case '9':
          //
          case 'r':
            reseti2ceprom();
    
        }
      }
    
      if (freq1.available()) {
        sum1 = sum1 + freq1.read();
        count1 = count1 + 1;
      }
      if (freq2.available()) {
        sum2 = sum2 + freq2.read();
        count2 = count2 + 1;
      }
      if (freq3.available()) {
        sum3 = sum3 + freq3.read();
        count3 = count3 + 1;
      }
      if (freq4.available()) {
        sum4 = sum4 + freq4.read();
        count4 = count4 + 1;
      }
      if (freq5.available()) {
        sum5 = sum5 + freq5.read();
        count5 = count5 + 1;
      }
      if (freq6.available()) {
        sum6 = sum6 + freq6.read();
        count6 = count6 + 1;
      }
      if (freq7.available()) {
        sum7 = sum7 + freq7.read();
        count7 = count7 + 1;
      }
      if (freq8.available()) {
        sum8 = sum8 + freq8.read();
        count8 = count8 + 1;
      }
    
    
      // print results every half second
      if (timeout > 500) {
        if (count1 > 0) {
          frequenza1 = (int)freq1.countToFrequency((sum1 / count1) / 10);
          Serial1.print("Cal.freq1.val=");
          Serial1.print(frequenza1);
          Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
          Serial1.write(0xff);
          Serial1.write(0xff);
          count1zero = 0;
        } else {
          if (count1zero > 1) {
            Serial1.print("Cal.freq1.val=0");
            Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
            Serial1.write(0xff);
            Serial1.write(0xff);
            count1zero = 0;
          }
          else {
            count1zero = count1zero + 1;
          }
        }
    
    
    
        if (count2 > 0) {
          frequenza2 = (int)freq2.countToFrequency((sum2 / count2) / 10);
          Serial1.print("Cal.freq2.val=");
          Serial1.print(frequenza2);
          Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
          Serial1.write(0xff);
          Serial1.write(0xff);
          count2zero = 0;
        } else {
          if (count2zero > 1) {
            Serial1.print("Cal.freq2.val=0");
            Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
            Serial1.write(0xff);
            Serial1.write(0xff);
            count2zero = 0;
          }
          else {
            count2zero = count2zero + 1;
          }
        }
    
        if (count3 > 0) {
          frequenza3 = (int)freq3.countToFrequency(sum3 / count3 / 10);
          Serial1.print("Cal.freq3.val=");
          Serial1.print(frequenza3);
          Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
          Serial1.write(0xff);
          Serial1.write(0xff);
          count3zero = 0;
        } else {
          if (count3zero > 1) {
            Serial1.print("Cal.freq3.val=0");
            Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
            Serial1.write(0xff);
            Serial1.write(0xff);
            count3zero = 0;
          }
          else {
            count3zero = count3zero + 1;
          }
        }
    
        if (count4 > 0) {
          frequenza4 = (int)freq4.countToFrequency(sum4 / count4 / 10);
          Serial1.print("Cal.freq4.val=");
          Serial1.print(frequenza4);
          Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
          Serial1.write(0xff);
          Serial1.write(0xff);
          count4zero = 0;
        } else {
          if (count4zero > 1) {
            Serial1.print("Cal.freq4.val=0");
            Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
            Serial1.write(0xff);
            Serial1.write(0xff);
            count4zero = 0;
          }
          else {
            count4zero = count4zero + 1;
          }
        }
    
        if (count5 > 0) {
          frequenza5 = (int)freq5.countToFrequency(sum5 / count5 / 10);
          Serial1.print("Cal.freq5.val=");
          Serial1.print(frequenza5);
          Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
          Serial1.write(0xff);
          Serial1.write(0xff);
          count5zero = 0;
        } else {
          if (count5zero > 1) {
            Serial1.print("Cal.freq5.val=0");
            Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
            Serial1.write(0xff);
            Serial1.write(0xff);
            count5zero = 0;
          }
          else {
            count5zero = count5zero + 1;
          }
        }
    
        if (count6 > 0) {
          frequenza6 = (int)freq6.countToFrequency(sum6 / count6 / 10);
          Serial1.print("Cal.freq6.val=");
          Serial1.print(frequenza6);
          Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
          Serial1.write(0xff);
          Serial1.write(0xff);
          count6zero = 0;
        } else {
          if (count6zero > 1) {
            Serial1.print("Cal.freq6.val=0");
            Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
            Serial1.write(0xff);
            Serial1.write(0xff);
            count6zero = 0;
          }
          else {
            count6zero = count6zero + 1;
          }
        }
    
        if (count7 > 0) {
          frequenza7 = (int)freq7.countToFrequency(sum7 / count7 / 10);
          Serial1.print("Cal.freq7.val=");
          Serial1.print(frequenza7);
          Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
          Serial1.write(0xff);
          Serial1.write(0xff);
          count7zero = 0;
        } else {
          if (count7zero > 1) {
            Serial1.print("Cal.freq7.val=0");
            Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
            Serial1.write(0xff);
            Serial1.write(0xff);
            count7zero = 0;
          }
          else {
            count7zero = count7zero + 1;
          }
        }
    
        if (count8 > 0) {
          frequenza8 = (int)freq8.countToFrequency(sum8 / count8 / 10);
          Serial1.print("Cal.freq8.val=");
          Serial1.print(frequenza8);
          Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
          Serial1.write(0xff);
          Serial1.write(0xff);
          count8zero = 0;
        } else {
          if (count8zero > 1) {
            Serial1.print("Cal.freq8.val=0");
            Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
            Serial1.write(0xff);
            Serial1.write(0xff);
            count8zero = 0;
          }
          else {
            count8zero = count8zero + 1;
          }
        }
    
        sum1 = 0;
        sum2 = 0;
        sum3 = 0;
        sum4 = 0;
        sum5 = 0;
        sum6 = 0;
        sum7 = 0;
        sum8 = 0;
        count1 = 0;
        count2 = 0;
        count3 = 0;
        count4 = 0;
        count5 = 0;
        count6 = 0;
        count7 = 0;
        count8 = 0;
        timeout = 0;
      }
    
    
      // print results every half second
      if (analogicprint > 1000) {
        analogic();
      }
    
    
      if (digitalprint > 1500) {
        digitali();
      }
    
      testciclo();
    
    }
    
    
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    void analogic() {
    
      AdcBattvalue = adc->analogRead(AdcBatt);
      AdcBattvalue = map(AdcBattvalue, 0, 4096, 0, BattCal);
      AdcBattvalue = constrain(AdcBattvalue, 0, BattCal);    // limits range of sensor values to between 0 and 100
      Serial1.print("Cal.adcbatt.val=");
      Serial1.print(AdcBattvalue);
      Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
      Serial1.write(0xff);
      Serial1.write(0xff);
    
      An1value = adc->analogRead(An1);
      An1value = map(An1value, 0, 1861, 0, An1Cal);
      An1value = constrain(An1value, 0, An1Cal);    // limits range of sensor values to between 0 and 100
      Serial1.print("Cal.an1.val=");
      Serial1.print(An1value);
      Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
      Serial1.write(0xff);
      Serial1.write(0xff);
    
      An2value = adc->analogRead(An2);
      An2value = map(An2value, 744, 3723, 0, An2Cal);
      An2value = constrain(An2value, 0, An2Cal);    // limits range of sensor values to between 0 and 100
      Serial1.print("Cal.an2.val=");
      Serial1.print(An2value);
      Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
      Serial1.write(0xff);
      Serial1.write(0xff);
    
    
      An3value = adc->analogRead(An3);
      An3value = map(An3value, 0, 4096, 0, An3Cal);
      An3value = constrain(An3value, 0, An3Cal);    // limits range of sensor values to between 0 and 100
      Serial1.print("Cal.an3.val=");
      Serial1.print(An3value);
      Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
      Serial1.write(0xff);
      Serial1.write(0xff);
    
      An4value = adc->analogRead(An4);
      An4value = map(An4value, 0, 4096, 0, An4Cal);
      An4value = constrain(An4value, 0, An4Cal);    // limits range of sensor values to between 0 and 100
      Serial1.print("Cal.an4.val=");
      Serial1.print(An4value);
      Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
      Serial1.write(0xff);
      Serial1.write(0xff);
    
    
      An5value = adc->analogRead(An5);
      An5value = map(An5value, 0, 4096, 0, An5Cal);
      An5value = constrain(An5value, 0, An5Cal);    // limits range of sensor values to between 0 and 100
      Serial1.print("Cal.an5.val=");
      Serial1.print(An5value);
      Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
      Serial1.write(0xff);
      Serial1.write(0xff);
    
      analogicprint = 0;
    
    }
    
    void digitali() {
    
    
    
      In1value = digitalRead(In1);
      if (In1value) {
        Serial1.print("Cal.in1.val=0");
        Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
        Serial1.write(0xff);
        Serial1.write(0xff);
      }
      else {
        Serial1.print("Cal.in1.val=1");
        Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
        Serial1.write(0xff);
        Serial1.write(0xff);
      }
    
    
      In2value = digitalRead(In2);
      if (In2value) {
        Serial1.print("Cal.in2.val=0");
        Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
        Serial1.write(0xff);
        Serial1.write(0xff);
      }
      else {
        Serial1.print("Cal.in2.val=1");
        Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
        Serial1.write(0xff);
        Serial1.write(0xff);
      }
    
    
      In3value = digitalRead(In3);
      if (In3value) {
        Serial1.print("Cal.in3.val=0");
        Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
        Serial1.write(0xff);
        Serial1.write(0xff);
      }
      else {
        Serial1.print("Cal.in3.val=1");
        Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
        Serial1.write(0xff);
        Serial1.write(0xff);
      }
    
    
      In4value = digitalRead(In4);
      if (In4value) {
        Serial1.print("Cal.in4.val=0");
        Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
        Serial1.write(0xff);
        Serial1.write(0xff);
      }
      else {
        Serial1.print("Cal.in4.val=1");
        Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
        Serial1.write(0xff);
        Serial1.write(0xff);
      }
    
    
      In5value = digitalRead(In5);
      if (In5value) {
        Serial1.print("Cal.in5.val=0");
        Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
        Serial1.write(0xff);
        Serial1.write(0xff);
      }
      else {
        Serial1.print("Cal.in5.val=1");
        Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
        Serial1.write(0xff);
        Serial1.write(0xff);
      }
    
    
      In6value = digitalRead(In6);
      if (In6value) {
        Serial1.print("Cal.in6.val=0");
        Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
        Serial1.write(0xff);
        Serial1.write(0xff);
      }
      else {
        Serial1.print("Cal.in6.val=1");
        Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
        Serial1.write(0xff);
        Serial1.write(0xff);
      }
    
      digitalprint = 0;
    
    }
    
    // Led per tes ciclo //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    void testciclo() {
      if (ledciclo < 1000) {
        digitalWrite(Out1, HIGH);
      }
      if (ledciclo > 1000) {
        digitalWrite(Out1, LOW);
      }
      if (ledciclo > 2000) {
        ledciclo = 0;
      }
    }
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    
    // Contaore  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    void hourmeter() {
    
      byte minuti;
    
      minuti = i2c_eeprom_read_byte(0x50, 0);
    
      Serial1.print("Cal.minuti.val=");
      Serial1.print(minuti, DEC);
      Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
      Serial1.write(0xff);
      Serial1.write(0xff);
    
    
      if (minuti < 60) {
        minuti = minuti + 1;
        i2c_eeprom_write_byte(0x50, 0, minuti);
      }
      else {
        // increment num
        minuti = 1;
        Serial1.print("Cal.minuti.val=");
        Serial1.print(minuti - 1, DEC);
        Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
        Serial1.write(0xff);
        Serial1.write(0xff);
        i2c_eeprom_write_byte(0x50, 0, minuti);
    
        byte lowByte = i2c_eeprom_read_byte(0x50, 1);
        byte highByte = i2c_eeprom_read_byte(0x50, 2);
    
        int   ore = ((lowByte << 0) & 0xFF) + ((highByte << 8) & 0xFF00);
    
        ore = ore + 1;
    
        Serial1.print("Cal.ore.val=");
        Serial1.print(ore, DEC);
        Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
        Serial1.write(0xff);
        Serial1.write(0xff);
    
        lowByte = ((ore >> 0) & 0xFF);
        highByte = ((ore >> 8) & 0xFF);
    
        i2c_eeprom_write_byte(0x50, 1, lowByte);
        i2c_eeprom_write_byte(0x50, 2, highByte);
    
      }
    }
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    
    
    
    // reset i2c eprom //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    void reseti2ceprom() {
      for (int i = 0; i <= 32000; i++) {
        i2c_eeprom_write_byte(0x50, i, 0);
      }
      i2c_eeprom_write_byte(0x50, 0, 1);
      byte minuti = i2c_eeprom_read_byte(0x50, 0);
      Serial1.print("Cal.minuti.val=");
      Serial1.print(minuti - 1, DEC);
      Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
      Serial1.write(0xff);
      Serial1.write(0xff);
      byte lowByte = i2c_eeprom_read_byte(0x50, 1);
      byte highByte = i2c_eeprom_read_byte(0x50, 2);
      int   ore = ((lowByte << 0) & 0xFF) + ((highByte << 8) & 0xFF00);
      Serial1.print("Cal.ore.val=");
      Serial1.print(ore, DEC);
      Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
      Serial1.write(0xff);
      Serial1.write(0xff);
      Contaore.restart();  // restart the Chrono
    }
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    
    
    // per scrivere e leggere i2c eprom ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
      int rdata = data;
      Wire.beginTransmission(deviceaddress);
      Wire.write((int)(eeaddress)); // MSB
      Wire.write((int)(eeaddress)); // LSB
      Wire.write(rdata);
      Wire.endTransmission();
      delay(10);
    }
    
    byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
      byte rdata = 0;
      Wire.beginTransmission(deviceaddress);
      Wire.write((int)(eeaddress)); // MSB
      Wire.write((int)(eeaddress)); // LSB
      Wire.endTransmission();
      Wire.requestFrom(deviceaddress, 1);
      if (Wire.available()) rdata = Wire.read();
      return rdata;
    }
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    
    // Accende e spegne out2 ///////////////////////////////////////////////////////////////////////////////////////////////////////
    void Out2On() {
      digitalWrite(Out2, HIGH); //  off
    }
    
    void Out2Off() {
      digitalWrite(Out2, LOW);  //  on
    }
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //Fine...

  2. #2
    Can anyone help me!!

  3. #3
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    8,839
    That is a large amount of code with unique devices.

    Is it totally stopped or just no longer printing to USB? Is there any idea where or why it stops? Did it work before up to some point before recent additions or changes?

    Information about what is connected - the devices and interface types - may help a reader recognize something, perhaps a photo of the setup and connections.

    does it fail stand alone or does user or device input cause the stoppage?

  4. #4
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    5,673
    We don't have your hardware. Try to make a smaller testprogram without dependencies to external hardware.

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,176
    Also know nothing about Chrono.h and LightChrono.h.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •