Skip to content

Instantly share code, notes, and snippets.

@kasperkamperman
Created March 27, 2015 19:43
Show Gist options
  • Save kasperkamperman/356f4eec64c5674e31e3 to your computer and use it in GitHub Desktop.
Save kasperkamperman/356f4eec64c5674e31e3 to your computer and use it in GitHub Desktop.
circular buffer to store and plot values
// this code is not a working sketch
int circularBufferSize;
int circularBufferIndex;
int [][] circularBuffer;
circularBufferSize = 512;
circularBufferIndex = 0;
circularBuffer = new int [6][circularBufferSize];
// write values to circular buffer when received from Arduino analog
for (int i=0; i<6; i++)
{
circularBuffer[i][circularBufferIndex] = 0; //valueFromArduino
if(circularBufferIndex<circularBufferSize-1) circularBufferIndex++;
else circularBufferIndex=0;
}
void drawDataPlot(int xPos, int yPos, int w, int h, int buffer)
{
float x;
float y;
// draw eggshell background
fill(255, 253, 248);
rect(xPos, yPos, w, h);
// add position in width and height.
// for mapping (after drawing the background)
w = xPos+w;
h = yPos+h;
int padding = 5;
noFill();
strokeWeight(1);
stroke(120);
// beginShape is a fast way to draw lines!
beginShape();
for (int i=0; i<circularBufferSize-1; i++)
{
x = map(i, 0, circularBufferSize, xPos+padding, w-padding);
// use sensorAvgMinValue and sensorAvgMaxValue to scale
y = map(getValueFromCircularBuffer(buffer, i), 0, 1023, h-padding, yPos+padding);
vertex(x,y);
}
endShape();
noStroke();
}
int getValueFromCircularBuffer(int buffer, int index)
{
// circularBufferIndex +1 is the smallest (the oldest in the array)
int idx = (circularBufferIndex + 1 + index) % circularBufferSize;
return circularBuffer[buffer][idx];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment