OK, this is part 2 of our Easy Module Shield examples using B4R. Lets crack on.
RGB LED
[codesyntax lang=”qbasic”]
#Region Project Attributes #AutoFlushLogs: True #CheckArrayBounds: True #StackBufferSize: 300 #End Region Sub Process_Globals Public Serial1 As Serial Private rpin, gpin, bpin As Pin End Sub Private Sub AppStart Serial1.Initialize(115200) Log("AppStart") rpin.Initialize(9, rpin.MODE_OUTPUT) gpin.Initialize(10, rpin.MODE_OUTPUT) bpin.Initialize(11, rpin.MODE_OUTPUT) 'red rpin.AnalogWrite(255) gpin.AnalogWrite(0) bpin.AnalogWrite(0) Delay(500) 'green rpin.AnalogWrite(0) gpin.AnalogWrite(255) bpin.AnalogWrite(0) Delay(500) 'blue rpin.AnalogWrite(0) gpin.AnalogWrite(0) bpin.AnalogWrite(255) Delay(500) End Sub
[/codesyntax]
DHT11 Example
[codesyntax lang=”qbasic”]
Sub Process_Globals Public Serial1 As Serial Public DHT11sensor As dht Public Timer1 As Timer Public DHT11pin As Pin Public Interval As Int Dim humidity,temperature As Double 'DHT22 readings End Sub Private Sub AppStart Serial1.Initialize(115200) Interval=5 '5 sec Timer Interval DHT11pin.Initialize(4,DHT11pin.MODE_INPUT) Timer1.Initialize("Timer1_Tick",Interval*1000) Timer1.Enabled=True Log("AppStart") End Sub Sub Timer1_Tick DHT11sensor.Read11(DHT11pin.PinNumber) humidity=DHT11sensor.GetHumidity temperature=DHT11sensor.GetTemperature Log("Humidity = ",NumberFormat(humidity,0,2), " %", " Temperature =",NumberFormat(temperature,0,2), " DegC") End Sub
[/codesyntax]
Logs should show something like this – seems to be quite slow
Humidity = 19.00 % Temperature =22.00 DegC
Humidity = 19.00 % Temperature =22.00 DegC
Humidity = 19.00 % Temperature =22.00 DegC
Humidity = 19.00 % Temperature =22.00 DegC
Humidity = 19.00 % Temperature =22.00 DegC
Humidity = 19.00 % Temperature =22.00 DegC
Humidity = 19.00 % Temperature =22.00 DegC
Humidity = 19.00 % Temperature =22.00 DegC
Humidity = 18.00 % Temperature =23.00 DegC
Humidity = 18.00 % Temperature =23.00 DegC
Humidity = 18.00 % Temperature =23.00 DegC
Humidity = 18.00 % Temperature =23.00 DegC
Humidity = 18.00 % Temperature =23.00 DegC
LM35 Example
[codesyntax lang=”qbasic”]
Sub Process_Globals Public Serial1 As Serial Private LM35Pin As Pin 'Output pin connected from the LDR Private LM35PinNumber As Byte = 0x02 'Pin number used is A2 (Analog) Private MeasureTimer As Timer 'Timer for the sensor measurement Private MeasureTimerInterval As ULong = 1 'Timerinterval in seconds End Sub Private Sub AppStart Serial1.Initialize(115200) Log("AppStart - LM35PIN ", LM35PinNumber, " read every ", MeasureTimerInterval, " seconds") 'Init the pin with LM35 connected LM35Pin.Initialize(LM35PinNumber, LM35Pin.MODE_OUTPUT) 'Init the timer MeasureTimer.Initialize("MeasureTimer_Tick", MeasureTimerInterval * 1000) 'Start the timer MeasureTimer.Enabled = True End Sub 'Handle the timer ticks Private Sub MeasureTimer_Tick 'Read the current state of the pin Dim rawvoltage As UInt = LM35Pin.AnalogRead 'Convert rawvoltage To celsius And fahrenheit Dim volts As Double = rawvoltage / 1023.0 * 5000 Dim celsiustemp As Double = volts * 0.1 Log("Celsius: ", celsiustemp) End Sub
[/codesyntax]
Look at the log window
Celsius: 22.4829
Celsius: 22.4829
Celsius: 23.4604
Celsius: 23.9492
Celsius: 24.4379
Celsius: 24.9267
Celsius: 25.4154
Celsius: 25.4154
Celsius: 25.4154
Celsius: 25.9042
Celsius: 26.3930
Celsius: 26.3930