0

Controlling NeoPixel with RGB Slider
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Controlling NeoPixel with RGB Slider

by meloffbird on Wed Mar 30, 2016 1:29 am

New to Arduino and looking for some tutorials on how to use the sliders to adjust the colors of a NeoPixel Ring.
This is the Dashboard I have created so far (https://io.adafruit.com/meloffbird/neopixel) and my code is below:

Code: Select all | TOGGLE FULL SIZE
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif
 
/****************************** Pins ******************************************/
 
#define PIN            2

/****************************** Neopixel ******************************************/

#define NUMPIXELS      24

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(24, PIN, NEO_RGBW + NEO_KHZ800);

int delayval = 500; // delay for half a second

 
/************************* WiFi Access Point *********************************/

#define WLAN_SSID       ""
#define WLAN_PASS       ""

/************************* Adafruit.io Setup *********************************/

 
/************ Global State (you don't need to change this!) ******************/
 
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
 
// Store the MQTT server, client ID, username, and password in flash memory.
// This is required for using the Adafruit MQTT library.
const char MQTT_SERVER[] PROGMEM    = AIO_SERVER;
// Set a unique MQTT client ID using the AIO key + the date and time the sketch
// was compiled (so this should be unique across multiple devices for a user,
// alternatively you can manually set this to a GUID or other random value).
const char MQTT_CLIENTID[] PROGMEM  = __TIME__ AIO_USERNAME;
const char MQTT_USERNAME[] PROGMEM  = AIO_USERNAME;
const char MQTT_PASSWORD[] PROGMEM  = AIO_KEY;
 
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD);
 
/****************************** Feeds ***************************************/
 
// Setup a feed called 'lamp' for subscribing to changes.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
const char NeoRED_FEED[] PROGMEM = AIO_USERNAME "/feeds/neored";
Adafruit_MQTT_Subscribe NeoRED = Adafruit_MQTT_Subscribe(&mqtt, NeoRED_FEED);

const char NeoGRN_FEED[] PROGMEM = AIO_USERNAME "/feeds/neogrn";
Adafruit_MQTT_Subscribe NeoGRN = Adafruit_MQTT_Subscribe(&mqtt, NeoGRN_FEED);

const char NeoBLU_FEED[] PROGMEM = AIO_USERNAME "/feeds/neoblu";
Adafruit_MQTT_Subscribe NeoBLU = Adafruit_MQTT_Subscribe(&mqtt, NeoBLU_FEED);
 
/*************************** Sketch Code ************************************/
 
int current = 0;
int ColorRead = 0;
void connect();
 
void setup() {

  pixels.begin(); // This initializes the NeoPixel library.
 
  Serial.begin(115200);
 
  Serial.println(F("Adafruit IO Example"));
 
  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  delay(10);
  Serial.print(F("Connecting to "));
  Serial.println(WLAN_SSID);
 
  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(F("."));
  }
  Serial.println();
 
  Serial.println(F("WiFi connected"));
  Serial.println(F("IP address: "));
  Serial.println(WiFi.localIP());
 
  // listen for events on the RED feed
  mqtt.subscribe(&NeoRED);
 
  // connect to adafruit io
  connect();
}
 
void loop() {
 
  Adafruit_MQTT_Subscribe *subscription;
 
  // ping adafruit io a few times to make sure we remain connected
  if(! mqtt.ping(3)) {
    // reconnect to adafruit io
    if(! mqtt.connected())
      // listen for events on the light feed
      mqtt.subscribe(&NeoRED);
     
      // connect to adafruit io
      connect();
  }
 
  // this is our 'wait for incoming subscription packets' busy subloop
  while (subscription = mqtt.readSubscription(1000)) {
 
    // we only care about the lamp events
    if (subscription == &NeoRED) {
 
      // convert mqtt ascii payload to int
      char *value = (char *)NeoRED.lastread;
      Serial.print(F("Received: "));
      Serial.println(value);
      current = atoi(value);
    }
      Serial.println(current);
     
    for(int i=0;i<NUMPIXELS;i++){
 
      // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
      pixels.setPixelColor(i, pixels.Color(0, 0, 0, 255)); // white
 
      pixels.show(); // This sends the updated pixel color to the hardware.
 
      delay(delayval); // Delay for a period of time (in milliseconds).
 
    }
  }
 
}
 
// connect to adafruit io via MQTT
void connect() {
 
  Serial.print(F("Connecting to Adafruit IO... "));
 
  int8_t ret;
 
  while ((ret = mqtt.connect()) != 0) {
 
    switch (ret) {
      case 1: Serial.println(F("Wrong protocol")); break;
      case 2: Serial.println(F("ID rejected")); break;
      case 3: Serial.println(F("Server unavail")); break;
      case 4: Serial.println(F("Bad user/pass")); break;
      case 5: Serial.println(F("Not authed")); break;
      case 6: Serial.println(F("Failed to subscribe")); break;
      default: Serial.println(F("Connection failed")); break;
    }
 
    if(ret >= 0)
      mqtt.disconnect();
 
    Serial.println(F("Retrying connection..."));
    delay(5000);
 
  }
 
  Serial.println(F("Adafruit IO Connected!"));
 
}
Last edited by meloffbird on Wed Mar 30, 2016 3:24 pm, edited 2 times in total.

meloffbird
 
Posts: 7
Joined: Thu Mar 03, 2016 10:09 pm

Re: Controlling NeoPixel with RGB Slider

by icosa on Wed Mar 30, 2016 3:10 pm

Hi Mel,

Cool idea. One thing though, did you just post your AdaFruit API key for everyone to see?

IC.

icosa
 
Posts: 11
Joined: Sun Dec 13, 2015 7:09 pm

Re: Controlling NeoPixel with RGB Slider

by meloffbird on Wed Mar 30, 2016 3:23 pm

Oops, thanks icosa. Wasn't thinking

meloffbird
 
Posts: 7
Joined: Thu Mar 03, 2016 10:09 pm

Re: Controlling NeoPixel with RGB Slider

by jwcooper on Mon Apr 04, 2016 12:30 pm

Another option would be to use the color picker widget to select your color (or change it from your sketch to update both the dashboard and your neopixels).

When you click the color circle on the picker widget, and choose a new color, it will send the hex values of the color to your device if you subscribe to that feed.

jwcooper
 
Posts: 666
Joined: Tue May 01, 2012 9:08 pm

Re: Controlling NeoPixel with RGB Slider

by AspectEleven on Wed Mar 14, 2018 8:46 pm

jwcooper wrote:Another option would be to use the color picker widget to select your color (or change it from your sketch to update both the dashboard and your neopixels).

When you click the color circle on the picker widget, and choose a new color, it will send the hex values of the color to your device if you subscribe to that feed.



Can you wonderful people at Adafruit go back and update the "Adafruit IO Basics: NeoPixel Controller" tutorial (https://learn.adafruit.com/adafruit-io- ... r/the-code) so that we know how the code should be written, and why? I'm trying to make a lamp for a buddy of mine, but I can't find such tutorials anywhere!

AspectEleven
 
Posts: 24
Joined: Mon Sep 15, 2014 10:51 am

Re: Controlling NeoPixel with RGB Slider

by Nazanin8915 on Wed Apr 17, 2019 9:55 am

Hi I have an important question please help if you know the answer
Iam trying to connect adafruit to webhook and change the colors by webhook Iam trying the following url to change the colors through webhook
https://maker.ifttt.com/trigger/MY EVENT NAME/with/key/hVu5hrqG45hqh8t_BrHd6dvJJIZvAkxI52-33dolnBn/?value1=#381212

but it doesnt work please if some one know the answer contact me through my email
Last edited by brubell on Thu Apr 18, 2019 10:22 am, edited 1 time in total.
Reason: removed email

Nazanin8915
 
Posts: 1
Joined: Wed Apr 17, 2019 8:20 am

Please be positive and constructive with your questions and comments.