Jump to content

Arduino behaving weird

Hey Guys!

I hooked up my WS2812 Led Strip to my Arduino and programmed a Light-Mode, but i can't explain myself why the variable "a" isnt getting count up.

Code:

#include <FastLED.h>
#define LED_PIN 2
#define NUM_LEDS 25

CRGB leds[NUM_LEDS];
int segNr = 0;

void setup() 
{
  FastLED.addLeds<WS2812, LED_PIN, RBG>(leds, NUM_LEDS);
  FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
  FastLED.clear();
  FastLED.show();
  Serial.begin(9600);

  Serial.print("Setup 1: ");
  Serial.println(segNr);
  segNr = 15;
  Serial.print("Setup 2: ");
  Serial.println(segNr);
}

void loop()
{
  // put your main code here, to run repeatedly:
  //leds[0] = CRGB(255, 0, 0);
  //leds[1] = CRGB(0, 255, 0);
  //leds[2] = CRGB(0, 0, 255);
  
  nightrider();
}

void nightrider() 
{
  Serial.print("Anfang: ");
  Serial.println(segNr);
  
  if (segNr >= 25) 
  {
    Serial.print("Reset: ");
    Serial.println(segNr);
    
    segNr = 0;
  } 
  else 
  {
    Serial.print("Set 1: ");
    Serial.println(segNr);
    
    for (int i = 0; i <= NUM_LEDS; i++)
    {
      leds[i] = CRGB(255, 255, 255);
    }
    
    Serial.print("Set 2: ");
    Serial.println(segNr);
    
    leds[segNr] = CRGB(255, 0, 0);
  }

  FastLED.show();
  
  delay(500);
  
  segNr++;
  
  Serial.print("Ende: ");
  Serial.println(segNr);
}


void randomcoloreffect() 
{

  for (int i = 0; i <= 25; i++)
  {
    leds[i] = CRGB(random(0, 255), random(0, 255), random(0, 255));
  }

  FastLED.show();
  delay(1000);
}

My Serial output is really weird because im getting "0" at "Set1:" and "-1" at "Set2".But i dont know why the for loop effects the a variable.

 

Thanks for your help.

Link to comment
Share on other sites

Link to post
Share on other sites

58 minutes ago, LuisTheOne said:

the variable "a"

Is that "segNr"?

 

I can't tell immediately what exactly is going wrong here (I'm not that great of a programmer) but generally speaking global variables should be avoided.

Perhaps you can make segNr a local variable instead of a global one. It seems you only use this variable inside the nightrider() function so just declare the variable in there.

like this:

void nightrider() 
{
  int segNr = <whatever value is required>;
  
  Serial.print("Anfang: ");
  Serial.println(segNr);
  
  <rest of the function>

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

It's probably because of the bounds on the for loop. You have it got to "i<=25" but the array is only 25 elements, so accessing leds[25] is actually accessing the 26th element of the array, which doesn't exist. I would guess segNr is stored in the memory after leds[], so it's getting affected. You can try printing segNr in the loop body to verify.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×