Exercise 7 - If Statement, Digital Output
Objective
-
Learn If statement
-
Revise analog input
-
Learn digital output
Night lights are normally used in children's bedrooms to reduce their fear of the dark and to provide some light in a darkened room. But even without children, a home should use night lights for safety. The lights use built-in photo sensors so that they turn on only at night.
In this exercise, we will program the light sensor and the Green LED to create a simple night light. We use the digital outputs to turn on and off the LED. To do this we will use the if statement
-
Create a new Visual Basic console project. Name the project Ifled
-
Add the highlighted lines to the generated source code
-
Add the following line if you are using the simulator
DAQ.Simulation = True
Program 7.1 Night Light
Imports Emant
Module Module1
Sub Main()
Dim volt, lux, compare As Double
Dim input As String
Console.WriteLine("Light Threshold in Lux")
input = Console.ReadLine
compare = Convert.ToDouble(input)
Dim DAQ As Emant300 = New Emant300
DAQ.Open()
volt = DAQ.ReadAnalog(Emant300.AIN.AIN0, Emant300.AIN.COM)
lux = 1333 * volt
Console.WriteLine(lux)
If lux < compare Then
DAQ.WriteDigitalBit(0, True)
Else
DAQ.WriteDigitalBit(0, False)
End If
DAQ.Close()
End Sub
End Module
-
In order to use the Emant300 class, add the class library Emant300.dll to the references folder. Repeat steps 4 to 8 from exercise 5.
-
Press Ctrl+F5 to Start without Debugging to run the program. Enter the Lux threshold and observe the bulb brightness. The Light Intensity measured in Lux is also displayed.

-
Press any key to return to the development environment.
-
Run the program several times. Choose a Lux threshold that is between the light intensity when the Photodiode is covered and when it is not. Observe the Green LED turning on when the light level drops below the threshold.
DAQ & Visual Basic Page 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37