Switch case locking up

Hi,

I am working on my exam project for my uni programming class. The program involves WS2812B leds, where I am programming a variety of different programs, which all currently goes into a switch case. However I am currently stuck on just the turn on/off segments of the code, each of which currently reside in separate cases. The code in and of itself works as intended, however when the code is run in a switch case, it locks up the switch, and I am unable to change to a different case.

Below is the entire loop, with case 1 & 2 being the ones of concern.

void loop() {

  if (Serial.available() <= 0)
  {

  }
  if (Serial.available() > 0)
  {
    Program = Serial.read() - 48;
  }
  switch (Program)
  {
    case 0:
      Serial.println("Please choose a program");
      Serial.println(Program);
      break;
    case 1:

      Serial.println("Program 1");
      Serial.println(Program);
      for (j = 0; j <= 59; j++)
      {
        strip.setPixelColor(j, WHITE);
        strip.show();
      }

      for (int i = 1; i <= 25; i++)
      {
        strip.setBrightness(i);
        Serial.println(i);
        strip.show();
        delay(25);
      }
      for (i = 25; i = 25; i = 25)
      {}

      break;

    case 2:
          Serial.println("Program 2");
      Serial.println(Program);
      for (j = 0; j <= 59; j++)
      {
        strip.setPixelColor(j, WHITE);
        strip.show();
      }
      for (int i = 25; i >= 1; i--)
      {
        strip.setBrightness(i);
        Serial.println(i);
        strip.show();
        delay(25);
      }
      for (i = 1; i = 1; i = 1)
      {}
      break;

    case 3:

      Serial.println("Program 3");
      Serial.println(Program);
      break;

    default:
      Serial.println("Please choose a program from 1 - 3");
      Serial.println(Program);
  }

  delay(1500);

}

I am relatively new to programming, so sorry if this is a simple question to answer.

As a sort of side question: The functions to turn on/off are also defined as two separate void functions, but I can not seem to call them in the switch.

Thank you in advance.

Sincerely
Jonas

Hello Jonas,

In the serial monitor what is line ending set to?

I am curious as to what those lines do. I have not seen a for like that before.

I have moved your question to a more suitable place in the forum.

When would there be less than no characters incoming? When the device doesn't like what it receives and sends it back to where it came from?

Also consider, if the condition is true, it does nothing. If it's false, it does nothing. So why does it exist?

Where is the entire sketch?

for (i = 1; i = 1; i = 1)

Hi
What is this?

An infinite loop. The value of an assignment expression is the value of the right hand expression.

Hi
In other words, it's a locking!!!! :grinning:

The 2nd i=25 : the for loop continues while this is true... which it always is, as it is non zero (assign i the value 25)... so it loops forever.

What are you trying to do with this code?

????

See reply #5.

It is set to No Line Ending.

It is to make sure i stays at either 25 or 1, otherwise it loops around and starts over.

OK, good. That was the most obvious thing I could think of that would cause you a problem. I'll leave it to others to help you more.

Thank you.

I can't remember its purpose, I included when I first made the switch case, but just never used it I guess.

It is about 250 - 260 lines long, and since the entirety of the turn on/off functions are included in case 1 & 2, I did not see it necessary to include the entire sketch, I thought that it would become too cluttered.

Below I have included the entire sketch.

#include <stdlib.h>
#include <TFT_eSPI.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define RED 255,0,0
#define GREEN 0,128,0
#define BLUE 0,0,255
#define WHITE 255,255,255
#define LED_DT 27
#define COLOR_ORDER GRB
#define LED_TYPE WS2812
#define NUM_LEDS 60
#define TFT_WHITE       0xFFFF      /* 255, 255, 255 */

Adafruit_NeoPixel strip(NUM_LEDS, LED_DT, NEO_GRB + NEO_KHZ800);

int i;
int k;
int j;
const int RanCol = random(0, 255);
const int SRanCol = random(0, 255);
const int TRanCol = random(0, 255);
#define r RanCol, SRanCol, TRanCol
TFT_eSPI tft = TFT_eSPI();
const int h = random(100, 1500);
const int sh = random(100, 1500);
int Program;

void MoodLight() {

  int locRan = random(0, 255);
  int SlocRan = random(0, 25);
  int TlocRan = random(0, 255);
#define p locRan, SlocRan, TlocRan
  for (int j = 0; j <= 59; j++)
  {
    strip.setPixelColor(j, p);
    strip.show();
  }
  for (int i = 5; i <= 100; i++)
  {
    strip.setBrightness(i);
    Serial.println(i);
    strip.show();
    delay(100);
  }

  delay(1500);
  for (int i = 100; i >= 5; i--)
  {
    strip.setBrightness(i);
    Serial.println(i);
    strip.show();
    delay(100);
  }
#undef p
}

void ChristmasLights() {

  for (int i = 0; i <= 59; i += 3)
  {
    Serial.println("i");
    strip.setPixelColor(i, RED);
    strip.show();
  }
  for (int k = 1; k <= 59; k += 3)
  {
    Serial.println("j");
    strip.setPixelColor(k, GREEN);
    strip.show();
  }
  for (int j = 2; j <= 59; j += 4)
  {
    Serial.println("k");
    strip.setPixelColor(j, BLUE);
    strip.show();
  }

  delay(h);

  for (int i = 1; i <= 59; i += 3)
  {
    Serial.println("i2");
    strip.setPixelColor(i, RED);
    strip.show();
  }
  for (int k = 0; k <= 59; k += 3)
  {
    Serial.println("k2");
    strip.setPixelColor(k, GREEN);
    strip.show();
  }
  for (int j = 2; j <= 59; j += 4)
  {
    Serial.println("j2");
    strip.setPixelColor(j, BLUE);
    strip.show();
  }

  delay(sh);

  for (int i = 2; i <= 59; i += 3)
  {
    Serial.println("i3");
    strip.setPixelColor(i, RED);
    strip.show();
  }
  for (int k = 1; k <= 59; k += 3)
  {
    Serial.println("k3");
    strip.setPixelColor(k, GREEN);
    strip.show();
  }
  for (int j = 0; j <= 59; j += 4)
  {
    Serial.println("j3");
    strip.setPixelColor(j, BLUE);
    strip.show();
  }
}

void taend() {
  for (j = 0; j <= 59; j++)
  {
    strip.setPixelColor(j, WHITE);
    strip.show();
  }

  for (int i = 1; i <= 25; i++)
  {
    strip.setBrightness(i);
    Serial.println(i);
    strip.show();
    delay(25);
  }
  for (i = 25; i = 25; i = 25)
  {}
}
void Sluk() {
  for (j = 0; j <= 59; j++)
  {
    strip.setPixelColor(j, WHITE);
    strip.show();
  }
  for (int i = 50; i >= 5; i--)
  {
    strip.setBrightness(i);
    Serial.println(i);
    strip.show();
    delay(100);
  }

}
// Taget fra 'Adafruit Strandtest'-eksempel.
void rainbow(int wait) {
  // Hue of first pixel runs 5 complete loops through the color wheel.
  // Color wheel has a range of 65536 but it's OK if we roll over, so
  // just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
  // means we'll make 5*65536/256 = 1280 passes through this outer loop:
  for (long firstPixelHue = 0; firstPixelHue < 5 * 65536; firstPixelHue += 256) {
    for (int i = 0; i < strip.numPixels(); i++) { // For each pixel in strip...
      // Offset pixel hue by an amount to make one full revolution of the
      // color wheel (range of 65536) along the length of the strip
      // (strip.numPixels() steps):
      int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
      // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
      // optionally add saturation and value (brightness) (each 0 to 255).
      // Here we're using just the single-argument hue variant. The result
      // is passed through strip.gamma32() to provide 'truer' colors
      // before assigning to each pixel:
      strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
    }
    strip.show(); // Update strip with new contents
    delay(25);  // Pause for a moment
  }
}
void setup() {
  Serial.begin(115200);
  // LEDS.addLeds <LED_TYPE, LED_DT, COLOR_ORDER> (leds, NUM_LEDS);
  strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.show();            // Turn OFF all pixels ASAP
  strip.setBrightness(25);
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif

}

void loop() {

  if (Serial.available() > 0)
  {
    Program = Serial.read() - 48;
  }
  switch (Program)
  {
    case 0:
      Serial.println("Please choose a program");
      Serial.println(Program);
      break;
    case 1:

      Serial.println("Program 1");
      Serial.println(Program);
      for (j = 0; j <= 59; j++)
      {
        strip.setPixelColor(j, WHITE);
        strip.show();
      }

      for (int i = 1; i <= 25; i++)
      {
        strip.setBrightness(i);
        Serial.println(i);
        strip.show();
        delay(25);
      }
      for (i = 25; i = 25; i = 25)
      {}

      break;

    case 2:
          Serial.println("Program 2");
      Serial.println(Program);
      for (j = 0; j <= 59; j++)
      {
        strip.setPixelColor(j, WHITE);
        strip.show();
      }
      for (int i = 25; i >= 1; i--)
      {
        strip.setBrightness(i);
        Serial.println(i);
        strip.show();
        delay(100);
      }
      for (i = 1; i = 1; i = 1)
      {}
      break;

    case 3:

      Serial.println("Program 3");
      Serial.println(Program);
      break;

    default:
      Serial.println("Please choose a program from 1 - 3");
      Serial.println(Program);
  }

  delay(1500);

}

All your loops like this will hang your program:

      for (i = 25; i = 25; i = 25)

Get rid of them.

It is to stop the code from looping around, if it is not included, the after i has either reached 25 or 1, it will wait 1500ms, and then run the code again.