![]() |
Forum Index : Microcontroller and PC projects : Newie got a few questions
![]() ![]() ![]() ![]() |
|||||
Author | Message | ||||
MicroBlocks![]() Guru ![]() Joined: 12/05/2012 Location: ThailandPosts: 2209 |
Which values is printed by this statement? [code] PRINT FieldArray$(2) [/code] I think it might contain a wrong value as that would be possible if none of the IF statements are executed Microblocks. Build with logic. |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
I actually made a mistake which I spotted then Jean I had forgotten to include the statement that said what happens if the serial port has data so it was probably empty because none of the IF statements were executed as you guessed. This is the correct code with all the bits in the right place, but now I'm back to the [quote] N = GetFieldArray(F$) Error: Expected a number [/quote] [code]Open "COM1:19200" As #1 'Open port for HC-12 TX/RX OPEN "COM3:9200" AS #3 'Open port for ESP8266 IF LOC(#1) > 1 THEN SERIAL1 END IF IF LOC(#3) > 1 THEN SERIAL3 END IF SUB SERIAL3 Local F$ LOCAL N as INTEGER LOCAL arg$ F$ = Input$(254, #3) 'Suck everything in buffer out N = GetFieldArray(F$) IF FieldArray$(0) = "Today" THEN T1 ELSE IF FieldArray$(0) = "Day2" THEN T2 ELSE IF FieldArray$(0) = "Day3" THEN T3 ELSE IF FieldArray$(0) = "Day4" THEN T4 ELSE IF FieldArray$(0) = "Day5" THEN T5 END IF CLOSE #3 pause 200 OPEN "COM3:9200" AS #3 'Open port for ESP8266 End Sub SUB T1 IF FieldArray$(2) = "01d" THEN FieldArray$(2) = "01d.ppm" ELSE IF FieldArray$(2) = "01n" THEN FieldArray$(2) = "01n.ppm" ELSE IF FieldArray$(2) = "02d" THEN FieldArray$(2) = "02d.ppm" ELSE IF FieldArray$(2) = "02n" THEN FieldArray$(2) = "02n.ppm" ELSE IF FieldArray$(2) = "03d" THEN FieldArray$(2) = "03d.ppm" ELSE IF FieldArray$(2) = "03n" THEN FieldArray$(2) = "03n.ppm" ELSE IF FieldArray$(2) = "04d" THEN FieldArray$(2) = "04d.ppm" ELSE IF FieldArray$(2) = "04n" THEN FieldArray$(2) = "04n.ppm" ELSE IF FieldArray$(2) = "09d" THEN FieldArray$(2) = "09d.ppm" ELSE IF FieldArray$(2) = "09n" THEN FieldArray$(2) = "09n.ppm" ELSE IF FieldArray$(2) = "10d" THEN FieldArray$(2) = "10d.ppm" ELSE IF FieldArray$(2) = "10n" THEN FieldArray$(2) = "10n.ppm" ELSE IF FieldArray$(2) = "11d" THEN FieldArray$(2) = "11d.ppm" ELSE IF FieldArray$(2) = "11n" THEN FieldArray$(2) = "11n.ppm" ELSE IF FieldArray$(2) = "13d" THEN FieldArray$(2) = "13d.ppm" ELSE IF FieldArray$(2) = "13d" THEN FieldArray$(2) = "13n.ppm" ELSE IF FieldArray$(2) = "13d" THEN FieldArray$(2) = "50d.ppm" ELSE IF FieldArray$(2) = "50d" THEN FieldArray$(2) = "50n.ppm" END IF PRINT FieldArray$(2) DisplayPicture FieldArray$(2) , 392 , 185 END SUB[/code] |
||||
jman![]() Guru ![]() Joined: 12/06/2011 Location: New ZealandPosts: 711 |
Hi N needs to be a String As you already have the first part of the name why not just add the exetension SUB T1 FieldArray$(2) = FieldArray$(2) + ".ppm" PRINT FieldArray$(2) DisplayPicture FieldArray$(2) , 392 , 185 END SUB Regards Jman |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2932 |
@jman: If you look at Lewis' last three 'values' in the IF...ELSEIF, you will see he can't just add ".ppm" ALTHOUGH THIS MAY BE A TYPO as there are two IF...= "13d" ![]() @Lew247 Is there a TYPO error in your above code. If so then go with man's post. Also it is more 'compact' to use SELECT CASE Also, should it not be ELSEIF (I thought you could only have a single ELSE in a IF..THEN...ENDIIF |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
Whoops well spotted Phil Your correct the 3 X 13d's were typos Not certain about the elseif or else and if on a seperate line tbh Jman I'm about to try your suggestion Edit just tried it [quote]N = GetFieldArray(F$) Error: Expected a number[/quote] Brilliant idea though, saves all those if then else statements Thanks ![]() |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2932 |
jman's suggestion is what you need and WILL work. Your issue is with your above quote. Can you post your all your code and we can see where/why you are getting this error. There have been errors in other peoples code too which hasn't helped but we will get you there. Post your most recent version (or email it to me if you prefer) ![]() |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
Thanks guys I actually found the error I'd somehow deleted the Function that parsed the string from the serial port |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2932 |
So is everything working in code properly now, and as you expect it to? ![]() Or are you still getting error message(s) which are giving you issues? ![]() WW |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
Yes and NO For some reason PRINT FieldArray$(0) is printing the LAST item sent by the serial port and not the 1st |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
Here's the full program (so far) Phil I "think" the one I sent you last still had a bit missing, use this one if you want to look at it, it's a direct copy from MMEdit - just remember to rem out the rtc section I didn't include the .ppm files as they go over the 500K upload limit2016-09-05_070655_Clock.zip The problem now is no matter what I do - PRINT FieldArray$(0) or FieldArray$(1) always prints the last item received by the com port |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2932 |
Are you sure it s not actually returning the last element of the PREVIOUS message? Is FieldArray$(1) retuning the first element, and ........ FieldArray$(x) returning the last element where x = number of elements expected? EDIT: This is a question for your question two posts back! |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
It seems so - but no idea how to fix it Got to pop out now for a bit, I'll come back to it when I get home again Edit: spotted one mistake, I had OPEN "COM3:9200" AS #3 instead of OPEN "COM3:9600" AS #3 Didn't fix the problem though |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2932 |
I am curious/confused by the following code: IF FieldArray$(0) = "Today" THEN T1
ELSE IF FieldArray$(0) = "Day2" THEN T2 ELSE IF FieldArray$(0) = "Day3" THEN T3 ELSE IF FieldArray$(0) = "Day4" THEN T4 ELSE IF FieldArray$(0) = "Day5" THEN T5 END IF Your first line is a single line IF...THEN statement as you have put the 'calculation' immediately after the THEN. But your next line is ELSE The third line is once more a single IF..THEN Not by a MM to try at moment but could be some issues here. Give me an hour and I will be able to try/confirm ![]() |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
Your correct with the ELSE IF being on one line, I just checked the manual BUT I don't think it makes any difference if it's one line or 2 as the results are the same (just tried it) What I'm trying to do is check the first field received by the serial port then depending on what that word is - selects which SUB the program jumps to EDIT: I just tried printing FieldElement(5) and it still prints the last field.... [code]SUB SERIAL3 Local F$ LOCAL N as INTEGER F$ = Input$(254, #3) 'Suck everything in buffer out N = GetFieldArray(F$) PRINT FieldArray$(5) IF FieldArray$(0) = "Today" THEN T1 ELSE IF FieldArray$(0) = "Day2" THEN T2 ELSE IF FieldArray$(0) = "Day3" THEN T3 ELSE IF FieldArray$(0) = "Day4" THEN T4 ELSE IF FieldArray$(0) = "Day5" THEN T5 END IF PRINT F$ CLOSE #3 pause 200 OPEN "COM3:9200" AS #3 'Open port for ESP8266 End Sub [/code] |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
I went back to stage 1 with the fieldarray and made it as simple as possible the FULL code now is [code]DIM FIELDARRAY$ OPEN "COM3:9600" AS #3 'Open port for ESP8266 Do cls IF LOC(#3) > 0 THEN F$ = Input$(100, #3) 'Suck everything in buffer out N = GetFieldArray(F$) PRINT FieldArray$(0) PRINT F$ END IF Loop END Function GetFieldArray( Record$, Delimiter$, KeepQuotes ) AS INTEGER Local Index, Char, InQuote, Count InQuote = 0 Count = 0 FieldArray$(Count) = "" If Delimiter$ = "" Then Delimiter$ = "," For Index = 1 To Len(Record$) Char = Asc(Mid$(Record$, Index, 1)) If Char = 34 Then InQuote = Not InQuote If Not InQuote And Instr(Delimiter$, Chr$(char)) >= 1 Then Count = Count + 1 FieldArray$(Count) = "" Else If Char <> 34 Or KeepQuotes Then FieldArray$(Count) = FieldArray$(Count) + Chr$(char) EndIf EndIf Next GetFieldArray = Count + 1 End Function [/code] The result once I type "RUN" is [quote]run 1 Today,shower rain,09d,16.63,1 87 016,5.7,170,87 shower Today,shower 87 rain,09d,16.91,1016,5.7,170,87[/quote] |
||||
MikeO Senior Member ![]() Joined: 11/09/2011 Location: AustraliaPosts: 275 |
Lew I am pretty sure you need a Goto or Gosub after the Else If <Exp> then Goto/Gosub and also each statement must be on a line of its own. I am puzzled, If you are having problems with the FieldArray$ function why don't you use the function I gave you? Codenquilts |
||||
MicroBlocks![]() Guru ![]() Joined: 12/05/2012 Location: ThailandPosts: 2209 |
The first statement is wrong it should be: [code] DIM FieldArray$(20) [/code] Now it is just a single string instead of an array. Also adjust the size of the array to the maximum number of fields that you can get. Microblocks. Build with logic. |
||||
MM_Wombat Senior Member ![]() Joined: 12/12/2011 Location: AustraliaPosts: 139 |
@ lew247 You would have to DIMension an array before you use it. Change the first line to DIM FieldArray$(20) Should work ok now, and populate the array. Just kept truncating the subsequent fields, onto the end of FieldArray$(0) MMWombat Keep plugging away, it is fun learning But can be expensive (if you keep blowing things up). Maximite, ColourMaximite, MM+ |
||||
MM_Wombat Senior Member ![]() Joined: 12/12/2011 Location: AustraliaPosts: 139 |
@MicroBlocks Beaten to the punch MMWombat Keep plugging away, it is fun learning But can be expensive (if you keep blowing things up). Maximite, ColourMaximite, MM+ |
||||
MicroBlocks![]() Guru ![]() Joined: 12/05/2012 Location: ThailandPosts: 2209 |
Yeah. :) I would also change the line [code] IF LOC(#3) > 0 THEN [/code] Because why continue when there is only a single character in the buffer into [code] IF LOC(#3) > 50 THEN [/code] (Change the 50 into the number of characters you will at a minimum need) Better yet as you have control about the format you send, make your life simple and use. [code] INPUT$ #3, Day$, Description$, Icon$, Timestamp$, Temp, etc.... [/code] Microblocks. Build with logic. |
||||
![]() ![]() ![]() ![]() |
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |