Int changes value to unexpected numbers

Well hello there,

I'm currently working on a voice activated led strip using an Arduino Uno. It will take some time before my voice regognition module arrives. So I decided to use my Serial as an input and start working on some code already.

Everything was going well until I encountered a strange bug. The code waits for input from the Serial, which it's supposed to remember afterwards. The input part goes correctly, is the remembering where it's going wrong.

If my enter = 1, it will remember input as 0. If I enter 2, it will remember it as 255. If I enter 3, -32768. With 4 and everything above, it will remember it correctly. As 52 and higher, since it uses ascii if I'm not mistaken.

I have no idea where I messed up, and would like to ask you sweet people to help me look.

#include <FastLED.h>
#define DATA_PIN 3
CRGB leds[300];
int input;


void setup() {
  // put your setup code here, to run once:
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, 300);  // GRB ordering is assumed
  FastLED.setBrightness(20);
  Serial.begin(9600);
  input = 0;
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(input);
  if(Serial.available() > 0)
  {
    Serial.println("Writing");
    input = Serial.read();
  }
  Serial.println(input);
  switch (input)
  {
    case '1':
      Load();
      break;

    case '2':
      Load2();
      break;

    case '3':
      Load3();
      break;
  }
}

  void Load()
  {
   for(int i = 2; i < 301; i++)
    {      
    leds[i] = CHSV(190, 255, 110); 
    leds[i+1] = CHSV(190, 255, 130); 
    leds[i+2] = CHSV(190, 255, 170); 
    leds[i+3] = CHSV(190, 255, 190); 
    leds[i+4] = CHSV(190, 255, 230); 
    leds[i+5] = CHSV(190, 255, 255); 
    leds[i-1] = CRGB::Black;
    leds[i-2] = CRGB::Black;
    leds[i-3] = CRGB::Black;
 
    FastLED.show();
    }
  }
  void Load2()
  {
    for (int i = 2; i < 301; i++)
    {
      leds[i] = CRGB::Red;
      FastLED.show();

    }

  }

  void Load3()
  {
    for (int i = 2; i < 301; i++)
    {
      leds[i] = CRGB::Green;

      FastLED.show();
    }
  }

Thank you in advance :slight_smile:

Main.ino (1.33 KB)

fiftysand:
The code waits for input from the Serial, which it's supposed to remember afterwards. The input part goes correctly, is the remembering where it's going wrong.

How does it "remember"? Did you save the value somewhere? To a variable?

aarg:
How does it "remember"? Did you save the value somewhere? To a variable?

It's a global variable. Should be remembered right?

The input part goes correctly, is the remembering where it's going wrong.

Where in the code do you expect the value entered to be "remembered" ?

What have you got the Line ending set to in the Serial monitor ? The value of the line ending forms part of the serial input unless you set it to no line ending

UKHeliBob:
Where in the code do you expect the value entered to be "remembered" ?

What have you got the Line ending set to in the Serial monitor ? The value of the line ending forms part of the serial input unless you set it to no line ending

I thought because it's a global variable, and I assign it a value, which I do with input = Serial.read();, it will remember it after it has looped the program.

And yes, my Serial monitor is set to line ending.

I thought because it's a global variable, and I assign it a value, which I do with input = Serial.read();, it will remember it after it has looped the program.

The value of the input variable will remain the same unless/until it is set to something else, which the line ending will do because by definition it is after the character that you entered.

fiftysand:
I thought because it's a global variable, and I assign it a value, which I do with input = Serial.read();, it will remember it after it has looped the program.

Well, yes, you assign it to 'input'. You use it in several places. It's definitely "remembered" so maybe you're just not using it the way you think you are.

UKHeliBob:
The value of the input variable will remain the same unless/until it is set to something else, which the line ending will do because by definition it is after the character that you entered.

So is there some fix for that?

Start by setting the Line ending to No line ending

UKHeliBob:
Start by setting the Line ending to No line ending

I misstyped. My serial was already set to no line ending. I'm sorry.

CRGB leds[300];
int input;

   for(int i = 2; i < 301; i++)
     {     
    leds[i] = CHSV(190, 255, 110);
    leds[i+1] = CHSV(190, 255, 130);
    leds[i+2] = CHSV(190, 255, 170);
    leds[i+3] = CHSV(190, 255, 190);
    leds[i+4] = CHSV(190, 255, 230);
    leds[i+5] = CHSV(190, 255, 255);
    leds[i-1] = CRGB::Black;
    leds[i-2] = CRGB::Black;
    leds[i-3] = CRGB::Black;
 
    FastLED.show();
    }

Plowing through memory gives interesting results. :wink:

Especially when it is memory that you don't own!

Read this: arrays

Then ask yourself, with regard to the code posted in reply #10, when i == 300, which elements does i, i +1, i + 2, i +3, i + 4, i +5 access?