2021年11月28日 星期日

ESP32 BMP280 大氣壓強度(高度計)感測模組

 ESP32 BMP280 BMP280 大氣壓強度(高度計)感測模組 




ESP32 SCL (GPIO 22) and SDA (GPIO 21) pins


#include <Wire.h>             // include Wire library, required for I2C devices

#include <Adafruit_Sensor.h>  // include Adafruit sensor library

#include <Adafruit_BMP280.h>  // include adafruit library for BMP280 sensor

#define BMP280_I2C_ADDRESS  0x76


Adafruit_BMP280 bmp280;

 

void setup() {

  Serial.begin(115200);

  Serial.println(F("BME280 test"));

  if (!bmp280.begin(BMP280_I2C_ADDRESS))

  {  

    Serial.println("Could not find a valid BMP280 sensor, check wiring!");

    while (1);

  }

  

}


void loop()

{

    // get temperature, pressure and altitude from library

    float temperature = bmp280.readTemperature();  // get temperature

    float pressure    = bmp280.readPressure();     // get pressure

    float altitude_   = bmp280.readAltitude(1013.25); // get altitude (this should be adjusted to your local forecast)

    //1013.15需修正

    //該1013.25值應為數百Pa處的海平面局部壓力。

    //如果要顯示高於地面的高度,則需要知道該位置,並編寫代碼以計算高度偏移。

    //海平面:泛指我們所處的高度,即1大氣壓(1atm或1013.25hPa 或 1013.25mb)

    //That 1013.25 value should be the local pressure at sea level in hundreds of Pa. 

    //If want to show altitude above ground level, you will need to know that for your location and write your code to calculate the altitude offset.

    // print data on the serial monitor software

    // 1: print temperature

    Serial.print("Temperature CELSIUS 攝氏溫度= ");

    Serial.print(temperature);

    Serial.println(" °C");

    float TempF = (temperature*1.8)+32;

    Serial.print("Temperature FAHRENHEIT 華氏溫度= ");

    Serial.print(TempF);

    Serial.println(" °F");

    // 2: print pressure

    Serial.print("Pressure 大氣壓力百帕(hPa)= ");

    Serial.print(pressure/100);

    Serial.println(" hPa");

    // 3: print altitude

    Serial.print("Approx Altitude 大概海拔高度= ");

    Serial.print(altitude_);

    Serial.println(" m");

    delay(2000);

}    

2021年11月21日 星期日

B4R Object arrays 用法

 B4R  Object arrays 用法




'Initialize the buttons and add the StateChange event
'Analog pins A4, A5
'Analog pins A0 = 14, A1 = 15, A2 = 16, A3 = 17, A4 = 18, A5 = 19
'in our case (i + 18), 18 and 19, therefore A4 and A5 
For i = 0 To NbButtons - 1
'Using the internal pull up resistor to prevent the pin from floating.
pinButtons(i).Initialize( i + 18, pinButtons(i).MODE_INPUT_PULLUP)
pinButtons(i).AddListener("pinButtons_StateChanged")
Next

'Initialize the LEDs
'Digital pins 7, 8, 9, 10
For i = 0 To NbLEDs - 1
pinLEDs(i).Initialize(i + 7, pinLEDs(i).MODE_OUTPUT)
Next


===========================================

#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region
Sub Process_Globals
Public Serial1 As Serial

Public NbLEDs = 4 As Int 'Number of Leds
Public pinLEDs(4) As Pin 'Pins for the Leds
Public NbButtons = 2 As Int 'Number of buttons
Public pinButtons(2) As Pin 'Pins for the buttons

Public TimerLEDs As Timer
Public LightON = False As Boolean 'lights ON or OFF, ON = True
Public LEDIndex = 0 As Int 'index of current LED
Public AllLedsON = False As Boolean 'lighting type 
Private BounceTime(2) As ULong
Private BounceDelay = 10 As ULong
End Sub
Private Sub AppStart
Private i As Int

Serial1.Initialize(115200)
Log("APP Starting.....")
TimerLEDs.Initialize("TimerLEDs_Tick", 500)

'Initialize the buttons and add the StateChange event
'Analog pins A4, A5
'Analog pins A0 = 14, A1 = 15, A2 = 16, A3 = 17, A4 = 18, A5 = 19
'in our case (i + 18), 18 and 19, therefore A4 and A5 
For i = 0 To NbButtons - 1
'Using the internal pull up resistor to prevent the pin from floating.
pinButtons(i).Initialize( i + 18, pinButtons(i).MODE_INPUT_PULLUP)
pinButtons(i).AddListener("pinButtons_StateChanged")
Next

'Initialize the LEDs
'Digital pins 7, 8, 9, 10
For i = 0 To NbLEDs - 1
pinLEDs(i).Initialize(i + 7, pinLEDs(i).MODE_OUTPUT)
Next
End Sub
Private Sub pinButtons_StateChanged (State As Boolean)
Private p As Pin

Log("State: ", State) 'Log the State value
p = Sender 'get the Pin which raised the event
Log("Pin Number: ", p.PinNumber) 'Log the Pin Number
Select p.PinNumber
Case 18 'Lights ON / OFF
If State = False Then 'if State = False, button down
If Millis - BounceTime(0) < BounceDelay Then
Return
Else
AllLedsON = Not(AllLedsON) 'change the state of AllLedsON
BounceTime(0) = Millis
If LightON = True Then 'if LightOn = False
For i = 0 To NbLEDs - 1
'switch OFF or ON all LEDs depending on AllLedsON
pinLEDs(i).DigitalWrite(AllLedsON) 
Next
'switch ON or OFF the current LED depending on AllLedsON
pinLEDs(LEDIndex).DigitalWrite(Not(AllLedsON))
End If
End If
End If
Case 19 'Light type
If State = False Then 'if State = False, button down
If Millis - BounceTime(1) < BounceDelay Then
Return
Else
LightON = Not(LightON) 'change the value of LightON
Log("Light: ", LightON) 'Log the LightOn value
BounceTime(1) = Millis
TimerLEDs.Enabled = LightON 'enable or disable TimerLEDs Timer

If LightON = False Then 'if LightOn = False, lights OFF
For i = 0 To NbLEDs - 1
pinLEDs(i).DigitalWrite(False) 'switch OFF all LEDs
Next
Else
For i = 0 To NbLEDs - 1
pinLEDs(i).DigitalWrite(AllLedsON) 'switch OFF or ON all LEDs
Next
'switch ON or OFF the current LED
pinLEDs(LEDIndex).DigitalWrite(Not(AllLedsON))
End If
End If
End If
End Select
End Sub
Private Sub TimerLEDs_Tick
pinLEDs(LEDIndex).DigitalWrite(AllLedsON) 'switch OFF or ON the current LED
'increment LedIndex by 1 and set it to 0 if LEDIndex = NbLEDs
LEDIndex = (LEDIndex + 1) Mod NbLEDs
pinLEDs(LEDIndex).DigitalWrite(Not(AllLedsON)) 'switch ON or OFF the current LED
End Sub


ESP32-WROOM-32





#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 600
#End Region
Sub Process_Globals
Public Serial1 As Serial

Public NbLEDs = 4 As Int 'Number of Leds
Public pinLEDs(4) As Pin 'Pins for the Leds
Public NbButtons = 2 As Int 'Number of buttons
Public pinButtons(2) As Pin 'Pins for the buttons

Public TimerLEDs As Timer
Public LightON = False As Boolean 'lights ON or OFF, ON = True
Public LEDIndex = 0 As Int 'index of current LED
Public AllLedsON = False As Boolean 'lighting type 
Private BounceTime(2) As ULong
Private BounceDelay = 10 As ULong
End Sub
Private Sub AppStart
Private i As Int

Serial1.Initialize(115200)
TimerLEDs.Initialize("TimerLEDs_Tick", 500)

'Initialize the buttons and add the StateChange event
'Pushbottom GPIO22, GPIO23
'LED out GPIO15 ,16 ,17 18  
' 
For i = 0 To NbButtons - 1
'Using the internal pull up resistor to prevent the pin from floating.
pinButtons(i).Initialize( i + 22 , pinButtons(i).MODE_INPUT_PULLUP)
pinButtons(i).AddListener("pinButtons_StateChanged")
Next

'Initialize the LEDs
'Digital pins 15 ,16 ,17 18
For i = 0 To NbLEDs - 1
pinLEDs(i).Initialize(i + 15, pinLEDs(i).MODE_OUTPUT)
Next
End Sub
Private Sub pinButtons_StateChanged (State As Boolean)
Private p As Pin

Log("State: ", State) 'Log the State value
p = Sender 'get the Pin which raised the event
Log("Pin Number: ", p.PinNumber) 'Log the Pin Number
Select p.PinNumber
Case 22 'Lights ON / OFF
If State = False Then 'if State = False, button down
If Millis - BounceTime(0) < BounceDelay Then
Return
Else
AllLedsON = Not(AllLedsON) 'change the state of AllLedsON
BounceTime(0) = Millis
If LightON = True Then 'if LightOn = False
For i = 0 To NbLEDs - 1
'switch OFF or ON all LEDs depending on AllLedsON
pinLEDs(i).DigitalWrite(AllLedsON) 
Next
'switch ON or OFF the current LED depending on AllLedsON
pinLEDs(LEDIndex).DigitalWrite(Not(AllLedsON))
End If
End If
End If
Case 23 'Light type
If State = False Then 'if State = False, button down
If Millis - BounceTime(1) < BounceDelay Then
Return
Else
LightON = Not(LightON) 'change the value of LightON
Log("Light: ", LightON) 'Log the LightOn value
BounceTime(1) = Millis
TimerLEDs.Enabled = LightON 'enable or disable TimerLEDs Timer

If LightON = False Then 'if LightOn = False, lights OFF
For i = 0 To NbLEDs - 1
pinLEDs(i).DigitalWrite(False) 'switch OFF all LEDs
Next
Else
For i = 0 To NbLEDs - 1
pinLEDs(i).DigitalWrite(AllLedsON) 'switch OFF or ON all LEDs
Next
'switch ON or OFF the current LED
pinLEDs(LEDIndex).DigitalWrite(Not(AllLedsON))
End If
End If
End If
End Select
End Sub
Private Sub TimerLEDs_Tick
pinLEDs(LEDIndex).DigitalWrite(AllLedsON) 'switch OFF or ON the current LED
'increment LedIndex by 1 and set it to 0 if LEDIndex = NbLEDs
LEDIndex = (LEDIndex + 1) Mod NbLEDs
pinLEDs(LEDIndex).DigitalWrite(Not(AllLedsON)) 'switch ON or OFF the current LED
End Sub


B4R LCD 量測脈波寬度 (Arduino UNO)

  B4R LCD 量測脈波寬度 (Arduino UNO)




#Region Project Notes
' pinButton -------------------A5 
'initialize the display(RS As Byte, RW As Byte, Enable As Byte
'RS pin > Arduino digital pin 12
'RW pin > 255 means mot used 
'Enable pin > Arduino digital pin 11 
'DataPins: Arduino digital pins 5, 4, 3, 2 
'DB7-----------------2
'DB6-----------------3
'DB5-----------------4
'DB4-----------------5
'-----------------------------------------
#End Region
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 600
#End Region
Sub Process_Globals
Public Serial1 As Serial
Public pinButton As Pin 'pin for the button
Public lcdDisplay As LiquidCrystal
Public TimeBegin, PulseWidth As ULong 'used to measure the time
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("APP Starting......")
'Using the internal pull up resistor to prevent the pin from floating.
pinButton.Initialize(pinButton.A5, pinButton.MODE_INPUT_PULLUP) 
pinButton.AddListener("pinButton_StateChanged")
'initialize the display(RS As Byte, RW As Byte, Enable As Byte
'RS pin > Arduino digital pin 12
'RW pin > 255 means mot used 
'Enable pin > Arduino digital pin 11 
'DataPins: Arduino digital pins 5, 4, 3, 2 
lcdDisplay.Initialize(12, 255, 11, Array As Byte(5, 4, 3, 2))
lcdDisplay.Begin(16, 2) 'set the display type 2 * 16 characters
lcdDisplay.Write("Pulse width [ms]") 'write "Pulse width" in the first line 
End Sub
Private Sub pinButton_StateChanged (State As Boolean)
Log("State: ", State) 'Log the State value
If State = False Then 'if State = False, button down
TimeBegin = Millis 'set TimeBegin, Millis = number of milliseconds since the last restart
Else
PulseWidth = Millis - TimeBegin 'calculate the numer of milliseconds since button down
lcdDisplay.SetCursor(0, 1) 'set the cursor at the begin of the second line 
lcdDisplay.Write("          ") 'clear the text
lcdDisplay.SetCursor(0, 1) 'set the cursor at the begin of the second line 
lcdDisplay.Write(PulseWidth) 'write the nubmer of milli seconds
Log("PulseWidth: ", PulseWidth)
End If
End Sub


B4R LCD 量測脈波寬度 (ESP32-WROOM-32)

 B4R LCD 量測脈波寬度



#Region Project Notes
'B4R HowTo Project: Pulse Width Meter (ESP32-WROOM-32)
'
'Connection (looking at the blue sensor from right to left)
 
 ' LCD 4bit mode RS--------------- GPIO13
 ' LCD 4bit mode EN--------------- GPIO12
 ' LCD 4bit mode D4--------------  GPIO14
 ' LCD 4bit mode D5--------------  GPIO27
 ' LCD 4bit mode D6--------------  GPIO26
 ' LCD 4bit mode D7--------------  GPIO25
'  
#End Region
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 600
#End Region
Sub Process_Globals
Public Serial1 As Serial
Public pinButton As Pin 'pin for the button
Public lcdDisplay As LiquidCrystal
Public TimeBegin, PulseWidth As ULong 'used to measure the time
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("APP Starting....")
'Using the internal pull up resistor to prevent the pin from floating.
pinButton.Initialize(23, pinButton.MODE_INPUT_PULLUP) 'Using the internal pull up resistor to prevent the pin from floating.
pinButton.AddListener("pinButton_StateChanged")

'initialize the display(RS As Byte, RW As Byte, Enable As Byte
'RS pin > Arduino digital pin 12
'RW pin > 255 means mot used
'Enable pin > Arduino digital pin 11
'DataPins: Arduino digital pins 5, 4, 3, 2
'lcdDisplay.Initialize(12, 255, 11, Array As Byte(5, 4, 3, 2))

lcdDisplay.Initialize(13, 255, 12, Array As Byte(14, 27, 26, 25))
lcdDisplay.Begin(16, 2) 'set the display type 2 * 16 characters
lcdDisplay.Write("Pulse width [ms]") 'write "Pulse width" in the first line 
End Sub
Private Sub pinButton_StateChanged (State As Boolean)
Log("State: ", State) 'Log the State value
If State = False Then 'if State = False, button down
TimeBegin = Millis 'set TimeBegin, Millis = number of milliseconds since the last restart
Else
PulseWidth = Millis - TimeBegin 'calculate the numer of milliseconds since button down
lcdDisplay.SetCursor(0, 1) 'set the cursor at the begin of the second line 
lcdDisplay.Write("          ") 'clear the text
lcdDisplay.SetCursor(0, 1) 'set the cursor at the begin of the second line 
lcdDisplay.Write(PulseWidth) 'write the nubmer of milli seconds
Log("State: ", PulseWidth)
End If
End Sub



B4R 4Bit 16x2 LCD 顯示器

 B4R 4Bit 16x2 LCD 顯示器










#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region
Sub Process_Globals
Public Serial1 As Serial

Public pinButton As Pin 'pin for the button
Public pinLEDGreen, pinLEDYellow, pinLEDRed As Pin 'pins for the Leds
Public lcdDisplay As LiquidCrystal
Public TimerGreenRed, TimerYellow As Timer
Public LightOn = False As Boolean
Public LightGreen = False As Boolean
Private BounceTime As ULong
Private BounceDelay = 10 As ULong
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
TimerGreenRed.Initialize("TimerGreenRed_Tick", 2000)
TimerYellow.Initialize("TimerYellow_Tick", 500)

pinButton.Initialize(pinButton.A5, pinButton.MODE_INPUT_PULLUP) 'Using the internal pull up resistor to prevent the pin from floating.
pinButton.AddListener("pinButton_StateChanged")
pinLEDGreen.Initialize(7, pinLEDGreen.MODE_OUTPUT)
pinLEDYellow.Initialize(8, pinLEDYellow.MODE_OUTPUT)
pinLEDRed.Initialize(9, pinLEDRed.MODE_OUTPUT)

'initialize the display(RS As Byte, RW As Byte, Enable As Byte
'RS pin > Arduino digital pin 12
'RW pin > 255 means mot used 
'Enable pin > Arduino digital pin 11 
'DataPins: Arduino digital pins 5, 4, 3, 2 
lcdDisplay.Initialize(12, 255, 11, Array As Byte(5, 4, 3, 2))
lcdDisplay.Begin(16, 2) 'set the display type 2 * 16 characters
lcdDisplay.Write("Light OFF") 'write "Light OFF" in the first line 
End Sub
Private Sub pinButton_StateChanged (State As Boolean)
Log("State: ", State) 'Log the State value
If State = False Then 'if State = False
If Millis - BounceTime < BounceDelay Then
Return
Else
pinLEDRed.DigitalWrite(True) 'switch ON the red LED
LightOn = Not(LightOn) 'change the value of LightOn
Log("Light: ", LightOn) 'Log the LightOn value
BounceTime = Millis
lcdDisplay.Clear 'clear the display
If LightOn = True Then
lcdDisplay.Write("Light ON") 'write "Light ON" in the first line 
lcdDisplay.SetCursor(0, 1) 'set the cursor at the begin of second line
lcdDisplay.Write("Red") 'write "red" in the second line
Else 'if State = True
lcdDisplay.Write("Light OFF") 'write "Light OFF" in the first line
End If
End If
End If
TimerGreenRed.Enabled = LightOn 'enable TimerGreenRed Timer

If LightOn = False Then 'if LightOn = False
pinLEDGreen.DigitalWrite(False) 'switch OFF LED Green
pinLEDYellow.DigitalWrite(False) 'switch OFF LED Yellow
pinLEDRed.DigitalWrite(False) 'switch OFF LED Red
End If
End Sub
Private Sub TimerGreenRed_Tick
If LightGreen = True Then 'if LightGreen = True
Log("TimerGreenRed_Tick LightYellow") 'write the Log
lcdDisplay.SetCursor(0, 1) 'position the cusor at the begin of second line
lcdDisplay.Write("yellow") 'write yellow on the display
TimerYellow.Enabled = True 'enable TimerYellow
pinLEDGreen.DigitalWrite(False) 'switch OFF LED Green
pinLEDYellow.DigitalWrite(True) 'switch ON LED Yellow
LightGreen = False 'set LightGreen to False
Else 'if LightGreen = False
Log("TimerGreenRed_Tick LightGreen")'write the Log
lcdDisplay.SetCursor(0, 1) 'set the cursor at the begin of second line
lcdDisplay.Write("green ") 'write "green " in the second line
pinLEDRed.DigitalWrite(False) 'switch OFF LED Red
pinLEDGreen.DigitalWrite(True) 'switch ON LED Green
LightGreen = True 'set LightGreen to True
End If
End Sub
Private Sub TimerYellow_Tick
Log("TimerYellow_Tick LightRed") 'write the Log
Log(" ")
lcdDisplay.SetCursor(0, 1) 'set the cursor at the begin of second line
lcdDisplay.Write("red   ") 'write "red   " in the second line
pinLEDYellow.DigitalWrite(False) 'switch OFF LED Yellow
pinLEDRed.DigitalWrite(True) 'switch ON LED Red
TimerYellow.Enabled = False 'disable Timer TimerYellow
End Sub

[ESP32-WROOM-32]





#Region Project Notes
'B4R HowTo Project: Light Dimmer (ESP32-WROOM-32)
'
'Connection (looking at the blue sensor from right to left)
 ' pinLEDGreen-------------------- GPIO19 
 ' pinLEDYellow------------------- GPIO18
 ' pinLEDRed---------------------- GPIO5
 
 ' LCD 4bit mode RS--------------- GPIO13
 ' LCD 4bit mode EN--------------- GPIO12
 ' LCD 4bit mode D4--------------  GPIO14
 ' LCD 4bit mode D5--------------  GPIO27
 ' LCD 4bit mode D6--------------  GPIO26
 ' LCD 4bit mode D7--------------  GPIO25
'  
#End Region
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 600
#End Region
Sub Process_Globals
Public Serial1 As Serial

Public pinButton As Pin 'pin for the button
Public pinLEDGreen, pinLEDYellow, pinLEDRed As Pin 'pins for the Leds
Public lcdDisplay As LiquidCrystal
Public TimerGreenRed, TimerYellow As Timer
Public LightOn = False As Boolean
Public LightGreen = False As Boolean
Private BounceTime As ULong
Private BounceDelay = 10 As ULong
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
TimerGreenRed.Initialize("TimerGreenRed_Tick", 2000)
TimerYellow.Initialize("TimerYellow_Tick", 500)

pinButton.Initialize(23, pinButton.MODE_INPUT_PULLUP) 'Using the internal pull up resistor to prevent the pin from floating.
pinButton.AddListener("pinButton_StateChanged")
pinLEDGreen.Initialize(19, pinLEDGreen.MODE_OUTPUT)
pinLEDYellow.Initialize(18, pinLEDYellow.MODE_OUTPUT)
pinLEDRed.Initialize(5, pinLEDRed.MODE_OUTPUT)

'initialize the display(RS As Byte, RW As Byte, Enable As Byte
'RS pin > Arduino digital pin 12
'RW pin > 255 means mot used 
'Enable pin > Arduino digital pin 11 
'DataPins: Arduino digital pins 5, 4, 3, 2 
'lcdDisplay.Initialize(12, 255, 11, Array As Byte(5, 4, 3, 2))

lcdDisplay.Initialize(13, 255, 12, Array As Byte(14, 27, 26, 25))

lcdDisplay.Begin(16, 2) 'set the display type 2 * 16 characters
lcdDisplay.Write("Light OFF") 'write "Light OFF" in the first line 
End Sub
Private Sub pinButton_StateChanged (State As Boolean)
Log("State: ", State) 'Log the State value
If State = False Then 'if State = False
If Millis - BounceTime < BounceDelay Then
Return
Else
pinLEDRed.DigitalWrite(True) 'switch ON the red LED
LightOn = Not(LightOn) 'change the value of LightOn
Log("Light: ", LightOn) 'Log the LightOn value
BounceTime = Millis
lcdDisplay.Clear 'clear the display
If LightOn = True Then
lcdDisplay.Write("Light ON") 'write "Light ON" in the first line 
lcdDisplay.SetCursor(0, 1) 'set the cursor at the begin of second line
lcdDisplay.Write("Red") 'write "red" in the second line
Else 'if State = True
lcdDisplay.Write("Light OFF") 'write "Light OFF" in the first line
End If
End If
End If
TimerGreenRed.Enabled = LightOn 'enable TimerGreenRed Timer

If LightOn = False Then 'if LightOn = False
pinLEDGreen.DigitalWrite(False) 'switch OFF LED Green
pinLEDYellow.DigitalWrite(False) 'switch OFF LED Yellow
pinLEDRed.DigitalWrite(False) 'switch OFF LED Red
End If
End Sub
Private Sub TimerGreenRed_Tick
If LightGreen = True Then 'if LightGreen = True
Log("TimerGreenRed_Tick LightYellow") 'write the Log
lcdDisplay.SetCursor(0, 1) 'position the cusor at the begin of second line
lcdDisplay.Write("yellow") 'write yellow on the display
TimerYellow.Enabled = True 'enable TimerYellow
pinLEDGreen.DigitalWrite(False) 'switch OFF LED Green
pinLEDYellow.DigitalWrite(True) 'switch ON LED Yellow
LightGreen = False 'set LightGreen to False
Else 'if LightGreen = False
Log("TimerGreenRed_Tick LightGreen")'write the Log
lcdDisplay.SetCursor(0, 1) 'set the cursor at the begin of second line
lcdDisplay.Write("green ") 'write "green " in the second line
pinLEDRed.DigitalWrite(False) 'switch OFF LED Red
pinLEDGreen.DigitalWrite(True) 'switch ON LED Green
LightGreen = True 'set LightGreen to True
End If
End Sub
Private Sub TimerYellow_Tick
Log("TimerYellow_Tick LightRed") 'write the Log
Log(" ")
lcdDisplay.SetCursor(0, 1) 'set the cursor at the begin of second line
lcdDisplay.Write("red   ") 'write "red   " in the second line
pinLEDYellow.DigitalWrite(False) 'switch OFF LED Yellow
pinLEDRed.DigitalWrite(True) 'switch ON LED Red
TimerYellow.Enabled = False 'disable Timer TimerYellow
End Sub


2021年11月20日 星期六

B4X 利用PulsePeriodModulation 控制直流馬達 轉速

 B4X 利用PulsePeriodModulation 控制直流馬達 轉速

(Arduino Uno)







#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region


Sub Process_Globals
Public Serial1 As Serial
Private MotorON = False As Boolean 'motor ON or OFF, ON = True
Private MotorDirection = False As Boolean 'motor direction 
Private MotorSpeed = 0 As UInt

Private pinSwitchMotorONOFF, pinSwitchMotorDirection, pinMotorSpeed, pinMotorControl1, pinMotorControl2, pinPot As Pin

Private BounceTimeMotorON, BounceTimeMotorDirection As ULong
Private BounceDelay = 10 As ULong
Private MaxPulsePeriod = 500 As ULong 'Max PulsePeriod value 
Private PulseWidth = 20 As ULong 'remains constant
Private PulsePeriod As ULong 'calculated in NextPulse
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")

pinSwitchMotorONOFF.Initialize(pinSwitchMotorONOFF.A5, pinSwitchMotorONOFF.MODE_INPUT_PULLUP)
pinSwitchMotorONOFF.AddListener("pinSwitchMotorONOFF_StateChanged")
pinSwitchMotorDirection.Initialize(pinSwitchMotorONOFF.A3, pinSwitchMotorDirection.MODE_INPUT_PULLUP)
pinSwitchMotorDirection.AddListener("pinSwitchMotorDirection_StateChanged")
pinMotorControl1.Initialize(2, pinMotorControl1.MODE_OUTPUT)
pinMotorControl2.Initialize(3, pinMotorControl1.MODE_OUTPUT)
pinMotorSpeed.Initialize(10, pinMotorSpeed.MODE_OUTPUT)

pinPot.Initialize(pinPot.A1, pinPot.MODE_INPUT)
pinMotorControl1.DigitalWrite(MotorDirection)
pinMotorControl2.DigitalWrite(Not(MotorDirection))

End Sub


Private Sub pinSwitchMotorONOFF_StateChanged (State As Boolean)
If State = False Then
If Millis - BounceTimeMotorON < BounceDelay Then
Return
Else
MotorON = Not(MotorON)
If MotorON = True Then
NextPulse(0)
Else
pinMotorSpeed.AnalogWrite(0)
End If
BounceTimeMotorON = Millis
End If
Log("MotorON: ", MotorON)
End If
End Sub


Private Sub pinSwitchMotorDirection_StateChanged (State As Boolean)
' Log("StateON ", State)
If State = False Then
If Millis - BounceTimeMotorDirection < BounceDelay Then
Return
Else
MotorDirection = Not(MotorDirection)
pinMotorControl1.DigitalWrite(MotorDirection)
pinMotorControl2.DigitalWrite(Not(MotorDirection))
BounceTimeMotorDirection = Millis
End If
Log("MotorDirection: ", MotorDirection)
End If
End Sub


Private Sub NextPulse(Tag As Byte)
If MotorON = True Then
PulsePeriod = MapRange(pinPot.AnalogRead, 0, 1023, PulseWidth, MaxPulsePeriod)
Log(PulsePeriod)
If PulsePeriod >= MaxPulsePeriod - 1 Then
pinMotorSpeed.DigitalWrite(False)
Else If PulsePeriod <= PulseWidth + 1 Then
pinMotorSpeed.DigitalWrite(True)
Else
pinMotorSpeed.DigitalWrite(True)
CallSubPlus("EndPulse", PulseWidth, 0)
End If
CallSubPlus("EndPulse", PulseWidth, 0)
CallSubPlus("NextPulse", PulsePeriod, 0)
' Log("NextPulse") 'Log for testing
End If
End Sub


Private Sub EndPulse(Tag As Byte)
pinMotorSpeed.DigitalWrite(False) 'switch the LED OFF 
' Log("EndPulse") 'Log for testing
End Sub


(ESP32-WROOM-32)





#Region Project Notes
'B4R HowTo Project: Light Dimmer (ESP32-WROOM-32)
'
'Connection (looking at the blue sensor from right to left)
' pushbutton switch --------------GPIO23
' pushbutton switch --------------GPIO22
' L293D pin15 control1   ---------GPIO12
' L293D pin10 control2   ---------GPIO14
' L293D pin9 PWM control ---------GPIO27
' potentiometer-------------------GPIO13 
'  
#End Region
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 600
#End Region
Sub Process_Globals
Public Serial1 As Serial
Private MotorON = False As Boolean 'motor ON or OFF, ON = True
Private MotorDirection = False As Boolean 'motor direction 
Private MotorSpeed = 0 As UInt

Private pinSwitchMotorONOFF, pinSwitchMotorDirection, pinMotorSpeed, pinMotorControl1, pinMotorControl2, pinPot As Pin

Private BounceTimeMotorON, BounceTimeMotorDirection As ULong
Private BounceDelay = 10 As ULong
Private MaxPulsePeriod = 500 As ULong 'Max PulsePeriod value 
Private PulseWidth = 20 As ULong 'remains constant
Private PulsePeriod As ULong 'calculated in NextPulse
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")

pinSwitchMotorONOFF.Initialize(23, pinSwitchMotorONOFF.MODE_INPUT_PULLUP)
pinSwitchMotorONOFF.AddListener("pinSwitchMotorONOFF_StateChanged")
pinSwitchMotorDirection.Initialize(22, pinSwitchMotorDirection.MODE_INPUT_PULLUP)
pinSwitchMotorDirection.AddListener("pinSwitchMotorDirection_StateChanged")
pinMotorControl1.Initialize(12, pinMotorControl1.MODE_OUTPUT)
pinMotorControl2.Initialize(14, pinMotorControl1.MODE_OUTPUT)
pinMotorSpeed.Initialize(27, pinMotorSpeed.MODE_OUTPUT)

pinPot.Initialize(13, pinPot.MODE_INPUT)
pinMotorControl1.DigitalWrite(MotorDirection)
pinMotorControl2.DigitalWrite(Not(MotorDirection))

End Sub
Private Sub pinSwitchMotorONOFF_StateChanged (State As Boolean)
If State = False Then
If Millis - BounceTimeMotorON < BounceDelay Then
Return
Else
MotorON = Not(MotorON)
If MotorON = True Then
NextPulse(0)
Else
pinMotorSpeed.AnalogWrite(0)
End If
BounceTimeMotorON = Millis
End If
Log("MotorON: ", MotorON)
End If
End Sub
Private Sub pinSwitchMotorDirection_StateChanged (State As Boolean)
' Log("StateON ", State)
If State = False Then
If Millis - BounceTimeMotorDirection < BounceDelay Then
Return
Else
MotorDirection = Not(MotorDirection)
pinMotorControl1.DigitalWrite(MotorDirection)
pinMotorControl2.DigitalWrite(Not(MotorDirection))
BounceTimeMotorDirection = Millis
End If
Log("MotorDirection: ", MotorDirection)
End If
End Sub
Private Sub NextPulse(Tag As Byte)
If MotorON = True Then
PulsePeriod = MapRange(pinPot.AnalogRead, 0, 1023, PulseWidth, MaxPulsePeriod)
Log(PulsePeriod)
If PulsePeriod >= MaxPulsePeriod - 1 Then
pinMotorSpeed.DigitalWrite(False)
Else If PulsePeriod <= PulseWidth + 1 Then
pinMotorSpeed.DigitalWrite(True)
Else
pinMotorSpeed.DigitalWrite(True)
CallSubPlus("EndPulse", PulseWidth, 0)
End If
CallSubPlus("EndPulse", PulseWidth, 0)
CallSubPlus("NextPulse", PulsePeriod, 0)
' Log("NextPulse") 'Log for testing
End If
End Sub
Private Sub EndPulse(Tag As Byte)
pinMotorSpeed.DigitalWrite(False) 'switch the LED OFF 
' Log("EndPulse") 'Log for testing
End Sub


  清除項目編譯快取 執行 flutter clean 重新取得套件依賴 執行 flutter pub get 完整重新編譯並執行 flutter run 1. 新增專案套件 (pubspec.yaml) 在 Flutter 專案根目錄下的 pubspec.yaml 中,新增 ...