PDA

View Full Version : "Select Case" Help



bmsherma
- 24th November 2009, 03:15
Is there a way to isolate a range of numbers in a case statement?

I've used this:

Select Case Voltage
Case is < 512
Expression...
End Select

But what if I want numbers less than 512 AND greater than 256??!

Tried this...didn't work:
Case is < 512 and is > 256

Tried this...didn't work:
Case 256 to 512

Tried this...didn't work:
Case 256:512

Any ideas how to do this? Basically I'm trying to type the code below in a more elegant way. I'm using IF-THEN's at the moment but since there is no ELSEIF command I have 4 separate blocks. I would like one block so I can use an ELSE statement (or in this situation a CASE ELSE). If I use an ELSE statement in the fourth block that ELSE statement will ALWAYS operate if the fourth block is false (even if one of the first 3 blocks is true)...NOT GOOD CODING!

PLEASE HELP!

If (Voltage <=256) and (Voltage > 0) Then
PORTC = %0001
PAUSE 100
Endif
If (Voltage <=512) and (Voltage > 256) Then
PORTC = %0010
PAUSE 100
ENDif
If (Voltage <= 768) And (Voltage > 512) Then
PORTC = %0100
Pause 100
endif
If (Voltage <= 1023) and (Voltage > 768) Then
PORTC = %1000
Pause 100
ENDif

How can I do this with Case Statements??

Thanks in advance!
-Brian

Darrel Taylor
- 24th November 2009, 06:09
I know this doesn't answer your question.
But perhaps all you need is ...
PORTC = DCD Voltage.HighByte
PAUSE 100

sayzer
- 24th November 2009, 07:54
<font color="#000080"><i>

</i></font>Temp <font color="#000080"><b>VAR WORD

</b></font>Temp = <font color="#FF0000">512 </font>- voltage <font color="#000080"><i>' Voltage

</i><b>SELECT CASE </b></font>Temp

<font color="#000080"><b>CASE IS </b></font>&lt; <font color="#FF0000">256 </font><font color="#000080"><i>' Voltage is greater than 256.

</i><b>END SELECT
</b></font>

Dave
- 24th November 2009, 11:36
bmsherma, Try this...

SELECT CASE VOLTAGE

CASE < 257
PORTC = %0001

CASE < 513
PORTC = %0010

CASE < 769
PORTC = %0100

CASE < 1024
PORTC = %1000

END SELECT
Pause 100

I use this construct when determining groups of numbers. You start with the smallest and move up to the largest.

Dave Purola,
N8NTA

bmsherma
- 24th November 2009, 14:42
Thanks everyone for the advice.

Dave P, your idea seems to work. However what if I want to isolate the low range OR the high range. I'm essentially trying to put logical operators in my case statement.

Case is < 256 OR is > 768
PORTC = %0001

Case Else
PORTC = %1000

Can I write that first Case statement with the OR term?

Or would I have to do this:

Case is < 256
PORTC = %0001
Case is > 768
PORTC = %0001
Case else
PORTC = %1000

Is there no way to use logical operators in Case Statements in PICBasicPRO??!

Dave
- 24th November 2009, 16:25
bmsherma, The second way you have it is the way I would do the construct..

Dave Purola,
N8NTA

Darrel Taylor
- 24th November 2009, 16:41
Is there no way to use logical operators in Case Statements in PICBasicPRO??!
NO!, logical operators are NOT allowed in Case statements.
<br>

bmsherma
- 25th November 2009, 16:02
Thanks everyone for the help!!

This forum is GREAT

Acetronics2
- 25th November 2009, 16:40
Hi,

With a very, very very little Trickery ... I think it makes it.




IF ( Voltage.8 ^ Voltage.9 ) THEN

PORTC = 1

ELSE

PORTC = 8

ENDIF



Alain

Acetronics2
- 26th November 2009, 08:16
Or ... With Select Case




Voltage = Voltage.Highbyte

SELECT CASE Voltage

Case 1,2

PORTC = 1

Case Else

PORTC = 8

END SELECT



Alain