Save the millis into the different variable each time the event happen

Hi!

Is this the best way save the millis into the different variable each time the sensor value is greater than zero?

const unsigned long space = 2000;
unsigned long truckTime = 0;
unsigned long save [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41};

byte state = 0;
int motionSensorValue = A0;

void setup() {
  pinMode (motionSensorValue, INPUT);
  Serial.begin (9600);
}

void loop() {
  unsigned long currentTime = millis();

  if (currentTime - truckTime >= space) {
    truckTime = currentTime;
    motionSensorValue  = analogRead (A0);

    if (motionSensorValue != 0) {
      state ++;
      switch (state) {
        case 1:
          save[0] = currentTime;
          break;
        case 2:
          save[1] = currentTime;
          break;
        case 3:
          save[2] = currentTime;
          break;
        case 4:
          save[3] = currentTime;
          break;
        case 5:
          save[4] = currentTime;
          break;
        case 6:
          save[5] = currentTime;
          break;
        case 7:
          save[6] = currentTime;
          break;
        case 8:
          save[7] = currentTime;
          break;
        case 9:
          save[8] = currentTime;
          break;
        case 10:
          save[9] = currentTime;
          break;
        case 11:
          save[10] = currentTime;
          break;
        case 12:
          save[11] = currentTime;
          break;
        case 13:
          save[12] = currentTime;
          break;
        case 14:
          save[13] = currentTime;
          break;
        case 15:
          save[14] = currentTime;
          break;
        case 16:
          save[15] = currentTime;
          break;
        case 17:
          save[16] = currentTime;
          break;
        case 18:
          save[17] = currentTime;
          break;
        case 19:
          save[18] = currentTime;
          break;
        case 20:
          save[19] = currentTime;
          break;

        case 21:
          save[20] = currentTime;
          break;
        case 22:
          save[21] = currentTime;
          break;
        case 23:
          save[22] = currentTime;
          break;
        case 24:
          save[23] = currentTime;
          break;
        case 25:
          save[24] = currentTime;
          break;
        case 26:
          save[25] = currentTime;
          break;
        case 27:
          save[26] = currentTime;
          break;
        case 28:
          save[27] = currentTime;
          break;
        case 29:
          save[28] = currentTime;
          break;
        case 30:
          save[29] = currentTime;
          break;
        case 31:
          save[30] = currentTime;
          break;
        case 32:
          save[31] = currentTime;
          break;
        case 33:
          save[32] = currentTime;
          break;
        case 34:
          save[33] = currentTime;
          break;
        case 35:
          save[34] = currentTime;
          break;
        case 36:
          save[35] = currentTime;
          break;
        case 37:
          save[36] = currentTime;
          break;
        case 38:
          save[37] = currentTime;
          break;
        case 39:
          save[38] = currentTime;
          break;
        case 40:
          save[39] = currentTime;
          break;
      }
    }

    Serial.print ("motionSensor value: ");
    Serial.println (motionSensorValue);
    Serial.print ("save 1: ");
    Serial.println (save[0]);
    Serial.print ("save 2: ");
    Serial.println (save[1]);
    Serial.print ("save 3: ");
    Serial.println (save[2]);
    Serial.print ("save 4: ");
    Serial.println (save[3]);
    Serial.print ("save 5: ");
    Serial.println (save[4]);
    Serial.print ("save 6: ");
    Serial.println (save[5]);
    Serial.print ("save 7: ");
    Serial.println (save[6]);
    Serial.print ("save 8: ");
    Serial.println (save[7]);
    Serial.print ("save 9: ");
    Serial.println (save[8]);
    Serial.print ("save 10: ");
    Serial.println (save[9]);
    Serial.print ("save 11: ");
    Serial.println (save[10]);
    Serial.print ("save 12: ");
    Serial.println (save[11]);
    Serial.print ("save 13: ");
    Serial.println (save[12]);
    Serial.print ("save 14: ");
    Serial.println (save[13]);
    Serial.print ("save 15: ");
    Serial.println (save[14]);
    Serial.print ("save 16: ");
    Serial.println (save[15]);
    Serial.print ("save 17: ");
    Serial.println (save[16]);
    Serial.print ("save 18: ");
    Serial.println (save[17]);
    Serial.print ("save 19: ");
    Serial.println (save[18]);
    Serial.print ("save 20: ");
    Serial.println (save[19]);
    Serial.print ("save 21: ");
    Serial.println (save[20]);
    Serial.print ("save 22: ");
    Serial.println (save[21]);
    Serial.print ("save 23: ");
    Serial.println (save[22]);
    Serial.print ("save 24: ");
    Serial.println (save[23]);
    Serial.print ("save 25: ");
    Serial.println (save[24]);
    Serial.print ("save 26: ");
    Serial.println (save[25]);
    Serial.print ("save 27: ");
    Serial.println (save[26]);
    Serial.print ("save 28: ");
    Serial.println (save[27]);
    Serial.print ("save 29: ");
    Serial.println (save[28]);
    Serial.print ("save 30: ");
    Serial.println (save[29]);
    Serial.print ("save 31: ");
    Serial.println (save[30]);
    Serial.print ("save 32: ");
    Serial.println (save[31]);
    Serial.print ("save 33: ");
    Serial.println (save[32]);
    Serial.print ("save 34: ");
    Serial.println (save[33]);
    Serial.print ("save 35: ");
    Serial.println (save[34]);
    Serial.print ("save 36: ");
    Serial.println (save[35]);
    Serial.print ("save 37: ");
    Serial.println (save[36]);
    Serial.print ("save 38: ");
    Serial.println (save[37]);
    Serial.print ("save 39: ");
    Serial.println (save[38]);
    Serial.print ("save 40: ");
    Serial.println (save[39]);
    Serial.print ("state: ");
    Serial.println (state);
  }
}

look at that switch. Couldn't it be

save[state - 1] = currentTime;

And that Serial print section could really use a for loop.

Yes it's also works well, its took me some minutes understand why.
Its a little bit more sophisticated.
I just wonder if there is some way make this dramatically more short.

Write it out with the changes mentioned. It's going to be pretty short then.

GrOnThOs:
Yes it's also works well, its took me some minutes understand why.
Its a little bit more sophisticated.
I just wonder if there is some way make this dramatically more short.

if you have time, look into Circular Buffers.

you can do it and will end up a little more elegant.

const unsigned long space = 2000;
unsigned long truckTime = 0;

byte state = 0;
int motionSensorValue = 0;
unsigned long save [] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

void setup() {
  pinMode (motionSensorValue, INPUT);
  Serial.begin (9600);
}

void loop() {
  unsigned long currentTime = millis();

  if (currentTime - truckTime >= space) {
    truckTime = currentTime;
    motionSensorValue  = analogRead (A0);

    if (motionSensorValue != 0) {
      state ++;
      switch (state) {
        case 1:
          save[state - 1] = currentTime;
          break;
        case 2:
          save[state - 1] = currentTime;
          break;
        case 3:
          save[state - 1] = currentTime;
          break;
        case 4:
          save[state - 1] = currentTime;
          break;
        case 5:
          save[state - 1] = currentTime;
          break;
        case 6:
          save[state - 1] = currentTime;
          break;
        case 7:
          save[state - 1] = currentTime;
          break;
        case 8:
          save[state - 1] = currentTime;
          break;
        case 9:
          save[state - 1] = currentTime;
          break;
        case 10:
          save[state - 1] = currentTime;
          break;
        case 11:
          save[state - 1] = currentTime;
          break;
        case 12:
          save[state - 1] = currentTime;
          break;
        case 13:
          save[state - 1] = currentTime;
          break;
        case 14:
          save[state - 1] = currentTime;
          break;
        case 15:
          save[state - 1] = currentTime;
          break;
        case 16:
          save[state - 1] = currentTime;
          break;
        case 17:
          save[state - 1] = currentTime;
          break;
        case 18:
          save[state - 1] = currentTime;
          break;
        case 19:
          save[state - 1] = currentTime;
          break;
        case 20:
          save[state - 1] = currentTime;
          break;

        case 21:
          save[state - 1] = currentTime;
          break;
        case 22:
          save[state - 1] = currentTime;
          break;
        case 23:
          save[state - 1] = currentTime;
          break;
        case 24:
          save[state - 1] = currentTime;
          break;
        case 25:
          save[state - 1] = currentTime;
          break;
        case 26:
          save[state - 1] = currentTime;
          break;
        case 27:
          save[state - 1] = currentTime;
          break;
        case 28:
          save[state - 1] = currentTime;
          break;
        case 29:
          save[state - 1] = currentTime;
          break;
        case 30:
          save[state - 1] = currentTime;
          break;
        case 31:
          save[state - 1] = currentTime;
          break;
        case 32:
          save[state - 1] = currentTime;
          break;
        case 33:
          save[state - 1] = currentTime;
          break;
        case 34:
          save[state - 1] = currentTime;
          break;
        case 35:
          save[state - 1] = currentTime;
          break;
        case 36:
          save[state - 1] = currentTime;
          break;
        case 37:
          save[state - 1] = currentTime;
          break;
        case 38:
          save[state - 1] = currentTime;
          break;
        case 39:
          save[state - 1] = currentTime;
          break;
        case 40:
          save[state - 1] = currentTime;
          break;
      }
    }
    Serial.print ("motion Sensor value: "); Serial.println (motionSensorValue);
    for (int i = 0; i <= 40; i ++) {
      Serial.print ("save "); Serial.print (i); Serial.print (": ");  Serial.println (save [i]);
    }
    Serial.print ("state: "); Serial.println (state);

  }
}

@ Delta_G Believe it or not, it took me several days to find the way.

@ BulldogLowell Thank you for the information, but it is not fair comparison if you include library.

      switch (state) {
        case 1:
          save[state - 1] = currentTime;
          break;
        case 2:
          save[state - 1] = currentTime;
          break;

In the immortal words of ex-PFC Wintergreen, “Too prolix”

GrOnThOs:
@ BulldogLowell Thank you for the information, but it is not fair comparison if you include library.

I didn’t say use a library, I mentioned the technique…

and if you did use a library, so what?

here you’ve learned enough to use an array:

unsigned long save [] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

and yet by here you’ve already forgotten everything you learned:

    if (motionSensorValue != 0) {
      state ++;
      switch (state) {

state appears to be unbounded. There's nothing preventing it from increasing too high and writing off the end of the array. This isn't an error in C++. It will just write your data into areas of memory that were being used for something else, like making your program actually work.

Always set an array bound and check that before using the array index out of bounds.

const int NumSaves = 20;
unsigned long save[NumSaves];
...
...
...
    state++;
    if(state > NumSaves) state=0;  //reset to zero and over-write the earlier readings

Of course this is just an example. You may want your thing to just stop and print everything when the number is exceeded.

const unsigned long space = 2000;
unsigned long truckTime = 0;

int motionSensorValue = 0;
unsigned long save [] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
byte moveToTheNext;

void setup() {
  pinMode (motionSensorValue, INPUT);
  Serial.begin (9600);
}

void loop() {
  unsigned long currentTime = millis();

  if (currentTime - truckTime >= space) {
    truckTime = currentTime;
    motionSensorValue  = analogRead (A0);

    if (motionSensorValue != 0) {
      save [moveToTheNext] = currentTime;
      moveToTheNext++;
    }

    Serial.print ("motion Sensor value: "); Serial.println (motionSensorValue);
    for (byte i = 0; i <= 40; i ++) {
      Serial.print ("save "); Serial.print (i); Serial.print (": ");  Serial.println (save [i]);
    }

  }
}

@ BulldogLowell Thank you very mach!

@MorganS Yes, I’ll take care of this.

by the way, a simple alternative to this aberration:

unsigned long save [] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

would be this:

unsigned long save [40];

BulldogLowell:
by the way, a simple alternative to this aberration:

unsigned long save [] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

would be this:

unsigned long save [40];

for (byte i = 0; i <= 40; i ++) {BZZZT!

I couldn’t be bothered to count all those zeroes!

const unsigned long space = 2000;
unsigned long truckTime = 0;

int motionSensorValue = 0;
unsigned long save [41] = {};
byte moveToTheNext;
bool doNotSave = false;

void setup() {
  pinMode (motionSensorValue, INPUT);
  Serial.begin (9600);
}

void loop() {
  unsigned long currentTime = millis();

  if (currentTime - truckTime >= space) {
    truckTime = currentTime;
    motionSensorValue  = analogRead (A0);

    if (motionSensorValue != 0 && doNotSave == false) {
      save [moveToTheNext] = currentTime;
      moveToTheNext++; if (moveToTheNext == 41) {
        doNotSave = true;
      }
    }

    Serial.print ("motion Sensor value: "); Serial.println (motionSensorValue);
    for (byte i = 0; i <= 40; i ++) {
      Serial.print ("save "); Serial.print (i); Serial.print (": ");  Serial.println (save [i]);
    }
  }
}

So here is the final code, two hundred lines shorter!

    if(state > NumSaves) state=0;  //reset to zero and over-write the earlier readings

I’ll take “Common C++ Errors” for $100, Alex.

“When an error in logic causes data to be read from or written to 1 position past the end of an array.”

BZZZT

What is a Fence Post Error?

Correct!

unsigned long save [41] = {};

moveToTheNext++; if (moveToTheNext == 41) {

for (byte i = 0; i <= 40; i ++) {

When you are using the same value (in this case the size of the save array), it is best practice to assign that value to a named constant (like SAVE_ARR_SIZE or something else descriptive) and use only that named constant in your code. That guards against unfortunate typos which could be caused by the manual retyping, and also means that changing the size of the array requires only modifying one place, rather than the 3 (at least!) places that your code requires.

When you are using the same value (in this case the size of the save array), it is best practice to assign that value to a named constant (like SAVE_ARR_SIZE or something else descriptive) and use only that named constant in your code. That guards against unfortunate typos which could be caused by the manual retyping, and also means that changing the size of the array requires only modifying one place, rather than the 3 (at least!) places that your code requires.

or just increase by one the area size?

unsigned long save [42] = {};

...and so proceed, ad infinitum

...and so proceed, ad infinitum

But it will be a position that will never be used because of "doNotSave" variable.

if (moveToTheNext == 41) {
        doNotSave = true;
      }

Much simpler to define a single constant, and use that single value everywhere, than have to remember to add or subtract a number, or waste precious RAM.