Skip to content

Instantly share code, notes, and snippets.

@ticapix
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save ticapix/11106302 to your computer and use it in GitHub Desktop.

Select an option

Save ticapix/11106302 to your computer and use it in GitHub Desktop.
Arduino code for the Blync (http://gronlier.fr/blog/2014/03/blync-2/)
#define RPIN 6
#define GPIN 5
#define BPIN 3
#define CMD_SIZE 4
#define CMD_AUTO 0
#define CMD_GET 1
#define CMD_SET 2
#define CMD_ACK 1
const char steps[6][3] = {
{0, 1, 0}, // start from red (255, 0, 0) and go to yellow (255, 255, 0)
{-1, 0, 0}, // go to green (0, 255, 0)
{0, 0, 1}, // go to cyan (0, 255, 255)
{0, -1, 0}, // go to blue (0, 0, 255)
{1, 0, 0}, // go to magenta (255, 0, 255)
{0, 0, -1}, // go back to red (255, 0, 0)
};
struct {
unsigned char color[3];
unsigned char step;
unsigned char counter;
enum {
AUTO = CMD_AUTO,
GET = CMD_GET,
SET = CMD_SET,
NONE
} mode;
byte cmd_buffer[CMD_SIZE];
unsigned char cmd_filled;
} memory;
void setup() {
pinMode(RPIN, OUTPUT);
pinMode(GPIN, OUTPUT);
pinMode(BPIN, OUTPUT);
Serial.begin(9600);
init_memory();
}
void init_memory() {
memset(&memory, 0, sizeof(memory));
memory.color[0] = 255; // start by red
}
void apply_color() {
analogWrite(RPIN, memory.color[0]);
analogWrite(GPIN, memory.color[1]);
analogWrite(BPIN, memory.color[2]);
}
void default_mode() {
memory.color[0] += steps[memory.step][0];
memory.color[1] += steps[memory.step][1];
memory.color[2] += steps[memory.step][2];
if (++memory.counter == 255) {
memory.step = ++memory.step % 6;
memory.counter = 0;
}
apply_color();
}
void loop() {
if (memory.mode == memory.AUTO) {
default_mode();
} else {
if (memory.mode == memory.GET) {
Serial.write(memory.color[0]);
Serial.write(memory.color[1]);
Serial.write(memory.color[2]);
} else if (memory.mode == memory.SET) {
apply_color();
Serial.write(CMD_ACK);
}
memory.mode = memory.NONE;
}
delay(30);
// serialEvent() is not compatible with the Esplora, Leonardo, or Micro
// so let's call it manualy
serialEvent();
}
void serialEvent() {
while (Serial.available() && memory.cmd_filled < CMD_SIZE) {
memory.cmd_buffer[memory.cmd_filled++] = Serial.read();
}
if (memory.cmd_filled > 0) {
if (memory.cmd_buffer[0] == memory.AUTO) {
memory.mode = memory.AUTO;
init_memory();
memory.cmd_filled = 0;
} else if (memory.cmd_buffer[0] == memory.GET) {
memory.mode = memory.GET;
memory.cmd_filled = 0;
} else if (memory.cmd_buffer[0] == memory.SET) {
if (memory.cmd_filled == CMD_SIZE) {
memory.mode = memory.SET;
memory.color[0] = memory.cmd_buffer[1];
memory.color[1] = memory.cmd_buffer[2];
memory.color[2] = memory.cmd_buffer[3];
memory.cmd_filled = 0;
}
} else {
memory.cmd_filled = 0;
}
}
}
#include <Adafruit_NeoPixel.h>
#define PIN 5
#define NUMPIXELS 5
#define DELAY 30 // delay for half a second
enum COLOR {
R = 0, G = 1, B = 2 }
COLOR;
static const char RGBSTEPS[6][3] = {
{
0, 1, 0 }
, // start from red (255, 0, 0) and go to yellow (255, 255, 0)
{
-1, 0, 0 }
, // go to green (0, 255, 0)
{
0, 0, 1 }
, // go to cyan (0, 255, 255)
{
0, -1, 0 }
, // go to blue (0, 0, 255)
{
1, 0, 0 }
, // go to magenta (255, 0, 255)
{
0, 0, -1 }
, // go back to red (255, 0, 0)
};
class RGBLed {
public:
RGBLed() {
_color[R] = 255;
_color[G] = 0;
_color[B] = 0;
_counter = 0;
_step = 0;
}
inline unsigned char r() {
return _color[R];
}
inline unsigned char g() {
return _color[G];
}
inline unsigned char b() {
return _color[B];
}
void inc(unsigned char n = 1) {
for (unsigned char i = 0; i < n; ++i) {
_color[0] += RGBSTEPS[_step][0];
_color[1] += RGBSTEPS[_step][1];
_color[2] += RGBSTEPS[_step][2];
if (++_counter == 255) {
_step = ++_step % 6;
_counter = 0;
}
}
}
private:
unsigned char _color[3];
unsigned char _step;
unsigned char _counter;
};
RGBLed leds[NUMPIXELS];
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
#define setPixelColor(i, r, g, b) pixels.setPixelColor(i, pixels.Color(g, r, b));
short fading[NUMPIXELS];
void setup() {
pixels.begin(); // This initializes the NeoPixel library.
memset(&fading, 0, sizeof(fading));
for (unsigned char i = 0; i < NUMPIXELS; i++) {
leds[i].inc(i * 255);
}
}
inline unsigned char fade(unsigned char color, unsigned char fading) {
return (color * (1 + sin(2 * 3.14 * fading / 255))) / 2;
}
void loop() {
for (unsigned char i = 0; i < NUMPIXELS; i++) {
fading[i] += 2*(i+1); // overflow is wanted
leds[i].inc();
setPixelColor(i, fade(leds[i].r(), fading[i]), fade(leds[i].g(), fading[i]), fade(leds[i].b(), fading[i]));
}
pixels.show(); // This sends the updated pixel color to the hardware.
delay(DELAY);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment