The following information is for the EMANT300, EMANT380
Do Loop
Although the For loop solves Sam's tedium of making repeated light intensity measurement, he needs to know beforehand the number of measurements to take. He wonders if Visual Basic will allow him to stop measurements when the switch is closed.
In this exercise, we will make use of the do loop to make repeated measurements every second until a switch is pressed.
Open
the Visual Basic solution VB_2010_Solution.sln.
Set the ReadLightdo project as the startup project.
View the project code by opening the project's Module1.vb
If
you are using the EMANT380
Bluetooth DAQ, change the parameter to False and COM5
to the COM port you are using, See exercise 1 step 4.
DAQ.Open(False,"COM5")
Press Ctrl+F5 to Start without Debugging to run the program. Light Intensity measurements in Lux will be displayed at the rate of 1 measurement per second.
To stop the measurements, press the button on the light application adaptor.
Press any key to return to the development environment.
Imports Emant
Module Module1
Sub Main()
Dim swstate As Boolean
Dim volt, lux As Double
Dim DAQ As Emant300 = New Emant300
DAQ.Open(True,"COM5")
Do
volt = DAQ.ReadAnalog(Emant300.AIN.AIN0, Emant300.AIN.COM)
lux = 1333 * volt
Console.WriteLine(lux)
swstate = DAQ.ReadDigitalBit(3)
DAQ.Delay(1000)
Loop While swstate
DAQ.Close()
End Sub
End Module
If
you are writing this program from scratch, in order to use the
Emant300 class, the class library Emant300.dll must be
added to the references folder. See exercise 5.
The do statement conditionally executes the statement(s) in the loop one or more times.
Do
statement1;
statement2;
..
Loop Until boolean-expression
Do statements in Loop until the condition is true
Do
statement1;
statement2;
..
Loop While boolean-expression
Do statements in Loop while the condition is true
Do
volt = DAQ.ReadAnalog(Emant300.AIN.AIN0, Emant300.AIN.COM)
lux = 1333 * volt
Console.WriteLine(lux)
swstate = DAQ.ReadDigitalBit(3)
DAQ.Delay(1000)
Loop While swstate
Our do statement is executed as follows:
Control is transferred to the statements in the do loop. In our example, the program
measures the voltage,
convert to Lux,
displays the value,
read the switch state. If the switch is not pressed, swstate is true. If switch is pressed, swstate is false.
delays 1 sec
When and if control reaches the end point of the statements in the loop, the boolean-expression is evaluated.
If the boolean expression yields true, control is transferred to the beginning of the do statement. Otherwise, control is transferred to the end point of the do statement.
Modify
the traffic lights program you created earlier into a pedestrian
crossing, In a pedestrian crossing, the light stays green until the crossing switch is closed. The green
light then turns off, and the yellow and red light turns on in sequence.