Skip to content

Instantly share code, notes, and snippets.

@jasonhejna
Created July 30, 2015 22:50
Show Gist options
  • Save jasonhejna/a87807961b6fdfeabab9 to your computer and use it in GitHub Desktop.
Save jasonhejna/a87807961b6fdfeabab9 to your computer and use it in GitHub Desktop.
int photo_pin = 1;
int light_pin = 10;
int light_hist[50] = {};
int loop_count = 0;
void setup() {
pinMode(light_pin, OUTPUT);
}
void loop() {
//read the value from our photo resistor
float light_reading = analogRead(photo_pin);
//loop through the array from start to finish. A bit tricky since the values are just appended to the array
int read_start = loop_count + 1;
if ( read_start > 50) { read_start = 0 }
int read_stop = read_start + 50;
int running_avg = 0;
int hist_key = 0;
for (i = read_start; i <= read_stop; i = i + 1) {
hist_key = i;
//keeps us reading sequentially
if (i > 50) { hist_key = i - 50; }
running_avg = int((light_hist[loop_count] + running_avg) / 2);
}
//assign current light reading to the running light_hist array
light_hist[loop_count] = light_reading;
//keep a running count for ever iteration through loop. Start over at 50
loop_count = loop_count + 1;
if (loop_count > 50){
loop_count = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment