Advertisement
Guest User

Untitled

a guest
May 31st, 2010
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. const int MEAN_N = 10;
  2. int sensorSTORAGE_01[MEAN_N];
  3. int sensorSTORAGE_02[MEAN_N];
  4.  
  5.  
  6. int ledPins[]     = {6,9};
  7. int sensorPins[]  = {2,3};
  8.  
  9. const int N_LEDS = 2;        // How many LEDS
  10. const int N_SENSORS = 2;     // How many SENSORS
  11.  
  12. int minDistance = 300;
  13. int maxDistance = 800;
  14.  
  15.  
  16. int sensorReadings[N_SENSORS];
  17. unsigned long sensorTotals[N_SENSORS];
  18. unsigned int sensorAvg[N_SENSORS];
  19. int indexs[N_SENSORS];
  20.  
  21.  
  22. void setup()   {                
  23.    Serial.begin(9600);  
  24.  
  25.  
  26. // SETUP pinModes
  27.  
  28.   for(int i = 0; i < N_LEDS; i++)
  29.   pinMode(ledPins[i], OUTPUT);
  30.  
  31.   for(int i = 0; i < N_SENSORS; i++)
  32.   pinMode(sensorPins[i], INPUT);  
  33.  
  34.   Serial.println(N_LEDS);
  35.   Serial.println(N_SENSORS);
  36.   Serial.println("SETUP COMPLETE");
  37.  
  38. }
  39.  
  40.  
  41. void loop() {
  42.  
  43.   for(int i = 0; i < N_SENSORS; i++) {
  44.  
  45.       sensorReadings[i] = analogRead(sensorPins[i]);
  46.  
  47.       sensorReadings[i] = map(sensorReadings[i], minDistance, maxDistance, 255, 0);
  48.       sensorReadings[i] = constrain(sensorReadings[i], 0, 255);
  49.  
  50.       addValueToStorage(sensorReadings[i], i);
  51.            
  52.       // Time for a little ILLUMINATION!!!
  53.      
  54.       analogWrite(ledPins[i], sensorAvg[i]);    
  55.  
  56.   }
  57.  
  58.            
  59. }
  60.  
  61. void addValueToStorage(int whatValue, int i) {
  62.  
  63.  // Adds the current reading to the right storage array
  64.  
  65.  int index = indexs[i];
  66.  
  67.   sensorTotals[i] = sensorTotals[i] - sensorSTORAGE_01[index];
  68.   sensorSTORAGE_01[index] = whatValue;
  69.   sensorTotals[i] = sensorTotals[i] + sensorSTORAGE_01[index];
  70.  
  71.   index ++;
  72.  
  73.   if (index >= MEAN_N)              
  74.   index = 0;  
  75.  
  76. indexs[i] = index;
  77.  
  78.   sensorAvg[i] = sensorTotals[i] / MEAN_N;        
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement