Why is my switch case not repeating



//segments
int segments[8] = {2,3,4,5,7,8,9,10};
int one[2] = {3,7};
int two[5] = {8,7,10,5,4};
int three[5] = {8,7,10,3,4};
int four[4] = {9,10,7,3};
int five[5] = {8,9,10,3,4};
int six[6] = {3,4,8,5,9,10};
int seven[3] = {3,7,8};
int eight[7] = {3,4,7,8,5,9,10};
int nine[5] = {9,8,10,3,7};

int deploytime = 1;
const int button = 6;
int buttonState = 0;
int lastButtonState = 0;

void setup() {
  // put your setup code here, to run once:
pinMode(button,INPUT);
for(int i; i<8; i++){pinMode(segments[i], OUTPUT);}
Serial.begin(9600);
}

void loop() {







timechange();




}

void timechange(){

  

buttonState = digitalRead(button);

if(buttonState != lastButtonState){
if (buttonState == HIGH) {
  
deploytime++;
//for(int i; i<8; i++){
//  digitalWrite(segments[i],LOW);
Serial.print(deploytime);
//}
clear();

}  
}

lastButtonState = buttonState;

switch (deploytime){
case 1 :;for(int i; i<2; i++){digitalWrite(one[i],HIGH);}break;
case 2 :;for(int i; i<5; i++){digitalWrite(two[i],HIGH);}break;
case 3 :;for(int i; i<5; i++){digitalWrite(three[i],HIGH);}break;
case 4 :;for(int i; i<4; i++){digitalWrite(four[i],HIGH);}break;
case 5 :;for(int i; i<5; i++){digitalWrite(five[i],HIGH);}break;
case 6 :;for(int i; i<6; i++){digitalWrite(six[i],HIGH);}break;
case 7 :;for(int i; i<3; i++){digitalWrite(seven[i],HIGH);}break;
case 8 :;for(int i; i<7; i++){digitalWrite(eight[i],HIGH);}break;
case 9 :;for(int i; i<5; i++){digitalWrite(nine[i],HIGH);}break;
default:deploytime=1;
}


////loop();


}


void clear(){

digitalWrite(segments[0],LOW);
digitalWrite(segments[1],LOW);
digitalWrite(segments[2],LOW);
digitalWrite(segments[3],LOW);
digitalWrite(segments[4],LOW);
digitalWrite(segments[5],LOW);
digitalWrite(segments[6],LOW);
digitalWrite(segments[7],LOW);
return;
}






After it cycles through once it doesn't go round again. The actual variable 'deploytime' go's back to one but the switch case doesn't go through again.This is code for a water rocket timer to deploy the parachute its not finished. Just need this done.

To make it easier to help, post a schematic of your project. (It can be done freehand).

i think i got it working i just called loop(); at the default in switch case

it uses a seven segment display
and a button

What do you want to do here ? case 1 :;
May be case 1 : ?

That is incorrect code. The fact that it works for now is irrelevant. First, do an auto format, get rid of the extra vertical white space and change the switch to at least for int i=0;.....

That will not work for long. The loop() functions calls the timechange() function, the timechange() function then calls the loop() function, round and round it goes. Each time a function is called, memory is allocated on the stack (for local variables, the location in the code to return to when the function ends, etc), recursively calling functions will store more and more on the stack until you run out of ram. Often results in a processor reset, or the code just stops working and does nothing.

Sorry I'm not very good with Arduino coding I don't really understand what you mean

Where?
Here ////loop(); ?

Don't worry that was just me being stupid i figured it out thx for the help

What part, Tools/Auto Format
delete extra blank lines.
Supply an initial value for the for loops as in for int i =0; i<2; i++

Supply an initial value for the for loops as in for int i =0 ; i<2; i++

1 Like

isn't 'int i' set to 0 by default?

It might be, but you shouldn't rely on that. Compiling for Nano with IDE 1.8.13 and with warnings enabled, it shows this warning (for my test code, not your code):

deleteme2.ino:7:11: warning: 'i' is used uninitialized in this function [-Wuninitialized]
   for(int i; i<2; i++){

1 Like

NO, maybe a review of the C language and C++ language is in order? Relying on assumptions is always dangerous; be explicit.

See if this is what you need.

But the matrix for zero is missing.

I'm gonna start using circuit python since i'v been learning python in school for a while and have a better understanding of python than c or c+. I also prefer it

Im gonna use the adafruit circuit python boards

Anyways thx

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.