Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

Simple delay problem


MortenS Mar 11, 2011 10:46 AM

Using LoggerNet v4.1

I'm totally confused and getting nuts. I wanted to check if a for-to loop was delaying 100 or 110 seconds.
It was converted from Edlog CR10X to CR1000 CRbasic code.

For i1 = 1 To 11
Delay(0,10000,MSEC)
Next i1

But this didn't seem to work when I was monitoring it using RTCM.

Then I tried making a little simple program I have included below:


CRBasic Source code:


'CR1000 Series Datalogger

Public x = 0
Public Led

BeginProg
Scan (10,Sec,0,0)
x = x + 1
Led = 1
Delay (0,4,Sec)
Led = 2
NextScan
EndProg

Numeric Display setup:
Add Public "Led",TimeStamp and x

When running, x is incremented by 1 after 4 seconds after startup! (it should be incremented as the first thing.

Led is set to 2 after 4 seconds of runtime and does not change at all.

I tried to set the update interval from 1 Sec to 50mSec in the n update interval in the "Numeric Display" without any differents.

No schedules are enabled during the Numeric Dispaly" test.

Do I misunderstand some basic elements or have I just turned nuts ?

Best regards

Morten Skov


IslandMan Mar 11, 2011 11:46 AM

Timer is a good way to time things.

'CR1000 Series Datalogger

'Declare Public Variables
Public i1
Public Elapsed

'Main Program
BeginProg

Elapsed = 0

Scan (1,Sec,0,0)

Timer(1,Sec,2) 'Reset and Start Timer
For i1 = 1 To 11
Delay(0,10000,MSEC)
Elapsed = Timer(1,Sec,4) ' Read the timer
Next i1

NextScan
EndProg


MortenS Mar 11, 2011 12:55 PM

Thanks Dave

That looks fine and I will use timers next time.

But I would like to know what I do wrong in my little simple delay program.

morten


IslandMan Mar 11, 2011 06:19 PM

When running, x is incremented by 1 after 4 seconds after startup! (it should be incremented as the first thing.
You have a 10 second scan rate. That means the instructions will execute at 10, 20, 30 seconds and so on. YOu may have started watching it with at 6 seconds into the 10 second interval and that's why you saw it change after 4 seconds and not immediately.
Led is set to 2 after 4 seconds of runtime and does not change at all.
You told LED to equal 2 after the 1st 4 second delay and never told it to do anything else. It will remain at 2 forever. Am I missing something here?


MortenS Mar 11, 2011 10:40 PM

Hi Dave

Maybe I don't understand the scan..nextscan


You told LED to equal 2 after the 1st 4 second delay and never told it to do anything else. It will remain at 2 forever. Am I missing something here?

I'm telling to set led = 1 in line 4. So I got totally confused when you said that led would be 2 forever.
As I have understood, line 3-6 is executed every 0,10,20,30,40,50 seconds every minut. (Like a never ending loop) and then executing the lines one after each other in that loop ?

1 BeginProg
2 Scan (10,Sec,0,0)
3 x = x + 1
4 Led = 1
5 Delay (0,4,Sec)
6 Led = 2
7 NextScan
8 EndProg


If you could make another example so I can understand the problem I would bee glad :-)

morten

* Last updated by: MortenS on 3/11/2011 @ 3:42 PM *


IslandMan Mar 12, 2011 01:41 AM

Hi Morten,

I was mistaken, the LED variable will change from 1 to 2 every 10 second scan but it will happen very fast as you have told it the delay command to delay the analog measurement task sequence and you have no analog measurements. Perhaps what you meant to do was pause the entire program for 4 seconds and all you have to do is change this:
Delay (0,4,Sec)
to this:
Delay (1,4,Sec)
Now you will see LED be 1 and after the 4 second delay it will change to 2 until the balance of the 10 second scan is over in which case it will be set back to 1.
If you right click on any of the commands such as Delay it will bring up a menu for you that explains these things as well as a help button to see examples and more help text.


MortenS Mar 14, 2011 08:49 AM

Thanks Dave

Now you hit the nail.

Think I got it now :-)


morten


IslandMan Mar 14, 2011 10:40 AM

No problem, happy to help.


Minh T. Aug 23, 2017 05:02 AM

IslandMan - Two questions (and only 2, I promise!) for you since you seem to understand how Delay(1,...) works. 

I'm observing that the Delay(0,...) command seems to be ignored in my application. An instrument I'm working with requires 5 seconds of silence on its COM lines after being powered (or else it will misbehave and needs to be power-cycled) before commands can be sent to it. 

My code looks like this: 

BeginProg
  <Do things> 
  SW12(1)                   'Turn on the instrument, connected to Com1
  Delay(0,5,Sec)         'Wait 5 seconds before sending anything out to Com1
  SerialOut(Com1, ...)

Scan()
...

NextScan

EndProg


Question 1: CRBasic seems to categorize and discern "Measurement Task Instructions", "Digital Task Instructions", and "Processing Tasks Instructions". Here, I'm using the term "Instruction" to mean a unit of code ... like a statement terminated by a semicolon in C (correct me if I'm wrong).The manual gives some examples (CR1000 Measurement and Control, p.133) but doesn't mention whether flow control code (conditional statements/loops) or variable/constant declarations or many other syntaxic elements are processing instructions. 

Are flow control instructions, variable declarations, expression evaluations, sub routine calls "Processing Tasks"? I need to know because my program relies on a function that delays the execution of ALL instructions. 

Question 2: Sometimes, it is necessary for my program to delay program execution inside a branch of a conditional statement (i.e., an else if, else,... block). The CRBasic API specifies that the Delay(0,...) routine should not be called within a conditional statement. Why? Also, is it ok to use Delay(1,...) inside a conditional statement? 

Log in or register to post/reply in the forum.