Joined: 22 Jan 2006 Posts: 72 Location: Dublin, Ireland
Posted: Thu Jun 18, 2015 8:19 pm Post subject: Select Case issue
Hi All
This is really question to Mark
I have issue with Select Case in my code, I was able to reproduce the problem in the sample file for select case.
Using case statement with AND for example:
Case 1 And Test = 1 : Print "1"
Case 2 And Test = 1 : Print "2"
And then next case just plane value without AND
Case 6 : Print "6"
Case 6 seams to be ignored for some reason.
Code:
'----------------------------------------------------------------------------------------- 'name : case.bas 'copyright : (c) 1995-2013, MCS Electronics 'purpose : demonstrates SELECT CASE statement 'micro : Mega48 'suited for demo : yes 'commercial addon needed : no '-----------------------------------------------------------------------------------------
$regfile="m48def.dat"' specify the used micro $crystal=8000000' used crystal frequency $baud=19200' use baud rate $hwstack=32' default use 32 for the hardware stack $swstack=10' default use 10 for the SW stack $framesize=40' default use 40 for the frame space
Dim I AsByte'dim variable Dim S AsString*5 , Z AsString*5 Dim Test AsByte
Test =1
Do
Input"Enter value (0-255) " , I
SelectCase I
Case1And Test =1:Print"1" Case2And Test =1:Print"2" 'Case 3 To 5 : Print "3-5" make sure this is remark for first test Case6:Print"6" CaseIs>=10:Print">= 10" CaseElse:Print"Not in Case statement" EndSelect Loop End
After compiling with Case 3 To 5 : Print "3-5" all working fine.
Code:
'----------------------------------------------------------------------------------------- 'name : case.bas 'copyright : (c) 1995-2013, MCS Electronics 'purpose : demonstrates SELECT CASE statement 'micro : Mega48 'suited for demo : yes 'commercial addon needed : no '-----------------------------------------------------------------------------------------
$regfile="m48def.dat"' specify the used micro $crystal=8000000' used crystal frequency $baud=19200' use baud rate $hwstack=32' default use 32 for the hardware stack $swstack=10' default use 10 for the SW stack $framesize=40' default use 40 for the frame space
Dim I AsByte'dim variable Dim S AsString*5 , Z AsString*5 Dim Test AsByte
Test =1
Do
Input"Enter value (0-255) " , I
SelectCase I
Case1And Test =1:Print"1" Case2And Test =1:Print"2" Case3To5:Print"3-5"'Using 3 To 5 Seams To Be Fixing The Issue Case6:Print"6" CaseIs>=10:Print">= 10" CaseElse:Print"Not in Case statement" EndSelect Loop End
Regards
Pawel
(BASCOM-AVR version : 2.0.7.9 , Latest : 2.0.7.8 )
you use unsupported syntax. The idea of select case, is that a decision is made based on the value of a variable.
this is stretch a bit by using the IS where you can use syntax like you want. So use IS if you want to code like that. But a better way would be :
if test=1 then
select case I
.....
end if _________________ Mark
Joined: 22 Jan 2006 Posts: 72 Location: Dublin, Ireland
Posted: Thu Jun 18, 2015 9:44 pm Post subject:
Thank you Mark
Pity be cause i have really neat piece of code for analog keypad done this way with nesting like below.
"Un-nesting" is done after button is released and ADC value is less then 10
Code:
If Key_stop =1Then'Key_stop Alias Up_stop_down.2 If Up_stop_down_flag.2 =0Then
Up_stop_down_flag.2 =1 Call Beep_sound
Call Rotor_stop
End If
End If
Code:
$nocompile
Sub Convert_adc_button:
Analog0 =Getadc(0)
Analog1 =Getadc(1)
Analog2 =Getadc(2) ' lsp, mode, menue, preset , park Dbg'for stack debug If Analog0 <10And Key_button <>0Then
Key_button =0
Key_button_flag =0 Debug"key_button_relesed" End If If Analog1 <10And Up_stop_down <>0Then
Up_stop_down =0
Up_stop_down_flag =0 Debug"UP_stop_down_relesed" End If If Analog2 <10And Func_button <>0Then
Func_button =0
Func_button_flag =0 Debug"function_button_relesed" End If
Case695To710And Up_stop_down.0 =0:'key up
Up_stop_down.0 =1 Debug"Key_UP: "
Case590To620And Up_stop_down.1 =0:'Key down
Up_stop_down.1 =1 Debug"Key_Down: " Case315To335And Up_stop_down.2 =0:' Key stop
Up_stop_down.2 =1 Debug"Key_Stop" Case740To750And Up_stop_down.3 =0:'Key up and stop
Up_stop_down.3 =1 Debug"key_up and down" Case670To685And Up_stop_down.4 =0:' key stop and down
Up_stop_down.4 =1 Debug"key_stop and down" Case790To810And Up_stop_down.5 =0:' key up and down
Up_stop_down.5 =1 Debug"key_up and down"
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum