Go Down

Topic: FastLED and Serial.read() interrupt mystery (Read 272 times) previous topic - next topic

NickyF0str

Greeting!

I'm trying to programm my Arduino Nano in such was, that it will control relay board (16 relays), ws2812b led (570 leds) and read data from Serial.

If I enable serial communication in my code - FastLED just stops working.

I have tried many different approaches to use non-blocking serial.read, but no success so far.

One very interesting thing - if I reduce the amout of leds in my program to 532 (instead of 570) - everything will work perfectly.

Here is my code:

Code: [Select]

//#define FASTLED_ALLOW_INTERRUPTS 0      // doesnt work
//#define FASTLED_INTERRUPT_RETRY_COUNT 0 // doesnt work either
#include <FastLED.h>

#define NUM_LEDS 570
#define DATA_PIN 2
#define BRIGHTNESS 255

CRGB leds[NUM_LEDS];

const int buttonPin           = A6; // button pin
const int AFpin               = 9;  // 16th relay pin, AF pin
const uint8_t controlPin[15]  = {19,3,4,12,8,10,17,5,18,6,11,7,15,16,14}; // camera pins
const int amount_pins         = 15;

bool inloop = false;

void setup() {
  Serial.begin(9600);
  Serial.print("Ready");
 
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);

  pinMode(AFpin, OUTPUT);
  digitalWrite(AFpin, HIGH);
  for (int i = 0; i < amount_pins; i++) {
    pinMode(controlPin[i], OUTPUT);
    digitalWrite(controlPin[i], HIGH);
  }
  turn_all_wait();
}


void loop() {
  if(Serial.available() {
    int t = Serial.read();
  }

  bool flag = true;
      if (analogRead(buttonPin) > 512) {
        digitalWrite(AFpin, LOW);
        af(4);
        before_shoot();
        while(flag) {
          if (analogRead(buttonPin) > 512) {
            flag = false;
            Serial.print(t);
            if (t == 1) {
              bomerang(150);
            }
            if (t == 2) {
              bullet();
            }
          }
        }
      }
}


void super_main() {
  bool flag2 = true;
  while(flag2) {
   
  }
}

void af(int count){
  // 1 count == 1.6 sec
  for(int c = 0; c < count; c++) {
    for(int k = 0; k < 14; k++) {
      blink_one(k, 0,0,250);
      blink_one(14-k, 0,0,250);
      FastLED.show();
      delay(5);
     
      blink_one(k, 250,0,0);
      blink_one(14-k,250,0,0); 
      FastLED.show();
      delay(5);
    }
  }
}

void before_shoot(){
  for(int j = 0; j < 255; j+=10) {
    for(int i = 0; i < NUM_LEDS; i++) {
      leds[i] = CRGB(0,0,250);
    }
    FastLED.setBrightness(j);
    FastLED.show();
  }
}

void blink_one_rnd(int pos) {
  pos = pos * 38;
  for(int i = 0; i < 38; i++) {
    leds[i+pos] = CRGB::Red;
  }
  FastLED.show();
  for(int i = 0; i < 38; i++) {
    leds[i+pos] = CRGB::PowderBlue;
  }
  FastLED.show();
}

void blink_one(int pos, int r, int g, int b) {
  pos = pos * 38;
  for(int i = 0; i < 38; i++) {
    leds[i+pos] = CRGB(r, g, b);
  }
}

void blink_all_on() {
  for(int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CRGB::AliceBlue;
  }
}

void turn_all_wait() {
  for(int j = 50; j < 255; j+=5) {
    for(int i = 0; i < NUM_LEDS; i++) {
      leds[i] = CRGB::DarkGreen;
    }
    FastLED.setBrightness(j);
    FastLED.show();
  }
}

void blink_all_off() {
  for(int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CRGB::Black;
  }
}

void loading(int count){
  // 1 count == 1.4 sec
  for(int l = 0; l < count; l++) {
    for(int i=0;i<38;i++){
        for(int k = 0;k<NUM_LEDS;k+=38){
          leds[i+k] = CRGB(0,255,0);
          leds[(38-i)+k] = CRGB(0,255,0);
        }
        FastLED.show();
      }
      for(int i=38;i>0;i--){
        for(int k = 0;k<NUM_LEDS;k+=38){
          leds[i+k] = CRGB(0,0,255);
          leds[(38-i)+k] = CRGB(0,0,255);
        }
        FastLED.show();
      }
 }
}

void bomerang(int x){
  blink_all_on();
  FastLED.show();
  shoot_boomerang(x);
  blink_all_off();
  FastLED.show();
  digitalWrite(AFpin, HIGH);
  blink_all_on();
  loading(4);
  turn_all_wait();
  delay(1);
}

void bullet() {
  blink_all_on();
  FastLED.show();
  shoot_delay();
  blink_all_off();
  FastLED.show();
  digitalWrite(AFpin, HIGH);
  blink_all_on();
  loading(4);
  turn_all_wait();
  delay(1);
}

void shoot_delay() {
  digitalWrite(controlPin[0], LOW);
  digitalWrite(controlPin[1], LOW);
  digitalWrite(controlPin[2], LOW);
  digitalWrite(controlPin[3], LOW);
  digitalWrite(controlPin[4], LOW);
  digitalWrite(controlPin[5], LOW);
  digitalWrite(controlPin[6], LOW);
  digitalWrite(controlPin[7], LOW);
  digitalWrite(controlPin[8], LOW);
  digitalWrite(controlPin[9], LOW);
  digitalWrite(controlPin[10], LOW);
  digitalWrite(controlPin[11], LOW);
  digitalWrite(controlPin[12], LOW);
  digitalWrite(controlPin[13], LOW);
  digitalWrite(controlPin[14], LOW);
  delay(80);
  digitalWrite(controlPin[0], HIGH);
  digitalWrite(controlPin[1], HIGH);
  digitalWrite(controlPin[2], HIGH);
  digitalWrite(controlPin[3], HIGH);
  digitalWrite(controlPin[4], HIGH);
  digitalWrite(controlPin[5], HIGH);
  digitalWrite(controlPin[6], HIGH);
  digitalWrite(controlPin[7], HIGH);
  digitalWrite(controlPin[8], HIGH);
  digitalWrite(controlPin[9], HIGH);
  digitalWrite(controlPin[10], HIGH);
  digitalWrite(controlPin[11], HIGH);
  digitalWrite(controlPin[12], HIGH);
  digitalWrite(controlPin[13], HIGH);
  digitalWrite(controlPin[14], HIGH);
}

void shoot_boomerang(int x) {
  digitalWrite(controlPin[0], LOW);
  delay(x);
  digitalWrite(controlPin[1], LOW);
  delay(x);
  digitalWrite(controlPin[2], LOW);
  delay(x);
  digitalWrite(controlPin[3], LOW);
  delay(x);
  digitalWrite(controlPin[4], LOW);
  delay(x);
  digitalWrite(controlPin[5], LOW);
  delay(x);
  digitalWrite(controlPin[6], LOW);
  delay(x);
  digitalWrite(controlPin[7], LOW);
  delay(x);
  digitalWrite(controlPin[8], LOW);
  delay(x);
  digitalWrite(controlPin[9], LOW);
  delay(x);
  digitalWrite(controlPin[10], LOW);
  delay(x);
  digitalWrite(controlPin[11], LOW);
  delay(x);
  digitalWrite(controlPin[12], LOW);
  delay(x);
  digitalWrite(controlPin[13], LOW);
  delay(x);
  digitalWrite(controlPin[14], LOW);

  delay(50);
  digitalWrite(controlPin[0], HIGH);
  digitalWrite(controlPin[1], HIGH);
  digitalWrite(controlPin[2], HIGH);
  digitalWrite(controlPin[3], HIGH);
  digitalWrite(controlPin[4], HIGH);
  digitalWrite(controlPin[5], HIGH);
  digitalWrite(controlPin[6], HIGH);
  digitalWrite(controlPin[7], HIGH);
  digitalWrite(controlPin[8], HIGH);
  digitalWrite(controlPin[9], HIGH);
  digitalWrite(controlPin[10], HIGH);
  digitalWrite(controlPin[11], HIGH);
  digitalWrite(controlPin[12], HIGH);
  digitalWrite(controlPin[13], HIGH);
  digitalWrite(controlPin[14], HIGH);
}


Any help appreciated. Thanks in advance!

surveyranger

What's the max amount of WS2812b's that the RAM on the Nano can support?

Go Up