Advertisement
Guest User

Untitled

a guest
Jun 5th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. #include <OSCMessage.h>
  2. #include <SLIPEncodedSerial.h>
  3. // these two includes are enough for our purpose
  4. // we don't have to import the whole OSC library
  5.  
  6. #include <FastLED.h>
  7.  #define NUM_LEDS 60
  8.  #define DATA_PIN 3
  9.  
  10. #define BAUDRATE 57600
  11. // 9600 is too slow
  12.  
  13. SLIPEncodedSerial SLIPSerial(Serial);
  14.  
  15. int analogValue = 0;
  16. long oldTime = 0;
  17. long newTime = 0;
  18.  
  19. CRGB leds[NUM_LEDS];
  20.  
  21. void setup()
  22. {
  23.   FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
  24.    pinMode(13, OUTPUT);
  25.   SLIPSerial.begin(BAUDRATE);
  26. }
  27.  
  28. void loop()
  29. {
  30.  
  31.   static int sendval;
  32.   int val = analogRead(2);
  33.   //val = sendval;
  34.   int numLedsToLight = map(sendval, 0, 1023, 1, 60);
  35.  
  36.        //first clear the existing led values
  37.   FastLED.clear();
  38.   for(int led = 0; led < numLedsToLight; led++)
  39.   {
  40.     leds[led] = CRGB::Blue;
  41.   }
  42.  
  43.   //send LED values to led
  44.  
  45.    // read analog pin 0 and send an OSC message every 100 ms //
  46.    newTime = millis();
  47.    if (newTime-oldTime > 100){
  48.      analogValue = analogRead(0);
  49.      // create OSC message and give it an address
  50.      OSCMessage msg("/analog");
  51.      // add data to the OSC message
  52.      msg.add(analogValue);
  53.      // make and send a SLIP packet
  54.      SLIPSerial.beginPacket();
  55.      msg.send(SLIPSerial);
  56.      SLIPSerial.endPacket();
  57.      oldTime = newTime;
  58.    }
  59.  
  60.  // look for incoming OSC messages:
  61.    
  62.    // first check if bytes are available
  63.    while(SLIPSerial.available())
  64.    {
  65.      // create empty OSC Message
  66.      OSCMessage msg;
  67.      // fill OSC message with incoming bytes till you reach end of packet //
  68.      while(!SLIPSerial.endofPacket())
  69.      {
  70.          int size = SLIPSerial.available();
  71.            while (size--)
  72.            {
  73.              msg.fill(SLIPSerial.read());
  74.            }
  75.      }    
  76.    
  77.      // now check the address (to compare the full adress, set the second argument to 0) //
  78.      if (msg.fullMatch("/LD", 0))
  79.    
  80.        // then check if first item is an integer
  81.        if (msg.isInt(0))
  82.        {
  83.          // use the integer argument for controlling the LED (zero = LOW, non-zero = HIGH) //
  84.          digitalWrite(13, msg.getInt(0));
  85.        }
  86.        
  87.      if (msg.fullMatch("/fad", 0))
  88.      {
  89.        // then check if first item is an integer
  90.        if (msg.isInt(0))
  91.        {
  92.          // use the integer argument for controlling the number of lit LEDs
  93.          sendval = (msg.getInt(0));
  94.        }
  95.        
  96.      }
  97.      
  98.    
  99. FastLED.show();
  100.  }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement