StartMillis not working when using touchread

fishy1

Member
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



}
 
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);
  [B]startMillis = 0;[/B]
#endif
 
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.
 
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);
  [B]startMillis = 0;[/B]
#endif

Yes you are completely right, no idea how I managed to somehow remove it in one copy repeatedly. Thanks!
 
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.
 
Back
Top