Hi,
ich habe folgenden Arduino Sketch von tweak4all
/* t4a_boblight
* (C) 2014 Hans Luijten, www.tweaking4all.com
*
* t4a_boblight is free software and can be distributed and/or modified
* freely as long as the copyright notice remains in place.
* Nobody is allowed to charge you for this code.
* Use of this code is entirely at your own risk.
*/
#include "FastLED.h"
// DEFINITIONS
#define STARTCOLOR 0xFFFFFF // LED colors at start
#define BLACK 0x000000 // LED color BLACK
#define DATAPIN 8 // Datapin
#define LEDCOUNT 50 // Number of LEDs used for boblight
#define SHOWDELAY 200 // Delay in micro seconds before showing
#define BAUDRATE 500000 // Serial port speed, 460800 tested with Arduino Uno R3
#define BRIGHTNESS 90 // Max. brightness in %
const char prefix[] = {0x41, 0x64, 0x61, 0x00, 0x18, 0x4D}; // Start prefix
char buffer[sizeof(prefix)]; // Temp buffer for receiving prefix data
// Init LED strand, WS2811/WS2912 specific
// These might work for other configurations:
// 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)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDCOUNT, DATAPIN, NEO_GRB + NEO_KHZ800);
int state; // Define current state
#define STATE_WAITING 1 // - Waiting for prefix
#define STATE_DO_PREFIX 2 // - Processing prefix
#define STATE_DO_DATA 3 // - Receiving LED colors
int readSerial; // Read Serial data (1)
int currentLED; // Needed for assigning the color to the right LED
int failCounter = 0;
void setup()
{
strip.begin();
Serial.begin(BAUDRATE);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
state = STATE_WAITING;
}
void loop()
{
/* if (Serial.available() >= 20) {
digitalWrite(13, HIGH);
}
else if (Serial.available() < 20) {
digitalWrite(13, LOW);
} */
if (failCounter >= 20) {
digitalWrite(13, HIGH);
}
else if (failCounter < 20) {
digitalWrite(13, LOW);
}
switch(state)
{
case STATE_WAITING:
if ( Serial.available() > 0 ) {
readSerial = Serial.read();
if ( readSerial == prefix[0] ) {
state = STATE_DO_PREFIX;
}
else {
failCounter++;
}
}
break;
case STATE_DO_PREFIX:
if( Serial.available() >= sizeof(prefix) - 1 ) {
Serial.readBytes(buffer, sizeof(prefix) - 1);
for( int Counter = 0; Counter < sizeof(prefix) - 1; Counter++) {
if ( buffer[Counter] == prefix[Counter+1] ) {
state = STATE_DO_DATA;
currentLED = 0;
}
else {
state = STATE_WAITING;
break;
}
}
}
break;
case STATE_DO_DATA:
if( Serial.available() > 2 ) {
Serial.readBytes( buffer, 3 );
strip.setPixelColor( currentLED++, buffer[0], buffer[1], buffer[2]);
}
if( currentLED > LEDCOUNT ) {
strip.show();
//delayMicroseconds(SHOWDELAY);
state = STATE_WAITING;
currentLED = 0;
}
break;
}
}
Display More
mit Hilfe der onboard LED des Arduino Uno habe ich ein paar "Debugausgaben" eingebaut. Unter anderem einen Counter der hochzählt wenn im STATE_WAITING nicht das richtige Byte gefunden wurde.
Ich hätte nicht damit gerechnet, aber zu meiner Überraschung blinkt die LED langsam. Erstmal war ich irritiert, aber anscheinend fängt ein Integer wieder bei 0 an, wenn er überläuft.
Jetzt frage ich mich natürlich wo meine Bytes hin verschwinden.
Meinem Verständnis nach müssten ja ununterbrochen die 6 Prefix Bytes und 150 LED Bytes gesendet werden. Und laut Debugausgaben tut Boblightd auch genau das.
Wo ist mein Denkfehler? Kann ich mich irgendwie in die Übertragung einklinken und mithören?
Achja, im Moment läuft der Arduino ohne angeschlossene LEDs. Da die aber sowieso keinen Rückkanal haben sollte sich das ja auch nicht auswirken, oder?
Und wenn ich schonmal am Fragen bin. Für was ist das Delay in Zeile 111?
Vielen Dank
Christopher