problem with manipulating array values

if (i <= rows) {
  if (currentMillis - previousMillis2 >= interval2) {
      previousMillis2 = currentMillis;
      array1[i] = buttonPresses;
      //Serial.println(i); 
      Serial.println(buttonPresses);  
      buttonPresses = 0;   
      i++;
  }
} else {
  for (int y = 0; y < rows; y++) {
    array1[y] = array1[y+1];
  }
  i--;
}

Once the program reaches the last index of the array, the program should overwrite the 1st index with the value from the 2nd index, and so on, moving the values of the array “up” and freeing the last index.

i’m having trouble with

for (int y = 0; y < rows; y++) {
    array1[y] = array1[y+1];
  }

is causing crazy behaviour.

I’m also getting these warning messages during compile.

warning: iteration 2 invokes undefined behavior [-Waggressive-loop-optimizations]

array1[y] = array1[y+1];

^

note: containing loop

for (int y = 0; y < rows; y++) {

^

  for (int y = 0; y < rows; y++) {
    array1[y] = array1[y+1];
  }

How is array1 declared ?
How many elements does it have ?

  for (int y = 0; y < rows; y++) {
    array1[y] = array1[y+1];
  }

Is rows the number of elements in the array?
What happends when y = rows - 1?

Please post the complete code.

Hi. Here’s the complete code. As of now, I’ve set the number of elements of the array to 4 elements and it’s set to count the button presses within a 5 second interval.

byte switchPin = 3;                    // switch is connected to pin 3
byte ledPin = 13;                      // led on pin 13
byte buttonPresses = 0;                // how many times the button has been pressed 
byte lastPressCount = 0;               // to keep track of last press count

unsigned long previousMillis = 0; //used as delay in reading the button press
const long interval = 300;

unsigned long previousMillis2 = 0; //used as time interval in counting the button presses
const long interval2 = 5000;

const int rows = 3;
int array1[rows];
int i = 0;


void setup() {
  pinMode(switchPin, INPUT);          // Set the switch pin as input
  digitalWrite(switchPin, HIGH);      // set pullup resistor
  Serial.begin(9600);                 // Set up serial communication at 9600bps
}

void loop(){
  unsigned long currentMillis = millis();
  if (digitalRead(switchPin) == LOW)  // check if button was pressed
  {

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    buttonPresses++;  
  }                      
  }


  
  if ((lastPressCount != buttonPresses) && (buttonPresses != 0))             // only do output if the count has changed
  {
    Serial.print ("Button press count = ");          // out to serial
    Serial.println(buttonPresses, DEC);
    
    lastPressCount = buttonPresses;    // track last press count

  }

if (i <= rows) { //storing values to the array
  if (currentMillis - previousMillis2 >= interval2) {
      previousMillis2 = currentMillis;
      array1[i] = buttonPresses;
      //Serial.println(i); 
      Serial.println(buttonPresses);  
      buttonPresses = 0;   
      i++;
  }
} else { /*once the last element is reached, loop through the array
  moving the elements one index lower. removing the 1st value. 
  then the program should continue storing the latest value 
  to the last element of the array*/
  for (int y = 0; y < rows; y++) {
    array1[y] = array1[y+1];
  }
  i--;
}
}

Can you please describe in words what the program is supposed to do ?

warning: iteration 2 invokes undefined behavior [-Waggressive-loop-optimizations]

array1[y] = array1[y+1];

In this instance, that error message means when y = 2 the code produced behaves in an unpredictable way.

You have defined a 3 element array, which has elements array1[0], array1[1], and array1[2], then you are trying to do the following:

array1[2] = array1[3]

david_2018:
In this instance, that error message means when y = 2 the code produced behaves in an unpredictable way.

You have defined a 3 element array, which has elements array1[0], array1[1], and array1[2], then you are trying to do the following:

array1[2] = array1[3]

I see, so the program is trying to get data from a nonexistent index, that’s why it behaves in an unpredictable way.

With that, I tried changing the loop into

for (int y = 1; y < rows; y++) {
array1[y-1] = array1[y];
}

so basically the index should always fall within the range I set. I’ve compiled this and so far no warning messages. will have to wait till I get to upload this to a board. Thanks!

UKHeliBob:
Can you please describe in words what the program is supposed to do ?

The program counts the button presses within a 5 seconds(this will be changed to 1 hour in the final version) and store it in an array. Once the array is filled up, the program should continue counting and storing every 5 seconds but will move the values of the array one index to the left.

the program is designed this way because I will be displaying the values of the array to a screen.

Thanks. I will go back and look at the code now that I know what you want it to do

What is the purpose of the 300 millisecond interval ?