So here goes, keep in mind that i am a total beginner on Arduino, so if you see something that could be coded in a better way just let me know!
The Arduino is connected by USB, it’s just easier (power supply, serial)
On the Arduino i’ve loaded this sketch:
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 6
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
int ss=0;
int rr=0;
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 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)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(24, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(9600);//Hiermee open je COM poorten op bepaalde snelheid
inputString.reserve(200);// reserve 200 bytes for the inputString:
strip.begin();
strip.show(); // Initialize all pixels to 'off'
blip(0,255,0,4); // just blip 4 times when ready
Serial.println("Ready!");
}
void loop() {
// Analyse the string when a newline arrives:
if (stringComplete && inputString == "1\n") { //Blip 4 times Blue
ss=0;
Serial.println("I found 1!!");
blip(0,0,255,4); // just blip once
// clear the string:
inputString = "";
stringComplete = false;
}
else if (stringComplete && inputString == "2\n") { //theaterChase in White
Serial.println("I found 2!!");
theaterChase(strip.Color(127, 127, 127), 50); // White
// clear the string:
inputString = "";
stringComplete = false;
}
else if (stringComplete && inputString == "3\n") { //CylonBounce 3 times in Red
Serial.println("I found 3!!");
CylonBounce(0xff, 0, 0, 4, 30, 50,3);
// clear the string:
inputString = "";
stringComplete = false;
}
else if (stringComplete && inputString == "4\n") { //No prgram connected!
Serial.println("I found 4!!");
Serial.println("No prgram connected!!");
if (rr==1){
rr=0;
reset(0,0,0);
}
else if (rr==0){
rr=1;
}
Serial.println(rr);
// clear the string:
inputString = "";
stringComplete = false;
}
else if (stringComplete && inputString == "5\n") { //reset/Moodlight, color fixed....
Serial.println("I found 5!!");
reset(255,63,20);
// clear the string:
inputString = "";
stringComplete = false;
}
else if (stringComplete && inputString == "6\n") { //FIRE!!
Serial.println("I found 4!!");
Serial.println("No prgram connected!!");
if (ss==1){
ss=0;
reset(0,0,0);
}
else if (ss==0){
ss=1;
}
Serial.println(ss);
// clear the string:
inputString = "";
stringComplete = false;
}
else if (stringComplete && inputString.startsWith("A")) { //blip RED, repetitions in the Command (eg A4 blips 4 times)
Serial.println("I found A!!");
int reps=0;
reps = inputString.substring(1).toInt();
Serial.println(reps);
blip(255,0,0,reps); // just blip once
// clear the string:
inputString = "";
stringComplete = false;
}
else if (stringComplete && inputString.startsWith("1") ) { //reset/Moodlight,Pimatic Special, the output for DummyLed needs some work, done here
String color = "";
color = inputString.substring(3);//Lose the '#' at the start of the colorcode....
Serial.println("The color is " + color); //just checking
char charbuf[8]; //We need a RGB format, so convert the HEX code to RGB
color.toCharArray(charbuf,8);
long int rgb=strtol(charbuf,0,16); //=>rgb=0x001234FE;
byte r=(byte)(rgb>>16);
byte g=(byte)(rgb>>8);
byte b=(byte)(rgb);
Serial.println(r);
Serial.println(g);
Serial.println(b);
reset(r,g,b);//Lastly, call reset to set the light
// clear the string:
inputString = "";
stringComplete = false;
}
else if (stringComplete) { //Just a catch if unknown command is given, output to serial only.
Serial.println("Found nothing for: ");
Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
}
if (ss==1){
// Regular (orange) flame:
int r = 226, g = 121, b = 35;
// Purple flame:
// int r = 158, g = 8, b = 148;
// Green flame:
//int r = 74, g = 150, b = 12;
// Flicker, based on our initial RGB values
for(int i=0; i<strip.numPixels(); i++) {
int flicker = random(0,55);
int r1 = r-flicker;
int g1 = g-flicker;
int b1 = b-flicker;
if(g1<0) g1=0;
if(r1<0) r1=0;
if(b1<0) b1=0;
strip.setPixelColor(i,r1,g1, b1);
}
strip.show();
delay(random(10,113));
}
if (rr==1){
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i*1+j) & 255));
}
strip.show();
delay(40);
}
}
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
void reset(byte red, byte green, byte blue) {
for(int i = 0; i < strip.numPixels(); i++ ) {
strip.setPixelColor(i, red, green, blue);
}
strip.show();
}
void blip(byte red, byte green, byte blue,int reps) {
for (int x=0; x<reps; x++){
for(int i = 0; i < strip.numPixels(); i++ ) {
strip.setPixelColor(i, red, green, blue);
}
strip.show();
delay(15);
for(int i = 0; i < strip.numPixels(); i++ ) {
strip.setPixelColor(i, 0, 0, 0);
}
strip.show();
delay(100);
}
}
void colorWipe(uint32_t c, uint8_t wait) { // Fill the dots one after the other with a color
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
reset(0,0,0);
}
void theaterChase(uint32_t c, uint8_t wait) { //Theatre-style crawling lights.
for (int j=0; j<10; j++) { //do 10 cycles of chasing
for (int q=0; q < 3; q++) {
for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, c); //turn every third pixel on
}
strip.show();
delay(wait);
for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
reset(0,0,0);
}
void CylonBounce(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay,int reps){
for(int x = 0; x < reps; x++){
for(int i = 0; i < strip.numPixels()-EyeSize-2; i++) {
reset(0,0,0);
strip.setPixelColor(i, red/10, green/10, blue/10);
for(int j = 1; j <= EyeSize; j++) {
strip.setPixelColor(i+j, red, green, blue);
}
strip.setPixelColor(i+EyeSize+1, red/10, green/10, blue/10);
strip.show();
delay(SpeedDelay);
}
delay(ReturnDelay);
for(int i = strip.numPixels()-EyeSize-2; i > 0; i--) {
reset(0,0,0);
strip.setPixelColor(i, red/10, green/10, blue/10);
for(int j = 1; j <= EyeSize; j++) {
strip.setPixelColor(i+j, red, green, blue);
}
strip.setPixelColor(i+EyeSize+1, red/10, green/10, blue/10);
strip.show();
delay(SpeedDelay);
}
delay(ReturnDelay);
}
reset(0,0,0);
}
uint32_t Wheel(byte WheelPos) { //Needed for the Rainbow animation
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
On the Pi i have the following Python script:
#!/usr/bin/env python
import time
import serial
import sys
#print "argumentlist: ",str(sys.argv)
arglist=sys.argv
#if len(sys.argv)<>2:
# sys.exit("Too many arguments")
arglist= arglist[0:]
print arglist
command=(arglist[1])
#print command
command=command+"\n"
#print command
ser = serial.Serial('/dev/ttyUSB0',9600)
time.sleep(1.6)
ser.write(command)
ser.close()
Now i just create a rule on Pimatic which would do execute "python /home/pi/Desktop/neo.py 1"
.
Also for moodlight (solid color) it’s possible to do execute "python /home/pi/Desktop/neo.py '1 $LedColor.color'"
(using the Ledcolor from a dummyled device).
Hope someone could use this, been a nice experience getting the Arduino to work!!