C switch case Vs PB Select Case

Everything else that doesn't fall into one of the other PB categories.
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

C switch case Vs PB Select Case

Post by Baldrick »

Just wondering if any of the C ppl here can tell me of a way to be able to use Case in C with multiple values so I can type less. :D
e.g. Sudeo code in PB We can do something like:

Code: Select all

				ADCValue(channel_number)=ADRESH; 
					Select ADCValue(0)
						Case 130 To 145	;4k7
							ProcedureReturn 1;
						Case 175 To 195	;6k8
							ProcedureReturn 2; 
					EndSelect 
In C I am at the moment doing this something like:

Code: Select all

				ADCValue[channel_number]=ADRESH; 
					switch (ADCValue[0]){
						Case 130: Case 131: Case 132: Case 133: Case 134: Case 135: Case 136: Case 137: 
						Case 138: Case 139: Case 140: Case 141: Case 142: Case 143: Case 144: Case 145:	//4k7
							Return 1;
							Break;
						Case 175: Case 176: Case 177: Case 178:Case 179: Case 180: Case 185: Case 186: Case 187: 
						Case 188: Case 189: Case 190: Case 191: Case 192: Case 193: Case 194: Case 195:	//6k8
							Return 2; 
							Break;
					};
So just wondering if any1 knows a way to use any similar 'To' statement in C something like the way it works in PB?
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: C switch case Vs PB Select Case

Post by Fred »

Better use standard 'if' 'else if' construct in your case, it will be faster and easier to read.
Perkin
Enthusiast
Enthusiast
Posts: 504
Joined: Thu Jul 03, 2008 10:13 pm
Location: Kent, UK

Re: C switch case Vs PB Select Case

Post by Perkin »

@Baldrick
Did you realise that the check for 181->184 is missing in the C version?
%101010 = $2A = 42
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Re: C switch case Vs PB Select Case

Post by Baldrick »

Thanks Fred.
Currently I have used 8 bits of a 10 bit resolution ADC converter by dropping the 2 low order bits in the function register, so I might see if I can do a little bitshifting on my left over high order bits to turn my resolution down from 8 bits to 6 also which might make my coding a little easier to work with & hopefully should keep within my desired needs with this project.
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Re: C switch case Vs PB Select Case

Post by Baldrick »

Perkin wrote:@Baldrick
Did you realise that the check for 181->184 is missing in the C version?
No matter Perkin, it is only a rough seudo code pulled from a microcontroller project & quickly even more roughed up in the PB editor to try & help explain what I would have liked to do with the C code.
Guess I am just made a little lazy by the ease of PB these days.... :mrgreen:
Perkin
Enthusiast
Enthusiast
Posts: 504
Joined: Thu Jul 03, 2008 10:13 pm
Location: Kent, UK

Re: C switch case Vs PB Select Case

Post by Perkin »

Guess I am just made a little lazy by the ease of PB these days.... :mrgreen:
I know what that's like :D
%101010 = $2A = 42
User avatar
Airr
User
User
Posts: 49
Joined: Tue Oct 04, 2005 4:29 am
Contact:

Re: C switch case Vs PB Select Case

Post by Airr »

Could you do something like this?

Code: Select all

if (ADCValue[0]>=130 && ADCValue[0]<=145) {
    Return 1;
}
AIR.
"Programming is like Poetry. Sometimes the words just escape you..." -me, to my manager.
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Re: C switch case Vs PB Select Case

Post by Baldrick »

Airr wrote:Could you do something like this?

Code: Select all

if (ADCValue[0]>=130 && ADCValue[0]<=145) {
    Return 1;
}
AIR.
That certainly looks like a good way to simplify things for me. I will give it a go. I have a reasonably stable proto board working now, which I will recode completely soon & will try this in the new code.
fwiw, I just simply bitshifted my analogue to digital resolution down to 6 bits which has worked wonders also. :)
It is just a pity that we cannot do some of the neat little tricks in C such as the Case X to Y thing which works so well for me in PB..
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Re: C switch case Vs PB Select Case

Post by Baldrick »

lol, np. In my situation I have stuck with using the switch case routine & currently I have moved forward enough to be at a stage where I am now in the process of getting a 1st run prototype production of my micrcocontroller pcb's manufactured. So it's all good. :mrgreen:
User avatar
Avatar Engineer
New User
New User
Posts: 5
Joined: Tue May 11, 2010 3:18 am
Location: Scottsdale, Arizona
Contact:

Re: C switch case Vs PB Select Case

Post by Avatar Engineer »

Most C compilers (and Pascal, etc) can only use a constant as a case.
Therefore any complex conditions to share a few case statements needs
a prior function to convert conditions to a constant.

If you think of "switch" like a rotary switch, it helps to overcome
the despair of being unable to condense the source code lines.

peace.
Post Reply