Hans Scharler
Published © MIT

L3D Cube Displaying CheerLights

Show the latest CheerLights color on the Internet-connected 3D LED cube.

BeginnerProtip1,452
L3D Cube Displaying CheerLights

Things used in this project

Hardware components

L3D Cube
×1
Photon
Particle Photon
×1

Software apps and online services

CheerLights API

Story

Read more

Code

L3D Cube Code for CheerLights

Arduino
#include "beta-cube-library.h"
#include "HttpClient.h"

#define MAX_BRIGHTNESS 55
/**
* Declaring the variables.
*/

Cube cube=Cube();
unsigned int nextTime = 0;    // Next time to contact the server
HttpClient http;

// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
    { "Accept" , "*/*"},  //the content will be plaintext
    { NULL, NULL } // NOTE: Always terminate headers will NULL
};

void fill(Color col);

http_request_t request;
http_response_t response;

void setup() {
    Serial.begin(9600);
    cube.begin();
    cube.background(black);
}

void loop() {
    if (nextTime > millis()) {
        return;
    }

    Serial.println();
    Serial.println("Application>\tStart of Loop.");
    
    //cheerlights URL
    //http://api.thingspeak.com/channels/1417/field/2/last.txt
    request.hostname = "api.thingspeak.com";
    request.port = 80;
    request.path = "/channels/1417/field/2/last.txt";

    // Get request
    http.get(request, response, headers);
    Serial.print("Application>\tResponse status: ");
    Serial.println(response.status);

    Serial.print("Application>\tHTTP Response Body: ");
    Serial.println(response.body);
    Serial.println(response.body.length());
    int red, green, blue;

    //if there's a valid hex color string from Cheerlights, update the color
    if(response.body.length()==7)
    {
    //convert the hex values from the response.body string into byte values
  red=hexToInt(response.body.charAt(1))*16+hexToInt(response.body.charAt(2));
  green=hexToInt(response.body.charAt(3))*16+hexToInt(response.body.charAt(4));
  blue=hexToInt(response.body.charAt(5))*16+hexToInt(response.body.charAt(6));
	Serial.print(red);
	Serial.print(", ");
	Serial.print(green);
	Serial.print(", ");
	Serial.println(blue);
	//scale the values to max_brightness, so as not to draw too much current from the cube
	red=map(red,0,255,0,MAX_BRIGHTNESS);
	green=map(green,0,255,0,MAX_BRIGHTNESS);
	blue=map(blue,0,255,0,MAX_BRIGHTNESS);
	Color col=Color(red, green, blue);
	//actually update the color on the cube, with a cute animation
	fill(col);

//cube.background is an immediate color change, where fill(col) does a little animation
//	cube.background(col);
//	cube.show();
    }   

    nextTime = millis() + 3000;
}

int hexToInt(char val)
{
	int v = (val > '9')? (val &~ 0x20) - 'A' + 10: (val - '0');
	return v;
}

//fills the LED cube with new new color col  and a little animatiojn
void fill(Color col)
{
	for(int z=0;z<cube.size;z++)
	for(int y=0;y<cube.size;y++)
	for(int x=0;x<cube.size;x++)
	{
	int index=z*cube.size*cube.size+y*cube.size+x;
	cube.setVoxel(x,y,z,col);
	if((index%2==0)||(index==(cube.size*cube.size*cube.size)-1))
		cube.show();
	}
}

Credits

Hans Scharler
15 projects • 89 followers
IoT Engineer, Maker - I have a toaster that has been tweeting since 2008.

Comments