Pages: 1 [2]   Go Down
Author Topic: Avoid multiple switch case and if else statements  (Read 1410 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

HMMM, how i can write same ? mean 2 bush button and out case leds 1-5 ?
start led 1 high
button1 led only up, led2 high all other low
i understand case row, case1 led1 hight,led2low,led3low etc,

but how make buttons and how read what is led state,and read led state button up or down move state new led.
mena state is 3 up button  move state 4 or down button  move state and led 2
i read and read but no understand switch case,array or what i must use?
 loop and how mark state to memory were read state and then bush button and move led up or down and write state to memory again ?
Buttons have A1 and A2 const int input
leds have D3,4,5,6,7 const int output
int have buttonstate=0
int lastbuttonstate=0 (one examples copy,i try and try but no understand.
lastbuttonstate=buttonstate
and no understand if buttonstate !=lastbuttonstate ???????
buttonstate=digitalread A1 or A2 button
but how jump to case ???? if buttonstate change up or down ???
or how make thats output binary ? start leds   have 1,0,0,0,0,
and if button down nothing and button up leds have 0,1,0,0,0
                                                    next up have 0,0,1,0,0
                                                    next up have 0,0,0,1,0
                                                    next up have 0,0,0,0,1
and lastbutton remember state alltime change up or down etc, 2 and button up change 3 or Button down change 1 etc.
sorry my write,but no understand,read and read lot but no all my test working smiley-sad
 
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12549
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm afraid that your description of the behaviour you want to achieve is incomprehensibe.

You need to either work out for yourself how to achieve that behaviour using state variables and the usual control structures, or come up with a coherent description of what it needs to do in enough detail for somebody else to understand, so that we can suggest algorithms that could be used to achieve it.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello again. This is the full code with the suggested switch case statements inserted. It works very well and has avoided many lines of if else statements . My thanks to everyone who contributed, I have learned a lot.

Code:
void morseOne(void) {          
  flash (100), delay(350), flash (400);         //morse code number 1
    }
  void morseTwo(void) {        
   flash (100), delay(350), flash (100), delay(350),  flash (400); //morse code number 2
  }
  void morseThree(void) {      
  flash (100), delay(350), flash (100), delay(350),flash (100), delay(350),flash (400); //Morse 3
    }
...

I strongly suggest you use semicolons there and not commas. Commas have a totally different meaning to what you seem to think here. For example, change:

Code:
void morseOne(void) {          
  flash (100), delay(350), flash (400);         //morse code number 1
    }

To:

Code:
void morseOne(void)
   {          
   flash (100);  
   delay (350);
   flash (400);        
   }

Also make use of new lines and indentation. Space is cheap, and adds to readability.



You might also want to consider making the numbers constants. Is 100 "dot" and 400 "dash"? Well how about making it explicit?

Code:
const int DOT = 100;
const int GAP = 350;
const int DASH = 400;

 void morseOne(void)
   {          
   flash (DOT);  
   delay (GAP);
   flash (DASH);        
   }

Then if you ever decide to make dots (say) 110 you only have to change one place and not dozens.
Logged

Pages: 1 [2]   Go Up
Jump to: