Guest User

Untitled

a guest
Mar 23rd, 2016
285
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Adafruit_NeoPixel.h>
  2.  
  3.  
  4. #define READPIN1       0
  5. #define READPIN2       1
  6. #define READPIN3       2
  7. #define READPIN4       3
  8.  
  9. #define READDIGITALPIN1       2
  10. #define READDIGITALPIN2       5
  11.  
  12.  
  13. #define WRITEPIN1      6
  14. #define NUMPIXELS      24
  15.  
  16. Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, WRITEPIN1, NEO_GRB + NEO_KHZ800);
  17. int ledIndex = 0;
  18. int frame = 0;
  19. String in = "a";
  20.  
  21. void setup()
  22. {
  23.   pinMode(READDIGITALPIN1, INPUT);
  24.    pixels.begin();
  25.   Serial.begin(9600);
  26.   Serial.setTimeout(100);
  27. }
  28.  
  29. //from http://www.elcojacobs.com/eleminating-noise-from-sensor-readings-on-arduino-with-digital-filtering/
  30. int analogReadFiltered(int sensorpin, int readCount){
  31.    // read multiple values and sort them to take the mode
  32.    int sampleCountHalf = readCount/6;
  33.    int sortedValues[readCount];
  34.    for(int i=0;i<readCount;i++){
  35.      int value = analogRead(sensorpin);
  36.      int j;
  37.      if(value<sortedValues[0] || i==0){
  38.         j=0; //insert at first position
  39.      }
  40.      else{
  41.        for(j=1;j<i;j++){
  42.           if(sortedValues[j-1]<=value && sortedValues[j]>=value){
  43.             // j is insert position
  44.             break;
  45.           }
  46.        }
  47.      }
  48.      for(int k=i;k>j;k--){
  49.        // move all values higher than current reading up one position
  50.        sortedValues[k]=sortedValues[k-1];
  51.      }
  52.      sortedValues[j]=value; //insert current reading
  53.    }
  54.    //return scaled mode of 10 values
  55.    float returnval = 0;
  56.    for(int i=readCount/2-sampleCountHalf;i<(readCount/2+sampleCountHalf);i++){
  57.      returnval +=sortedValues[i];
  58.    }
  59.    return = returnval/(sampleCountHalf+sampleCountHalf);
  60. }
  61.  
  62.  
  63. void loop()
  64. {
  65.   // 0 - 1024 ( 0 - 1023 )
  66.   int val1 = analogReadFiltered(READPIN1, 26);
  67.   int val2 = analogReadFiltered(READPIN2, 12);
  68.   int val3 = analogReadFiltered(READPIN3, 12);
  69.  
  70.  
  71.   int digital1 = digitalRead(READDIGITALPIN1);
  72.   int digital2 = digitalRead(READDIGITALPIN2);
  73.  
  74.   float values[] = {val1, val2, val3, digital1, digital2};
  75.   int count = 5;
  76.  
  77.   Serial.flush();
  78.  
  79.   for(int i=0; i<count; i++)
  80.   {
  81.     Serial.print(values[i]);
  82.     Serial.print(",");
  83.   }
  84.  
  85.   Serial.println();
  86.  
  87.  
  88.   if(frame % 2 == 0)
  89.   {
  90.     while (Serial.available() > 0)
  91.     {
  92.       in = Serial.readStringUntil('\n');
  93.     }    
  94.    
  95.     ledIndex++;
  96.    
  97.     int r,g,b;
  98.     int skipSize = 8;
  99.  
  100.     if(in.charAt(0) == 'a')  //default
  101.     {
  102.       r = 0;
  103.       g = 50;
  104.       b = 200;
  105.     }
  106.     else if(in.charAt(0) == 'b')  //carry pallet
  107.     {
  108.       r = 0;
  109.       g = 240;
  110.       b = 50;
  111.     }
  112.     else if(in.charAt(0) == 'c')  //pallet delivered
  113.     {
  114.       r = 240;
  115.       g = 200;
  116.       b = 0;
  117.       skipSize = 4;
  118.       ledIndex--;
  119.     }  
  120.    
  121.    
  122.    
  123.     for(int i=0;i<NUMPIXELS;i++)
  124.     {
  125.      
  126.       int index = (ledIndex+i) % NUMPIXELS;
  127.      
  128.       if(index % skipSize != 0)
  129.       {
  130.         pixels.setPixelColor(i, 0, 0, 0);
  131.       }
  132.       else
  133.       {
  134.         pixels.setPixelColor(i, r, g, b);
  135.       }
  136.     }
  137.    
  138.     pixels.show();      
  139.   }
  140.  
  141.  frame++;
  142.   delay(10);
  143. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×