UKHeliBob:
Have you read and understood Using millis() for timing. A beginners guide ?
yep got the basics of it. but still don’t know how to use it on program that i’m trying to do:
#include <FastLED.h>
#define NUM_LEDS 30
#define DATA_PIN 5
CRGB leds[NUM_LEDS];
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];
//temporary
char pointerToEffect[numChars] = {0};
int firstData = 0;
int secondData = 0;
int thirdData = 0;
int fourthData = 0;
int fifthData = 0;
int sixthData = 0;
boolean newData = false;
unsigned long previousMillis = 0;
const long interval = 1000;
//unsigned long temp = 0;
void setup() {
Serial.begin(9600);
LEDS.addLeds<WS2812,DATA_PIN,RGB>(leds,NUM_LEDS);
LEDS.setBrightness(250);
}
void loop() {
unsigned long currentMillis = millis();
//temp = currentMillis;
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
parseData();
//showNewData();
setAll(0,0,0);
newData = false;
}
switch(pointerToEffect[0]) {
case 'a':
//Twinkle - Color (red, green, blue), count, speed delay, only one twinkle (true/false)
twinkle(secondData, thirdData, fourthData, fifthData, sixthData);
showNewData();
break;
case 'b':
//first data is ratioPercent, second is delaySpeed
//65, 85
cylon(secondData);
break;
}
}
/////////////////////////////////////////////////////////////////////////////////////
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
/////////////////////////////////////////////////////////////////////////////
void parseData() { // split the data into its parts
char * strtokIndx;
strtokIndx = strtok(tempChars,",");
strcpy(pointerToEffect, strtokIndx);
strtokIndx = strtok(NULL, ",");
firstData = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
secondData = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
thirdData = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
fourthData = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
fifthData = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
sixthData = atoi(strtokIndx);
}
//////////////////////////////////////////////
//tester
//////////////////////////////////////////////
void showNewData() {
Serial.print("Pointer to effect ");
Serial.println(pointerToEffect);
Serial.print("First data ");
Serial.println(firstData);
Serial.print("Second data ");
Serial.println(secondData);
Serial.print("Third data ");
Serial.println(thirdData);
Serial.print("Fourth data ");
Serial.println(fourthData);
Serial.print("Fifth data ");
Serial.println(fifthData);
Serial.print("Sixth data ");
Serial.println(sixthData);
}
///////////////////////////////
//All LED effect functions here
///////////////////////////////
void fadeall(int ratioPercent) { for(int i = 0; i < NUM_LEDS; i++) { leds[i].fadeToBlackBy(ratioPercent); } }
void cylon(int delaySpeed) {
static uint8_t hue = 0;
for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(hue++, 255, 255);
FastLED.show();
fadeall(firstData);
delay(delaySpeed);
}
for(int i = (NUM_LEDS)-1; i >= 0; i--) {
leds[i] = CHSV(hue++, 255, 255);
FastLED.show();
fadeall(firstData);
delay(delaySpeed);
}
}
void twinkle(byte red, byte blue, byte green, int count, int speedDelay) {
// byte red, byte blue, byte green, int count, int speedDelay, boolean onlyOne
// red = second data, blue = third data, green = fourth data, count = fifth, speedDelay = sixth data onlyOne = dependent on first data
//byte red = 0xff;
//byte blue = 0x00;
//byte green = 0x00;
//int count = 5;
//int speedDelay = 500;
boolean onlyOne = true;
if (firstData == 1){
onlyOne = true;
}
else if (firstData == 0){
onlyOne = false;
}
setAll(0,0,0);
for (int i=0; i<count; i++) {
setPixel(random(NUM_LEDS),red,green,blue);
FastLED.show();
delay(speedDelay);
if(onlyOne) {
setAll(0,0,0);
}
}
delay(speedDelay);
}
/*void millisAsDelay(){
while (millis() < temp + interval) {
previousMillis = temp;
}
} */
////////////////
//Set all pixel
///////////////
void setPixel(int Pixel, byte red, byte green, byte blue) {
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
}
// Set all LEDs to a given color and apply it (visible)
void setAll(byte red, byte green, byte blue) {
for(int i = 0; i < NUM_LEDS; i++ ) {
setPixel(i, red, green, blue);
}
FastLED.show();
}
im trying to make it so that when a new data is received it applies that effect immediately without waiting for the previous effect to finish. i’ve tried to do it using only a single effect but does it so fast that it ruins the selected effect. here’s the code:
#include <FastLED.h>
#define NUM_LEDS 30
#define DATA_PIN 5
CRGB leds[NUM_LEDS];
int period = 1000;
unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
unsigned long currentMillis;
void setup() {
LEDS.addLeds<WS2812,DATA_PIN,RGB>(leds,NUM_LEDS);
LEDS.setBrightness(255);
}
void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].fadeToBlackBy(25); } }
void loop() {
static uint8_t hue = 0;
currentMillis = millis();
if(currentMillis - previousMillis >= period){
for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(125, 255, 255);
FastLED.show();
fadeall();
}
previousMillis = currentMillis;
}
if(currentMillis - previousMillis2 >= period){
for(int i = (NUM_LEDS)-1; i >= 0; i--) {
leds[i] = CHSV(hue++, 255, 255);
FastLED.show();
fadeall();
}
previousMillis2 = currentMillis;
}
}