![]() |
Forum Index : Microcontroller and PC projects : IF THEN ELSE ELSEIF not working
Author | Message | ||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
For some reason I cannot get a subroutine to work I am parsing data from an ESP8266 the parsing routine is working fine. The data being sent from the ESP8266 always starts with either STX1 STXR STXR STXV so I know what the data that follows is I want the data from the line beginning STX1 to go to T1 STXR to go to T2 STXP to go to T3 STXV to go to T4 The ONLY way I can get it to work is this sub - but it's no good because it's collecting all the data no matter what the data begins with instead of each data going to separate subs they are all being processed by T1 [quote] Sub SERIAL2 'Com2 ESP8266 Weather Forecast LOCAL N as INTEGER N=GetFieldArray(ESP$) IF FieldArray$(0)="STX1" T1 'If the 1st array is "STXT" then GOSUB T1 ENDIF IF FieldArray$(0)="STXR" T2 'If the 1st array is "STXR" then GOSUB T2 ENDIF IF FieldArray$(0)="STXP" T3 'If the 1st array is "STXP" then GOSUB T3 ENDIF IF FieldArray$(0)="STXV" T4 'If the 1st array is "STXV" then GOSUB T4 ENDIF End Sub[/quote] The codes I have tried are [code]Sub SERIAL2 'Com2 ESP8266 Weather Forecast LOCAL N as INTEGER N=GetFieldArray(ESP$) IF FieldArray$(0)="STX1" then 'If the 1st array is "STXT" then GOSUB T1 T1 ELSEIF FieldArray$(0)="STXR" then 'If the 1st array is "STXR" then GOSUB T2 T2 ELSE FieldArray$(0)="STXP" then 'If the 1st array is "STXP" then GOSUB T3 T3 ELSE FieldArray$(0)="STXV" then 'If the 1st array is "STXV" then GOSUB T4 T4 ENDIF End Sub[/code] [code]Sub SERIAL2 'Com2 ESP8266 Weather Forecast LOCAL N as INTEGER N=GetFieldArray(ESP$) IF FieldArray$(0)="STX1" then 'If the 1st array is "STXT" then GOSUB T1 T1 ENDIF IF FieldArray$(0)="STXR" then 'If the 1st array is "STXR" then GOSUB T2 T2 ENDIF ELSE FieldArray$(0)="STXP" then 'If the 1st array is "STXP" then GOSUB T3 T3 ENDIF ELSE FieldArray$(0)="STXV" then 'If the 1st array is "STXV" then GOSUB T4 T4 ENDIF End Sub[/code] [code]Sub SERIAL2 'Com2 ESP8266 Weather Forecast LOCAL N as INTEGER N=GetFieldArray(ESP$) IF FieldArray$(0)="STX1" then 'If the 1st array is "STXT" then GOSUB T1 T1 IF FieldArray$(0)="STXR" then 'If the 1st array is "STXR" then GOSUB T2 T2 IF FieldArray$(0)="STXP" then 'If the 1st array is "STXP" then GOSUB T3 T3 IF FieldArray$(0)="STXV" then 'If the 1st array is "STXV" then GOSUB T4 T4 ENDIF ENDIF ENDIF ENDIF End Sub[/code] |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10310 |
You have a comment inside the command which overrides the rest of the line. Also IF THEN GOSUB is a single line command and doesn't need ENDIF Try Sub SERIAL2 'Com2 ESP8266 Weather Forecast LOCAL N as INTEGER N=GetFieldArray(ESP$) IF FieldArray$(0)="STX1" then GOSUB T1 'If the 1st array is "STXT" call T1 IF FieldArray$(0)="STXR" then GOSUB T2 'If the 1st array is "STXR" call T2 IF FieldArray$(0)="STXP" then GOSUB T3' If the 1st array is "STXP" call T3 IF FieldArray$(0)="STXV" then GOSUB T4 'If the 1st array is "STXV" call T4 End Sub Slightly more efficient using multiline format Sub SERIAL2 'Com2 ESP8266 Weather Forecast LOCAL N as INTEGER N=GetFieldArray(ESP$) IF FieldArray$(0)="STX1" then GOSUB T1 'If the 1st array is "STXT" call T1 ELSE IF FieldArray$(0)="STXR" then GOSUB T2 'If the 1st array is "STXR" call T2 ELSE IF FieldArray$(0)="STXP" then GOSUB T3' If the 1st array is "STXP" call T3 ELSE IF FieldArray$(0)="STXV" then GOSUB T4 'If the 1st array is "STXV" call T4 ENDIF End Sub |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
Thanks Peter |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2944 |
SELECT CASE is an alternative option available. I personally like this one as it makes easy reading at a later date! @Peter/@Geoff - Is there any speed advantage/dis-advantage between CASE and IF/THEN? Sub SERIAL2 'Com2 ESP8266 Weather Forecast LOCAL N as INTEGER N=GetFieldArray(ESP$) SELECT CASE FieldArray$(0) CASE "STX1" GOSUB T1 'If the 1st array is "STXT" call T1 CASE "STXR" GOSUB T2. 'If "STXR" call T2 CASE "STXP" GOSUB T3 'If "STXP" call T3 CASE "STXV" GOSUB T4 'If "STXV" call T4 END SELECT End Sub |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3292 |
No, both of them perform roughly the same. Generally IF/THEN is more convenient for simple decision trees while SELECT/CASE works better for complex cases. It is a matter of taste. Geoff Geoff Graham - http://geoffg.net |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
Thanks everyone, I solved the problem by just having one string coming from the ESP8266. I do have a 2nd problem now though which is related The idea of the SUB is IF a section of the weather data is missing instead of it being printing "not found" to the screen it prints one or 2 characters instead, that way the screen doesn't mess up The MM does get as far as the SUB, I put a test print in there to check but the SUB just gets ignored. [code]SUB CHECKARRAY 'Sub to check for "NOT FOUND" and replace with "" so display does not mess up print "I got here" if temp2$ = "Not Found" THEN temp2$ = "00" end if if icon$ = "Not Found" then icon$ = "50n" if temp1$ = "Not Found" THEN temp1$ = "00" end if IF press1$ = "Not Found" THEN press1$ = "00" end if if speed$ = "Not Found" THEN speed$ = "00" end if angle1$ = "Not Found" THEN angle1$ = "360" end if if hum1$ = "Not Found" THEN hum1$ = "00" end if if tempmin$ = "Not Found" THEN tempmin$ = "00" end if if tempmax$ = "Not Found" THEN tempmax$ = "00" end if if hum2$ = "Not Found" THEN hum2$ = "00" end if if vis$ = "Not Found" THEN vis$ = "00" end if if desc0$ = "Not Found" THEN desc0$ = "00" end if if rain44$ = "Not Found" THEN rain44$ = "00" end if if press2$ = "Not Found" THEN press2$ = "00" end if if uv$ = "Not Found" THEN uv$ = "00" end if if tempmin1$ = "Not Found" THEN tempmin1$ = "00" end if if tempmax1$ = "Not Found" THEN tempmax1$ = "00" end if if rain$ = "Not Found" THEN rain$ = "00" end if if tt1$ = "Not Found" THEN tt1$ = "00:00:00 " ENDIF if dow1$ = "Not Found" then dow1$ = "***" end if if dm1$ = "Not Found" then dm1$ = "00:JAN" end if if Year$ = "Not Found" then Year$ = "1900" end if END SUB[/code] This SUB just does not work, it gets ignored. Any idea why? |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
I "might" have it sorted. Trying [code] "Not" + CHR$(32) + "Found" THEN[/code] I have to wait for the "not found" to actually be sent before I know if it's working or not EDIT: It Didn't work - back to the drawing board |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
Does the "not found" always have the same case? "Not Found" is different to "Not found" Are there any leading or trailing space characters? To fix the case problem I use UCASE$ if rain$ = "Not Found" THEN rain$ = "00" end if becomes if UCASE$(rain$) = "NOT FOUND" THEN rain$ = "00" end if etc or if INSTR(UCASE$(rain$),"NOT FOUND") = 0 THEN rain$ = "00" end if to cover any leading or trailing spaces Jim VK7JH MMedit |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
Thanks Jim, I'll give that a try but I#m going to have to try and find an sd card socket somewhere locally, the one of the back of my display is not detecting the card anymore I've changed cards in case it was the card |
||||
robert.rozee Guru ![]() Joined: 31/12/2012 Location: New ZealandPosts: 2442 |
there are two errors in your code where bits are missing, marked ######## in the below: SUB CHECKARRAY 'Sub to check for "NOT FOUND" and replace with "" so display does not mess up print "I got here" if temp2$ = "Not Found" THEN temp2$ = "00" end if if icon$ = "Not Found" then icon$ = "50n" ######## if temp1$ = "Not Found" THEN temp1$ = "00" end if IF press1$ = "Not Found" THEN press1$ = "00" end if if speed$ = "Not Found" THEN speed$ = "00" end if ######## angle1$ = "Not Found" THEN angle1$ = "360" end if if hum1$ = "Not Found" THEN hum1$ = "00" end if if tempmin$ = "Not Found" THEN tempmin$ = "00" end if if tempmax$ = "Not Found" THEN tempmax$ = "00" end if if hum2$ = "Not Found" THEN hum2$ = "00" end if if vis$ = "Not Found" THEN vis$ = "00" end if if desc0$ = "Not Found" THEN desc0$ = "00" end if if rain44$ = "Not Found" THEN rain44$ = "00" end if if press2$ = "Not Found" THEN press2$ = "00" end if if uv$ = "Not Found" THEN uv$ = "00" end if if tempmin1$ = "Not Found" THEN tempmin1$ = "00" end if if tempmax1$ = "Not Found" THEN tempmax1$ = "00" end if if rain$ = "Not Found" THEN rain$ = "00" end if if tt1$ = "Not Found" THEN tt1$ = "00:00:00 " ENDIF if dow1$ = "Not Found" then dow1$ = "***" end if if dm1$ = "Not Found" then dm1$ = "00:JAN" end if if Year$ = "Not Found" then Year$ = "1900" end if END SUB you should also be indenting your code and adding blank lines to make it more readable. i would also suggest changing all the if-then statements in this particular subrouting to their single-line versions to improve readability: SUB CHECKARRAY 'Sub to check for "NOT FOUND" and replace with "" so display does not mess up print "I got here" if temp2$ = "Not Found" THEN temp2$ = "00" if icon$ = "Not Found" then icon$ = "50n" if temp1$ = "Not Found" THEN temp1$ = "00" IF press1$ = "Not Found" THEN press1$ = "00" if speed$ = "Not Found" THEN speed$ = "00" IF angle1$ = "Not Found" THEN angle1$ = "360" if hum1$ = "Not Found" THEN hum1$ = "00" if tempmin$ = "Not Found" THEN tempmin$ = "00" if tempmax$ = "Not Found" THEN tempmax$ = "00" if hum2$ = "Not Found" THEN hum2$ = "00" if vis$ = "Not Found" THEN vis$ = "00" if desc0$ = "Not Found" THEN desc0$ = "00" if rain44$ = "Not Found" THEN rain44$ = "00" if press2$ = "Not Found" THEN press2$ = "00" if uv$ = "Not Found" THEN uv$ = "00" if tempmin1$ = "Not Found" THEN tempmin1$ = "00" if tempmax1$ = "Not Found" THEN tempmax1$ = "00" if rain$ = "Not Found" THEN rain$ = "00" if tt1$ = "Not Found" THEN tt1$ = "00:00:00 " if dow1$ = "Not Found" then dow1$ = "***" if dm1$ = "Not Found" then dm1$ = "00:JAN" if Year$ = "Not Found" then Year$ = "1900" END SUB cheers, rob :-) |
||||
Paul_L Guru ![]() Joined: 03/03/2016 Location: United StatesPosts: 769 |
If you load your code from your post of 3/16 at 7:46 am into the extremely helpful MMEdit code editor and then select <P>rogram, <F>ormat you will instantly see an indented version of your code with vertical lines showing how all the IF ... ENDIF, DO ... LOOP, SUB ... END SUB, and other structures are paired. This will make the missing chunks of structure that Rob spotted above much more obvious. I definitely agree that sometimes the single line IF ... THEN structure can be much clearer. Stop tearing your hair out, use MMEdit to format your code! Paul in NY |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
I always use MM Edit I actually didn't know it could do that.... |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
A strange problem [code] if val(prob$) > 0 then Text 70,750, prob$ "% chance of "+type$, LB, 4, 1, RGB(BLACK),RGB(WHITE) end if print val(prob$), "Val"[/code] Result is 0.28 Val but the text line does not print I've even changed the 0 to 0.00 and it still won't print the text Edit: latest version of Picromite is what I'm using Edit2 I changed it to [code] val1 = val(prob$) * 100 print val1 if val1 > 2 then Text 70,750, prob$ "% chance of "+type$, LB, 4, 1, RGB(BLACK),RGB(WHITE) end if print val(prob$), "Val"[/code] Still does the same other than val1 = 28 which is correct but the if/then still doesn't work |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10310 |
Should be a "+" between prob$ and "%... |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
Thank you Peter I definitely owe you many beers or whatever else you drink and thanks to WhiteWizzard who told me about multiplying the number to make it greater than 0.5 |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |