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

Thread: StartMillis not working when using touchread

  1. #1

    StartMillis not working when using touchread

    Is this a known incompatibility?

    Using teensy 3.6 and audio board, trying to set up cap sensing and get some delay and audio as well.

    Wrote a simple program to calibrate each pin and then report if they're touched. However, I'm unable to time it with elapsedmillis. Pretty sure it is down to the touchread as when I comment out:

    Code:
     for (int i = 0; i < actualnoofpins; i++) {
        bigarray[3][i] = touchRead(bigarray[0][i]); //logs touchread value in 3rd row
        if (bigarray[3][i] > bigarray[2][i]) { //if touchread of pin bigger than fudged average
          bigarray[4][i] = 1;
        }
        else {
          bigarray[4][i] = 0;
        }
      }
    it works nicely. When touchread loops are left in, it just returns 1 or 0.

    Thanks for your help.



    Code:
    #include <elapsedMillis.h>
    elapsedMillis totaltime;
    elapsedMillis startMillis;
    
    int thresholdcap = 51;
    float fudgefactor = 1.3;
    int bigarray[4][8];
    int pinvals[8] = {0, 1, 29, 30, 16, 17, 0, 0}; //LA,B,C,D,E,F - NOTE need to change 0's at end..
    const int actualnoofpins = 6;
    #define SerialDebug //JUST comment out to remove serial monitor
    
    
    void setup() {
      // put your setup code here, to run once:
    #ifdef SerialDebug
      Serial.begin(9600);
      while ( !Serial && ( millis() - startMillis < 10000 ) );
      Serial.println("run ok up to here");
    #endif
    
      for (int i = 0; i < 8; i++) { //drops pinvals into bigarray
        bigarray[0][i] = pinvals[i];
      }
    
      for (int i = 0; i < 20; i++) { //Sums 20 touchreads on each pin
        for (int j = 0; j < actualnoofpins; j++) {
          bigarray[1][j] = touchRead(bigarray[0][j]) + bigarray[1][j];
        }
      }
    
      for (int j = 0; j < actualnoofpins; j++) { //Averages pin values then multiplies by fudge
        bigarray[2][j] = int(bigarray[1][j] * fudgefactor / 20);
      }
    
    #ifdef SerialDebug
      Serial.print("Pin calibration values are: ");
      for (int i = 0; i < actualnoofpins; i++) {
        Serial.print(bigarray[2][i]);
        Serial.print("  ");
      }
    #endif
    
    }
    
    void loop() {
    
      for (int i = 0; i < actualnoofpins; i++) {
        bigarray[3][i] = touchRead(bigarray[0][i]); //logs touchread value in 3rd row
        if (bigarray[3][i] > bigarray[2][i]) { //if touchread of pin bigger than fudged average
          bigarray[4][i] = 1;
        }
        else {
          bigarray[4][i] = 0;
        }
      }
    #ifdef SerialDebug
      Serial.print("Pin playing values are: ");
      for (int i = 0; i < actualnoofpins; i++) {
        Serial.print(bigarray[3][i]);
        Serial.print("  ");
      }
      Serial.print("time between loop is: ");
      Serial.println(startMillis);
    #endif
    
    
    
    }

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,060
    It isn't noted what isn't what working in what way that I can see? No posted output showing what is wrong?

    To guess I would suspect from code comment the ending code should read like this?
    Code:
    #ifdef SerialDebug
      Serial.print("Pin playing values are: ");
      for (int i = 0; i < actualnoofpins; i++) {
        Serial.print(bigarray[3][i]);
        Serial.print("  ");
      }
      Serial.print("time between loop is: ");
      Serial.println(startMillis);
      startMillis = 0;
    #endif

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,323
    I don't quite understand what you're saying is wrong, but I do see what looks like a problem in the code....

    This is the definition of "bigarray".

    Code:
    int bigarray[4][8];
    The legal range for the first index would be 0 to 3, since it's defined as 4.

    Here's one of the places it's used.

    Code:
          bigarray[4][i] = 1;
        }
        else {
          bigarray[4][i] = 0;
        }
    If you're using Arduino, you should see warnings about this problem. If you're compiling with something other than Arduino, maybe you should configure it to show you the compiler warnings.

  4. #4
    Quote Originally Posted by defragster View Post
    It isn't noted what isn't what working in what way that I can see? No posted output showing what is wrong?

    To guess I would suspect from code comment the ending code should read like this?
    Code:
    #ifdef SerialDebug
      Serial.print("Pin playing values are: ");
      for (int i = 0; i < actualnoofpins; i++) {
        Serial.print(bigarray[3][i]);
        Serial.print("  ");
      }
      Serial.print("time between loop is: ");
      Serial.println(startMillis);
      startMillis = 0;
    #endif
    Yes you are completely right, no idea how I managed to somehow remove it in one copy repeatedly. Thanks!

  5. #5
    Quote Originally Posted by PaulStoffregen View Post
    I don't quite understand what you're saying is wrong, but I do see what looks like a problem in the code....

    This is the definition of "bigarray".

    Code:
    int bigarray[4][8];
    The legal range for the first index would be 0 to 3, since it's defined as 4.

    Here's one of the places it's used.

    Code:
          bigarray[4][i] = 1;
        }
        else {
          bigarray[4][i] = 0;
        }
    If you're using Arduino, you should see warnings about this problem. If you're compiling with something other than Arduino, maybe you should configure it to show you the compiler warnings.

    Great catch - yes using arduino but missed the warnings - thanks was giving me some unexpected behaviour.

Posting Permissions

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