Skip to content

Instantly share code, notes, and snippets.

@Alexs2424
Created May 20, 2019 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alexs2424/f9ac29a5af45a0bb8a99bea4a9c9179b to your computer and use it in GitHub Desktop.
Save Alexs2424/f9ac29a5af45a0bb8a99bea4a9c9179b to your computer and use it in GitHub Desktop.
Here's the code I wrote for use with my final project.
// Adafruit IO Subscription Example
//
// Adafruit invests time and resources providing this open source code.
// Please support Adafruit and open source hardware by purchasing
// products from Adafruit!
//
// Written by Todd Treece for Adafruit Industries
// Copyright (c) 2016 Adafruit Industries
// Licensed under the MIT license.
//
// All text above must be included in any redistribution.
/************************** Configuration ***********************************/
// edit the config.h tab and enter your Adafruit IO credentials
// and any additional configuration needed for WiFi, cellular,
// or ethernet clients.
#include "config.h"
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
#define LED_PIN 4
// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 60
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
/************************ Program Starts Here *******************************/
// set up the 'counter' feed
AdafruitIO_Feed *counter = io.feed("albumColor");
void setup() {
// start the serial connection
Serial.begin(115200);
// wait for serial monitor to open
while(! Serial);
/* NEOPIXEL SETUP */
// These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
// Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
// END of Trinket-specific code.
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
/* END NEOPIXEL SETUP */
Serial.print("Connecting to Adafruit IO");
// start MQTT connection to io.adafruit.com
io.connect();
// set up a message handler for the count feed.
// the handleMessage function (defined below)
// will be called whenever a message is
// received from adafruit io.
counter->onMessage(handleMessage);
// wait for an MQTT connection
// NOTE: when blending the HTTP and MQTT API, always use the mqttStatus
// method to check on MQTT connection status specifically
while(io.mqttStatus() < AIO_CONNECTED) {
Serial.print(".");
delay(500);
}
// Because Adafruit IO doesn't support the MQTT retain flag, we can use the
// get() function to ask IO to resend the last value for this feed to just
// this MQTT client after the io client is connected.
counter->get();
// we are connected
Serial.println();
Serial.println(io.statusText());
}
void loop() {
// io.run(); is required for all sketches.
// it should always be present at the top of your loop
// function. it keeps the client connected to
// io.adafruit.com, and processes any incoming data.
io.run();
// Because this sketch isn't publishing, we don't need
// a delay() in the main program loop.
}
// this function is called whenever a 'counter' message
// is received from Adafruit IO. it was attached to
// the counter feed in the setup() function above.
void handleMessage(AdafruitIO_Data *data) {
Serial.print("received <- ");
Serial.println(data->value());
//store each in a string that will eventually be each color value.
char red[5];
char blue[5];
char green[5]; //might not have to declare size here
int dashIndex = 0;
char *colorData = data->value();
// colorData = data->value();
int numDigitsRed = 0; //these were 1
int numDigitsGreen = 0;
int numDigitsBlue = 0;
for (int i = 0; i < 12; i++) {
// Serial.print(colorData[i]);
Serial.println();
if (colorData[i] == '-') {
dashIndex++;
Serial.print("found the dash!");
Serial.println("");
continue;
}
if (dashIndex == 0) { //gives the red value
if (colorData[i] != 'x') {
// Serial.print("Dash index of 0, adding ");
red[numDigitsRed] = colorData[i]; //was numDigitsRed -1
Serial.println("What happen");
Serial.println(red[numDigitsRed]);
numDigitsRed++;
red[numDigitsRed] ='\0';
}
} else if (dashIndex == 1) { //gives the green value
if (colorData[i] != 'x') {
green[numDigitsGreen] = colorData[i]; //was numDigitsGreen - 1
numDigitsGreen++;
green[numDigitsGreen] ='\0';
}
} else if (dashIndex == 2) { //gives the blue value
if (colorData[i] != 'x') {
blue[numDigitsBlue] = colorData[i]; //digits -1
numDigitsBlue++;
blue[numDigitsBlue] = '\0';
}
}
}
Serial.println(red[0]);
Serial.println("numDigitsRed");
Serial.println(numDigitsRed);
Serial.println("numDigitsGreen");
Serial.println(numDigitsGreen);
Serial.println("numDigitsBlue");
Serial.println(numDigitsBlue);
char curRedArr[5];
char curGreenArr[5];
char curBlueArr[5];
//RED SEPARATION AND TURNING INTO AN INT
for (int redIndex = 0; redIndex < numDigitsRed; redIndex++) {
curRedArr[redIndex] = red[redIndex];
Serial.print("red loop getting called. adding");
Serial.println(redIndex);
Serial.println(curRedArr[redIndex]);
}
curRedArr[numDigitsRed]='\0';
String curRedStr = String(curRedArr);
Serial.println("curRedStr:");
Serial.println(curRedStr);
int amtRed = curRedStr.toInt();
Serial.println("Red:");
Serial.println(amtRed);
//GREEN
for (int greenIndex = 0; greenIndex < numDigitsGreen; greenIndex++) {
curGreenArr[greenIndex] = green[greenIndex];
}
curGreenArr[numDigitsGreen]='\0';
String curGreenStr = String(curGreenArr);
Serial.println("curGreenStr:");
Serial.println(curGreenStr);
int amtGreen = curGreenStr.toInt();
Serial.println("Green:");
Serial.println(amtGreen);
//BLUE
for (int blueIndex = 0; blueIndex < numDigitsBlue; blueIndex++) {
curBlueArr[blueIndex] = blue[blueIndex];
}
curBlueArr[numDigitsBlue] = '\0';
String curBlueStr = String(curBlueArr);
Serial.println("curBlueStr:");
Serial.println(curBlueStr);
int amtBlue = curBlueStr.toInt();
Serial.println("Blue:");
Serial.println(amtBlue);
strip.clear();
for (int numLed = 0; numLed < LED_COUNT; numLed++) {
strip.setPixelColor(numLed, amtRed, amtGreen, amtBlue);
}
strip.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment