Loop run just once

Status
Not open for further replies.

Klemen96

Member
Hello guys,

I am new in these waters.
I am doing for my finish project for Faculty. VU meter.
I have Teensy 3.2 + audio board. On it is connected programabile LED strip type WS2812B. For input I use LineIn or Mic.
My code works and everything is shown on LEDs and Serial monitor (for test) if I use at the end of my code FastLED.show(); then Delay(40); and FastLED.clear();. But this is not correct, everytime all LEDs turn off after code ends.
After that I've put in my code which LEDs and how needs to be turned on and other LEDs which I don't need I turn it off:
for(int led1 = NUM_LEDS; led1 >= ledsToLightLeft; led1--){
ledsLeft[led1] = CRGB(0,0,0);
}
Then before code ends I just put FastLED.show(); to show what code calculates.

So regarding on microphone output, code calculates how many LEDs need to be turned on, other from previous calculation turns off (if previous peak was higher).
But when I run the program. The Void loop runs just once (LEDs turn on that value), even if I open Serial monitor it shows the given peak from microphone but it stays on that value.
Bellow is my code and if someone have a clue what is incorrect please comment it:

Code:
#include <FastLED.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SerialFlash.h>

#define DATA_PIN 12
#define NUM_LEDS 60
#define BRIGHTNESS  50
CRGB ledsLeft[NUM_LEDS];

// GUItool: begin automatically generated code
AudioInputI2S            i2s2;           //xy=412,200
AudioOutputI2S           i2s1;           //xy=595.0000076293945,156.9999885559082
AudioAnalyzePeak         peak2;          //xy=596.551176071167,294.00562477111816
AudioAnalyzePeak         peak1;          //xy=597.5511741638184,239.0056552886963
AudioConnection          patchCord1(i2s2, 0, i2s1, 0);
AudioConnection          patchCord2(i2s2, 0, peak1, 0);
AudioConnection          patchCord3(i2s2, 1, i2s1, 1);
AudioConnection          patchCord4(i2s2, 1, peak2, 0);
AudioControlSGTL5000     sgtl5000_1;     //xy=421.00000762939453,370.9999942779541
// GUItool: end automatically generated code

int middleColorLED = 30;
int ledsToLightLeft;
elapsedMillis msecs;

void setup() {
  AudioMemory(100);
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(ledsLeft, NUM_LEDS);//.setCorrection( TypicalSMD5050 );
  FastLED.setBrightness(BRIGHTNESS);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.8);
  sgtl5000_1.inputSelect(AUDIO_INPUT_LINEIN);
  sgtl5000_1.lineInLevel(7);
}

void loop() {

  if (msecs > 1000){
    if (peak1.available()) {
      msecs = 0;
      float leftPeak = peak1.read();
      int count;   
        for (count=0; count < 60-leftPeak; count++) {
        Serial.print(" ");
        }
        while (count++ < 60) {
        Serial.print("<");
        }
        while (count++ < 60) {
        Serial.print(" ");
        }
         Serial.print(leftPeak);
        Serial.println();
   
        ledsToLightLeft = map(leftPeak, 0, 1, 0, NUM_LEDS);
      
        for(int led = 0; led < ledsToLightLeft; led++){
          if(led < middleColorLED){
            ledsLeft[led] = CRGB(map(led, 0, middleColorLED - 1, 0, 255) ,255,0);
            }
          else if(led > middleColorLED){
            ledsLeft[led] = CRGB(255, map(led, middleColorLED, NUM_LEDS, 255, 0),0);
          }    
        }

        for(int led1 = NUM_LEDS; led1 >= ledsToLightLeft; led1--){ //IF I INCLUDE THAT FOR LOOP IT STOP WORKING. IT RUN JUST ONCE.
          ledsLeft[led1] = CRGB(0,0,255);
        }
       FastLED.show();
      }
  }
}
I will be very thankful for any asnwer.
I hope I was clear enough
 
Last edited by a moderator:
ledsLeft is a CRGB array of size NUM_LEDS.
However array index starts at 0. So your last element will be NUM_LEDS -1
Adjust your for loop. Currently it’s writing over some bit of memory that causes your teensy to stop working.

That’s my theory.
 
ledsLeft is a CRGB array of size NUM_LEDS.
However array index starts at 0. So your last element will be NUM_LEDS -1
Adjust your for loop. Currently it’s writing over some bit of memory that causes your teensy to stop working.

That’s my theory.

Hello,
15min before your reply I found out that, but took me all afternoon :p
Thank you for your reply, you found out in few minutes.

Anyway, now I have new problem.
LEDs which needs to be turned ON all the time, the ones that doesn't stop emitting (green ones), actually blinks and it's not smooth for eyes.
Could be that because I have thin wires without capacitor on power cables? I use 5V 10A DC Power Suppy.
https://electronics.stackexchange.c...ld-i-need-a-capacitor-in-front-of-a-led-strip
 
Seems there is a way to limit brightness that would reduce power to see if that is the problem.

Not sure if this is the needed/available func - perhaps in setup()?
Code:
	/// Set the global brightness scaling

	/// @param scale a 0-255 value for how much to scale all leds before writing them out

	void setBrightness(uint8_t scale) { m_Scale = scale; }
 
Hello,

I allready tryed that, but effect is the same. From value 10 to 150m but effect is the same.
actually sometimes works werry good, sometimes flickers :/
 
Status
Not open for further replies.
Back
Top