I using Adafruit_NeoPixel from https://github.com/adafruit/Adafruit_NeoPixel and I have problem about serial communication with my board, but it's work fine when i don't use pixels.show(); I have no idea why, but anyone can explain this, and how to fix this.(I have to refresh led overtime too) *sorry for my English
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 6
#define NUMPIXELS 16
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int delayval = 500;
void setup() {
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
pixels.begin();
Serial.begin(115200);
}
void loop() {
for(int i=0;i<NUMPIXELS;i++){
pixels.setPixelColor(i, pixels.Color(0,150,0));
pixels.show();
}
}
void serialEvent() {
if(Serial.available()){
delay(5);
size_t len = Serial.available();
char rcvData[128];
Serial.readBytes( rcvData, len );
rcvData[len] = '\0';
Serial.write(rcvData);
Serial.write('\n');
}
}
Serial input
123456789
123456789
123456789
123456789
123456789
123456789
123456789
123456789
123456789
abcdefghijklmnopqrs
abcdefghijklmnopqrs
abcdefghijklmnopqrs
abcdefghijklmnopqrs
abcdefghijklmnopqrs
abcdefghijklmnopqrs
abcdefghijklmnopqrs
Serial Output
12679
12349
123459
12456789
12569
126789
12389
12389
12389
abdefklqrs
abfghijklmnopqrs
abghmnos
abcdijkpqs
abfglmns
abcdijopqs
abefglmnrs
Well, if u read the Adafruit_NeoPixel::show part, it's because of the crucial timing.
note: Data latch = 50+ microsecond pause in the output stream. Rather than put a delay at the end of the function, the ending time is noted and the function will simply hold off (if needed) on issuing the subsequent round of data until the latch time has elapsed. This allows the mainline code to start generating the next frame of data rather than stalling for the latch.
while(!canShow()); endTime is a private member (rather than global var) so that mutliple
instances on different pins can be quickly issued in succession (each instance doesn't delay the next). In order to make this code runtime-configurable to work with any pin, SBI/CBI instructions are eschewed in favor of full PORT writes via the OUT or ST instructions. It relies on two facts: that peripheral functions (such as PWM) take precedence on output pins, so our PORT- wide writes won't interfere, and that interrupts are globally disabled while data is being issued to the LEDs, so no other code will be accessing the PORT. The code takes an initial 'snapshot' of the PORT state, computes 'pin high' and 'pin low' values, and writes these back to the PORT register as needed.noInterrupts(); Need 100% focus on instruction timing
Hope this answeres your question