The following information is for the EMANT300, EMANT380
For Loop
When Sam was doing the light measurement exercise, he thought that there must be a better to measure the light intensity repeatedly without him having to run the program each time he needs a reading.
Computers are capable of doing things over and over again without getting tired or bored like Sam. An interactive or repetitive loop allows a set of statements to be repeated.
In this exercise, we will study and run a program that measures the light intensity 10 times at the rate of one measurement per second.
Open the Visual Basic solution VB_2010_Solution.sln.
Set the ReadLightFor 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. 10 Light Intensity measurements in Lux will be displayed at the rate of 1 measurement per second.
Press any key to return to the development environment.
Imports Emant
Module Module1
Sub Main()
Dim volt, lux As Double
Dim i As Integer
Dim DAQ As Emant300 = New Emant300
DAQ.Open(True,"COM5")
For i = 0 To 9
volt = DAQ.ReadAnalog(Emant300.AIN.AIN0, Emant300.AIN.COM)
lux = 1333 * volt
Console.WriteLine(lux)
DAQ.Delay(1000)
Next
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 For statement is of the form:
For variable = expression1 To expression2 [ Step expression3 ]
statement2
..
Next
For i = 0 To 9
volt = DAQ.ReadAnalog(Emant300.AIN.AIN0, Emant300.AIN.COM)
lux = 1333 * volt
Console.WriteLine(lux)
DAQ.Delay(1000)
Next
In the above for loop, the light intensity is measured and displayed 10 times.
expression1: this is executed when the loop is entered. i is assigned 0 in the above example
expression2: if this is evaluated true, the statements in the loop are executed otherwise the loop is exited. Thus as long as i is less or equal to 9, light intensity is measured
expression3: this is executed each time after the statements in the loop are executed. i is incremented Step. If omitted, i increments by 1.
In the Emant300 class, we have a method called Delay. The program is delayed by the time in msec. The following line delays the program by 1000 ms before continuing with the next statement.
DAQ.Delay(1000)