Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import RPi.GPIO as GPIO
- from hx711 import HX711
- import smbus
- import time
- import os
- import threading
- def Load_Cell(bus):
- GPIO.setmode(GPIO.BCM)
- hx = HX711(5, 6)
- hx.set_reading_format("MSB", "MSB")
- hx.set_reference_unit(-2199) # Set your reference unit value here (e.g., for grams)
- readings = 0
- hx.reset()
- hx.tare()
- folder_path = "./DATA"
- if not os.path.exists(folder_path):
- os.makedirs(folder_path)
- file_name = os.path.join(folder_path, "LC.txt")
- with open(file_name, "w") as file:
- start_time = time.monotonic()
- while True:
- weight = hx.get_weight(1)
- timestamp = time.monotonic() - start_time
- timestamp = round(timestamp, 5)
- if weight > 0:
- file.write(f"Weight: {weight:.2f} g\t{timestamp:.3f}\n") # Limit decimal precision as needed
- file.flush()
- time.sleep(0.001) # Reduced sleep time to improve data acquisition
- def Time_of_Flight(bus):
- bus.write_byte_data(0x29, 0x80, 0x01)
- # ... (rest of the ToF sensor code)
- # Use the provided bus parameter instead of creating a new one
- def MPU_6050(bus):
- bus.write_byte_data(0x68, 0x6b, 0)
- # ... (rest of the MPU6050 sensor code)
- # Use the provided bus parameter instead of creating a new one
- if __name__ == "__main__":
- try:
- bus = smbus.SMBus(1) # Use I2C bus 1
- bus.set_clock(10000)
- # Creating and starting the threads
- thread1 = threading.Thread(target=Load_Cell, args=(bus,))
- thread2 = threading.Thread(target=Time_of_Flight, args=(bus,))
- thread3 = threading.Thread(target=MPU_6050, args=(bus,))
- thread1.start()
- thread2.start()
- thread3.start()
- # Wait for threads to finish (this won't be reached unless threads are stopped)
- thread1.join()
- thread2.join()
- thread3.join()
- except KeyboardInterrupt:
- print("Exiting...")
- GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement