OK I dont know how but after tried everything I can, finally it works as I expected
first thing I do: remove the While(1) loop
after that, check serial every cycle and if (paletteCounter!=0) then: do palette paterns
else: do FHT setup and fastled visualizer, at the end of FHT code, reset paletteCounter=0
/*
Written by FischiMc and SupaStefe
This sketch uses a 10x10 RGB LED-Matrix as a spectrum analyzer
It uses a FTT Library to analyze an audio signal connected to the
pin A7 of an Arduino nano. Everytime a column gets higher than
10 pixels the color of each column changes.
*/
#define LOG_OUT 0 //set output of FFT library to linear not logarithmical
#define LIN_OUT 1
#define FHT_N 256
#define UPDATES_PER_SECOND 100//set to 256 point fft
#include <FHT.h>
#include <FastLED.h> //include the FastLED Library
#include <math.h> //include library for mathematic funcions
#define DATA_PIN 6 //DATA PIN WHERE YOUR LEDS ARE CONNECTED
#define NUM_LEDS 42 //amount of LEDs in your matrix
CRGB leds[NUM_LEDS];
float faktoren[7] = {1.75, 1.55, 1.45, 1.25, 1.45, 1.55,1.75}; //factors to increase the height of each column
unsigned char hs[7] = {8, 8, 8, 8,8,8,8}; //height of each column
float hue;
int previousMillis;
int paletteCounter;
int previousMillis2= 0;
int currentMillis2;
int currentMillis;
CRGBPalette16 currentPalette;
TBlendType currentBlending;
extern CRGBPalette16 myRedWhiteBluePalette;
extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;
void setBalken(unsigned char column, unsigned char height){ //calculation of the height of each column
unsigned char h = (unsigned char)map(height, 0, 255, 0, 7);
h = (unsigned char)(h * faktoren[column]);
if (h < hs[column]){
hs[column]--;
}
else if (h > hs[column]){
hs[column] = h;
}
if (height > 250){
hue+=2; //CHANGE THIS VALUE IF YOU WANT THE DIFFERENCE BETWEEN THE COLORS TO BE BIGGER
if(hue > 25) hue=0;
}
for(unsigned char y = 0; y < 8; y++){ //set colors of pixels according to column and hue
if(hs[column] > y){
leds[y+(column*6)] = CHSV((hue*10)+(column*6), 255, 180);
} else {
leds[y+(column*6)] = CRGB::Black;
}
}
}
unsigned char grenzen[8] = {0,3,5,7,9,11,13,16}; //borders of the frequency areas
void setup() {
FastLED.addLeds<WS2812B, DATA_PIN, GRB> (leds, NUM_LEDS);
Serial.begin(115200); //use the serial port
//turn off timer0 for lower jitter
ADCSRA = 0xe5; //set the adc to free running mode
ADMUX = 0b01000111; //use pin A7
DIDR0 = 0x01; //turn off the digital input for
analogReference(EXTERNAL); //set aref to external
paletteCounter=1;
currentPalette = RainbowColors_p;
currentBlending = LINEARBLEND;
}
void serialInterrupt()
{
detachInterrupt(0);
interrupts();
paletteCounter = Serial.parseInt();
Serial.print("Zoe");
attachInterrupt(0, serialInterrupt, CHANGE);
}
void loop() {
if(Serial.read() >= 0){
paletteCounter = Serial.parseInt();
delay(100);
}
if (paletteCounter!=0){
currentMillis2 = millis();
if (currentMillis2 - previousMillis2 >=10) {
ChangePalettePeriodically();
static uint8_t startIndex = 0;
startIndex = startIndex + 1; /* motion speed */
FillLEDsFromPaletteColors( startIndex);
// check if "interval" time has passed (1000 milliseconds)
FastLED.show();
previousMillis2 = millis();
}
}
else {
//reduces jitter
cli(); //UDRE interrupt slows this way down on arduino1.0
for (int i = 0 ; i < 256 ; i+=1) { //save 256 samples
while(!(ADCSRA & 0x10)); //wait for adc to be ready
ADCSRA = 0xf5; //restart adc
byte m = ADCL; //fetch adc data
byte j = ADCH;
int k = (j << 8) | m; //form into an int
k -= 0x0200; //form into a signed int
k <<= 6; //form into a 16b signed int
fht_input[i] = k; //put real data into even bins
}
fht_window(); // window the data for better frequency response
fht_reorder(); // reorder the data before doing the fft
fht_run(); // process the data in the fft
fht_mag_lin(); // take the output of the fft
sei();
fht_lin_out[0] = 0;
fht_lin_out[1] = 0;
for(unsigned char i = 0; i <= 6; i++){
unsigned char maxW = 0;
for(unsigned char x = grenzen[i]; x < grenzen[i+1];x++){
if((unsigned char)fht_lin_out[x] > maxW){
maxW = (unsigned char)fht_lin_out[x];
}
}
currentMillis=millis;
if (currentMillis-previousMillis>=1){
hue++;
previousMillis=millis;
}
setBalken(i, maxW);
Serial.print(maxW);
Serial.print(" ");
Serial.println("");
FastLED.show();
paletteCounter==0;
}
}
}
there's 1 more weird thing: If I input value in range (0-10), mode visualizer active, I have to input value greater than 10 if I want pallet pattern ( if (paletteCounter!=0) then: do palette paterns?????)