B4J Question Sensors

Ilya G.

Active Member
Licensed User
Longtime User
Please help with HC-SR505 PIR detection sensor

Arduino IDE code:
B4X:
int led = 13 ;// назначение пина для светодиода
int pirPin = 17; // назначение пина для мини ИК датчика
int value ;// переменная для хранения положения датчика
void setup ()
{
  Serial.begin(9600);
  pinMode (led, OUTPUT) ;// пин светодиода работает как выход
  pinMode (pirPin, INPUT) ; // пин датчика работает как вход
}
void loop ()
{
  value = digitalRead (pirPin) ;// чтение значения с датчика
  if (value == HIGH) // когда с ИК сенсора появляется высокий уровень, светодиод загорается
  {
    digitalWrite (led, HIGH);
    Serial.println("movement");
  }
  else
  {
    digitalWrite (led, LOW);
    Serial.println("no movement");
  }
  delay(1000);
}

B4J code doesn't work on RPi 3:
B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Private controller As GpioController
    Private Pin17 As GpioPinDigitalInput
    Private Timer1 As Timer
End Sub

Sub AppStart (Args() As String)
    controller.Initialize
    Pin17.Initialize("Pin17", 17)
    Pin17.SetPinPullResistance("PULL_DOWN")
   
    Timer1.Initialize("Timer1", 3000)
    Timer1.Enabled = True
    StartMessageLoop
End Sub

Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Timer1_Tick
    Log(Pin17.State)
End Sub
 
Upvote 0

Ilya G.

Active Member
Licensed User
Longtime User
Still not working :(
B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Dim controller As GpioController
    Dim Pin17 As GpioPinDigitalInput
    Dim Timer1 As Timer
End Sub

Sub AppStart (Args() As String)
    controller.Initialize
    Pin17.Initialize("Pin17", 17)
    Pin17.SetPinPullResistance("PULL_DOWN")
  
    Timer1.Initialize("Timer1", 3000)
'    Timer1.Enabled = True
    StartMessageLoop
End Sub

Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Timer1_Tick
    Log(Pin17.State)
End Sub

Sub Pin17_StateChange
    Log(Pin17.State)
End Sub
 
Upvote 0

Ilya G.

Active Member
Licensed User
Longtime User
Python code work well
B4X:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO_PIR = 17
print "PIR Module Test (CTRL-C to exit)"
# Set pin as input
GPIO.setup(GPIO_PIR,GPIO.IN)      # Echo
Current_State  = 0
Previous_State = 0
try:
  print "Waiting for PIR to settle ..."
  # Loop until PIR output is 0
  while GPIO.input(GPIO_PIR)==1:
    Current_State  = 0
  print "  Ready"
  # Loop until users quits with CTRL-C
  while True :
    # Read PIR state
    Current_State = GPIO.input(GPIO_PIR)
    if Current_State==1 and Previous_State==0:
      # PIR is triggered
      print "  Motion detected!"
      # Record previous state
      Previous_State=1
    elif Current_State==0 and Previous_State==1:
      # PIR has returned to ready state
      print "  Ready"
      Previous_State=0
    # Wait for 10 milliseconds
    time.sleep(0.01)
except KeyboardInterrupt:
  print "  Quit"
  # Reset GPIO settings
  GPIO.cleanup()
 
Upvote 0
Top