Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 09:50 09 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : SetPin xx,FIN bug?

Author Message
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9745
Posted: 09:16pm 08 Mar 2017
Copy link to clipboard 
Print this post

This is strange, and I can't make it work INSIDE a running code.

If I ask for the result on the pin with the fansink fan tach on it AT THE COMMAND PROMPT, it works fine. The result is as expected.

If I ask for the result inside any running code, the result is ALWAYS zero:





Code that demonstrates this issue:


Const NSP_FAN_RPM=52

SetPin NSP_FAN_RPM,FIN,250


Do
Print Pin(NSP_FAN_RPM);
Print "-";
If Pin(NSP_FAN_RPM)<25 Then Exit Do
Pause 1000
Loop

Print "Exit."


Note that from the command-prompt, I can issue the IF/THEN, and it works.
But inside a running code, it will not.

Removing the 250ms gate time from the SetPin command makes no difference.
If you put Print Pin(NSP_FAN_RPM) after the SetPin command, but before the loop, the result is also zero:





If it IS something I am doing wrong, I would love to know what it is, but.....

Houston - do we have a bug?

EDIT: MMBASIC 5.03.02, MM+ Explore64.Edited by Grogster 2017-03-10
Smoke makes things work. When the smoke gets out, it stops!
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 09:28pm 08 Mar 2017
Copy link to clipboard 
Print this post

First you should only read the value of the pin once.
Otherwise you print something on screen and then test with another value which is unknown.
[code]
Const NSP_FAN_RPM=52

SetPin NSP_FAN_RPM,FIN,250


Do
Value = Pin(NSP_FAN_RPM)
  Print Value;
  Print "-";
  If Value < 25 Then Exit Do
  Pause 1000
Loop

Print "Exit."
[/code]
This probably not solve your problem as it seems to be in the firmware, but it will make debugging easier.
Microblocks. Build with logic.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9745
Posted: 09:31pm 08 Mar 2017
Copy link to clipboard 
Print this post

Nice idea - I will try that now....

EDIT: Nope... Nice idea though.



Edited by Grogster 2017-03-10
Smoke makes things work. When the smoke gets out, it stops!
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 09:59pm 08 Mar 2017
Copy link to clipboard 
Print this post

What about when you put a pause before you read the pin.
With such a short program it might be to fast.
Or query the pin with a timer, like once every second?

[code]
Const NSP_FAN_RPM=52

SetPin NSP_FAN_RPM,FIN,250


Do
  Pause 1000
  Value = Pin(NSP_FAN_RPM)
  Print Value;
  Print "-";
  If Value < 25 Then Exit Do
Loop

Print "Exit."
[/code]

Microblocks. Build with logic.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9745
Posted: 10:07pm 08 Mar 2017
Copy link to clipboard 
Print this post

This code snippet is from a much much bigger code, just to isolate this specific issue. This code does nothing useful, other then demonstrate the problem.

Will try adding a pause.....

....you might be onto something here:





Looks like you DO INDEED need a pause of some description directly before or after the command to make it work. Note how the first one is still zero - the one before the loop starts, then all subsequent loops work.

I had a PAUSE 1000 at the end of the loop, so all I did was move it to the top of the loop, and it works now - interesting.

EDIT: Yes, you need to pause at least the time specified in the SetPin command - 250mS in this case. If I pause 250, it works fine. If I pause 249 or less, I get zeros.

EDIT: Fine tuning of the delays results in a nice rapid-fire output - plenty fast enough for my purposes. I will make suitable changes to the big program and hopefully that will fix it.





Testing proves that you MUST put the pause before the command to read the pin. Placing the delay anywhere else - like at the end of the loop - results in zeros.

This would appear to be the work-around, but I would still like to know if this is considdered normal behaviour for this command.




Edited by Grogster 2017-03-10
Smoke makes things work. When the smoke gets out, it stops!
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2959
Posted: 10:21pm 08 Mar 2017
Copy link to clipboard 
Print this post

At the Command Prompt, there is a 'significant' delay in you typing each line of code. This gives enough 'time' for certain things to 'happen/finish' before typing your next 'thing'.

In a running program, the lines of code are executed extremely fast, meaning that with certain commands, there is not enough time to 'get the result' before doing a test such as you are trying to do.

You already seem to now be fixing it - but hopefully the above makes sense.

A good analogy is with a COM port reading example: You need to receive the whole message at lets say 2400 baud, BEFORE trying to decipher the input 1mS after issuing the 'go get message' command Edited by WhiteWizzard 2017-03-10
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9745
Posted: 10:25pm 08 Mar 2017
Copy link to clipboard 
Print this post

Yes, I am starting to see that now. This command just tricked me, cos I did not think it needed it, but there you go.
Smoke makes things work. When the smoke gets out, it stops!
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2959
Posted: 11:07pm 08 Mar 2017
Copy link to clipboard 
Print this post

From the manual (regarding Fin):

By default the gate time is one second which is the length of time that MMBasic will use to count the number of cycles on the input and this means that the reading is updated once a second with a resolution of 1 Hz. By specifying a third argument to the SETPIN command it is possible to specify an alternative gate time between 10 mS and 100000 mS. Shorter times will result in the readings being updated more frequently but the value returned will have a lower resolution. The PIN() function will always return the frequency in Hz regardless of the gate time used.


So with your gate time of 250ms (as per your code in original post), you have defined yourself the need to wait at least 250mS before doing any 'checks'

If it were a 'blocking' command, then no need to wait.

These things often catch me out too
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9745
Posted: 12:09am 09 Mar 2017
Copy link to clipboard 
Print this post

I learnt something new today, so it's all good.
Smoke makes things work. When the smoke gets out, it stops!
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2959
Posted: 12:49am 09 Mar 2017
Copy link to clipboard 
Print this post

Hi G - Please stay awake - I need to email you about something
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 09:37am 09 Mar 2017
Copy link to clipboard 
Print this post

  Grogster said  Looks like you DO INDEED need a pause of some description directly before or after the command to make it work. Note how the first one is still zero - the one before the loop starts, then all subsequent loops work.

I had a PAUSE 1000 at the end of the loop, so all I did was move it to the top of the loop, and it works now - interesting.


Hi Grog,

Is there a reason that you can't have all your pin definitions at the beginning of the code before the main processes start to run?

I've done most of mine like this. Don't know if that is good programming practice or not, but I like to see it all in one place.

I realise I don't need variables; should change them to constants...

[Code] 'Pins used - Inputs
Dim Integer SolarPanel=4 'Analog input ~ 1000mV
Dim Integer FlowSen1=5 'Water Flow Sensor Input
Dim Integer PinTmp=16 '1-Wire DS18B20 Input Pin
Dim Integer LimitSw1=24 'Motorised Ball Valve Limit Switch

SetPin SolarPanel,AIN
Setpin FlowSen1,FIN
Setpin(LimitSw1),AIn

'Pins used - Outputs
dim Integer PinPump1=26 'Pump Running (Red LED) & also to be Pump Relay output
Dim Integer Valve1Sw1=9 'Activated to Set Valve to HeatPump Mode
Dim Integer Valve1Sw2=10 'Activated to Set Valve to Solar Mode

Setpin(PinPump1),Dout,OC : Pin(PinPump1)=1
Setpin(Valve1Sw1),Dout,OC : Pin(Valve1Sw1)=1
Setpin(Valve1Sw2),Dout,OC : Pin(Valve1Sw2)=1
[/code]

I'm presuming I've got enough execution delay from setting them reading that I've not seen the problem.

Phil
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9745
Posted: 02:47pm 09 Mar 2017
Copy link to clipboard 
Print this post

I do.

The pins are defined in lines 282-289, and the main loop starts at line 565.
Lines 30-282 define all the DIM and CONST variables.
Lines 0-30 are just comments for future reference as to how the system is setup.

The above is ONLY AN EXAMPLE to show the problem. It does not reflect the actual layout of the code I am trying to use it in. The problem is the same regardless of if it is in the "Big" code, or in the examples above - the error results are exactly the same.

I have added a 25mS pause in the main loop, just before the command to read the FIN pin, and it now works fine. I reduced the gate time in the SetPin command to 10mS.
Accuracy is not important for this input. The code just needs to be able to detect if the fan has stopped spinning due to it being clogged up or having failed etc, so that it can then send a message for attention. The system will run 24/7, so the fansink fan WILL fail at some point, and so this needs to be monitored. There is also an 18B20 on the heatsink too.

....but I digress a little! Edited by Grogster 2017-03-11
Smoke makes things work. When the smoke gets out, it stops!
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025