The following information is for the EMANT300, EMANT380

# Exercise 10 – Do Loop

## Objective

• 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.

1. Open the Visual Basic solution VB_2010_Solution.sln.

2. Set the ReadLightdo project as the startup project.

3. View the project code by opening the project's Module1.vb

4. 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")``

5. 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.

6. To stop the measurements, press the button on the light application adaptor.

## Program 10.1 Measure Light Intensity Until Switch Pressed

``````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
lux = 1333 * volt
Console.WriteLine(lux)
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.

## Do statement

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
lux = 1333 * volt
Console.WriteLine(lux)
DAQ.Delay(1000)
Loop While swstate
``````

Our do statement is executed as follows:

1. 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

1. When and if control reaches the end point of the statements in the loop, the boolean-expression is evaluated.

2. 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.

## Pedestrian Crossing (optional exercise)

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.