Skip to content
Snippets Groups Projects
Commit c1afbb01 authored by Evan Jones's avatar Evan Jones
Browse files

Upload New File

parent 6d6c37c8
No related branches found
No related tags found
No related merge requests found
import grovepi
import time
# Pin definitions
PIR_PIN = 2
ULTRASONIC_PIN = 4
LIGHT_SENSOR_PIN = 1 # Analog port A1
BUTTON_PIN = 3
# Initialise all sensors at startup
def Initialise_Sensors():
grovepi.pinMode(PIR_PIN, "INPUT")
grovepi.pinMode(BUTTON_PIN, "INPUT")
print("Sensors initialized")
# Read motion sensor
def Read_Motion():
try:
motion = grovepi.digitalRead(PIR_PIN)
print(f"Motion Sensor Reading: {motion}")
return motion
except Exception as e:
print(f"Motion Sensor Error: {e}")
return None
# Read distance from ultrasonic ranger
def Read_Distance():
try:
distance = grovepi.ultrasonicRead(ULTRASONIC_PIN)
print(f"Ultrasonic Distance: {distance} cm")
return distance
except Exception as e:
print(f"Ultrasonic Ranger Error: {e}")
return None
# Read light level
def Read_Light_Level():
try:
light_level = grovepi.analogRead(LIGHT_SENSOR_PIN)
print(f"Light Sensor Reading: {light_level}")
return light_level
except Exception as e:
print(f"Light Sensor Error: {e}")
return None
# Handle button press
def Handle_Button():
try:
button_state = grovepi.digitalRead(BUTTON_PIN)
print(f"Button State: {button_state}")
return button_state
except Exception as e:
print(f"Button Error: {e}")
return None
# Test functions individually
if __name__ == "__main__":
Initialise_Sensors()
# Run each sensor test 5 times
for i in range(5):
print(f"\nTest Iteration: {i+1}")
Read_Motion()
Read_Distance()
Read_Light_Level()
Handle_Button()
time.sleep(1)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment