Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <OSCMessage.h>
- #include <SLIPEncodedSerial.h>
- // these two includes are enough for our purpose
- // we don't have to import the whole OSC library
- #include <FastLED.h>
- #define NUM_LEDS 60
- #define DATA_PIN 3
- #define BAUDRATE 57600
- // 9600 is too slow
- SLIPEncodedSerial SLIPSerial(Serial);
- int analogValue = 0;
- long oldTime = 0;
- long newTime = 0;
- CRGB leds[NUM_LEDS];
- void setup()
- {
- FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
- pinMode(13, OUTPUT);
- SLIPSerial.begin(BAUDRATE);
- }
- void loop()
- {
- static int sendval;
- int val = analogRead(2);
- //val = sendval;
- int numLedsToLight = map(sendval, 0, 1023, 1, 60);
- //first clear the existing led values
- FastLED.clear();
- for(int led = 0; led < numLedsToLight; led++)
- {
- leds[led] = CRGB::Blue;
- }
- //send LED values to led
- // read analog pin 0 and send an OSC message every 100 ms //
- newTime = millis();
- if (newTime-oldTime > 100){
- analogValue = analogRead(0);
- // create OSC message and give it an address
- OSCMessage msg("/analog");
- // add data to the OSC message
- msg.add(analogValue);
- // make and send a SLIP packet
- SLIPSerial.beginPacket();
- msg.send(SLIPSerial);
- SLIPSerial.endPacket();
- oldTime = newTime;
- }
- // look for incoming OSC messages:
- // first check if bytes are available
- while(SLIPSerial.available())
- {
- // create empty OSC Message
- OSCMessage msg;
- // fill OSC message with incoming bytes till you reach end of packet //
- while(!SLIPSerial.endofPacket())
- {
- int size = SLIPSerial.available();
- while (size--)
- {
- msg.fill(SLIPSerial.read());
- }
- }
- // now check the address (to compare the full adress, set the second argument to 0) //
- if (msg.fullMatch("/LD", 0))
- // then check if first item is an integer
- if (msg.isInt(0))
- {
- // use the integer argument for controlling the LED (zero = LOW, non-zero = HIGH) //
- digitalWrite(13, msg.getInt(0));
- }
- if (msg.fullMatch("/fad", 0))
- {
- // then check if first item is an integer
- if (msg.isInt(0))
- {
- // use the integer argument for controlling the number of lit LEDs
- sendval = (msg.getInt(0));
- }
- }
- FastLED.show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement