Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int MEAN_N = 10;
- int sensorSTORAGE_01[MEAN_N];
- int sensorSTORAGE_02[MEAN_N];
- int ledPins[] = {6,9};
- int sensorPins[] = {2,3};
- const int N_LEDS = 2; // How many LEDS
- const int N_SENSORS = 2; // How many SENSORS
- int minDistance = 300;
- int maxDistance = 800;
- int sensorReadings[N_SENSORS];
- unsigned long sensorTotals[N_SENSORS];
- unsigned int sensorAvg[N_SENSORS];
- int indexs[N_SENSORS];
- void setup() {
- Serial.begin(9600);
- // SETUP pinModes
- for(int i = 0; i < N_LEDS; i++)
- pinMode(ledPins[i], OUTPUT);
- for(int i = 0; i < N_SENSORS; i++)
- pinMode(sensorPins[i], INPUT);
- Serial.println(N_LEDS);
- Serial.println(N_SENSORS);
- Serial.println("SETUP COMPLETE");
- }
- void loop() {
- for(int i = 0; i < N_SENSORS; i++) {
- sensorReadings[i] = analogRead(sensorPins[i]);
- sensorReadings[i] = map(sensorReadings[i], minDistance, maxDistance, 255, 0);
- sensorReadings[i] = constrain(sensorReadings[i], 0, 255);
- addValueToStorage(sensorReadings[i], i);
- // Time for a little ILLUMINATION!!!
- analogWrite(ledPins[i], sensorAvg[i]);
- }
- }
- void addValueToStorage(int whatValue, int i) {
- // Adds the current reading to the right storage array
- int index = indexs[i];
- sensorTotals[i] = sensorTotals[i] - sensorSTORAGE_01[index];
- sensorSTORAGE_01[index] = whatValue;
- sensorTotals[i] = sensorTotals[i] + sensorSTORAGE_01[index];
- index ++;
- if (index >= MEAN_N)
- index = 0;
- indexs[i] = index;
- sensorAvg[i] = sensorTotals[i] / MEAN_N;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement