Shifting data in an array

I have an array of ints and I want to shift all of it to the left then add a new int into the new space created; so if I were to use it on an array like the below...

{5, 5, 5, 5, 5, 5, 5}

I would shift all data to the left and add a new int to the end (in this case I'll add 6); this would result in it looking like this...

{5, 5, 5, 5, 5, 5, 6}

the reason for this is that I want to move a 3d printer head around and to save the last 150 locations it was at but if i just have it fill an array the array overflows and the arduino crashes; i want to have an array of those previous locations and i want to shift it so it will be up to date and not overflow.

Try something like:

   memcpy(array, &array[1], sizeof(array) - sizeof(int));
   array[6] =  newValue;

Its not necessary to re-arrange the data in the array. Just increment your starting index instead, and each time you increment the index, wrap it around if it goes past the end.

Regards,
Ray L.

Google circular buffer. Check out the implementation of HarwareSerial in the core for a good example.

EconJack I take it it would look something like the below?

 int RobotArray[200];

 memcpy(RobotArray, &RobotArray[1], sizeof(RobotArray) - sizeof(int))
 RobotArray[200] = newvalue;

RayLivingston I'm not sure how you would go about that? does that mean i can somehow change the index of the array? so myarray[5] would become myarray[6]? How do you edit that in an array, I thought you could only change the data that was being held within?

Delta_G Cheers I'll give it a look see if I can put together something useful many thanks!

Almost. The statement:

 int RobotArray[200];

gives you 200 elements, ranging from 0 through 199. So you would need to have:

 RobotArray[199] = newvalue;

You need to tell us how you want to use the data in the array.

Normally with a circular buffer you are only interested in adding to the head and retrieving from the tail.

However you could access any value by counting (in a circular fashion) from the tail. The tail will always have the oldest value.

There is code for a circular buffer in Yet Another Software Serial. It is essentially the same as in HardwareSerial - but might be easier to find.

...R

150 ints is 300 bytes. The inner loop of memcpy() is exceptionally efficient so it might be 6 machine instructions long. That's 1800 instructions. On a 16MHz Arduino, that's more than 11 microseconds. If you are driving stepper motors, that's a huge amount of time to spend copying data.

Incrementing two counters (head and tail) then checking if they need to wrap around is probably easy to do in less than 64 instructions, or just a few thousandths of a microsecond. Let the data stand still and change your pointers to the data.

Think about how you would do this if you had things in a line of physical boxes. You wouldn't move the contents of each box down - you'd keep track of which box was the last one you put something in and move along the boxes as samples come in - discarding the outdated value and putting in the latest one. When you reached the end, you'd go back to the start.

That's what a circular buffer is.

Thanks for the Circular buffer tip, done some reading and it's a very clever solution to the problem! If anyone else is reading this looking to solve this problem I used the below video to get a better understanding of a circular buffer.

https://www.youtube.com/watch?v=fdCAsZkjpSg