parent
77b788c048
commit
dffac60962
75
test.py
75
test.py
|
@ -1,31 +1,52 @@
|
|||
global GPIO
|
||||
import RPi.GPIO as GPIO
|
||||
import time
|
||||
import time, signal, sys
|
||||
sys.path.append('./SDL_Adafruit_ADS1x15')
|
||||
|
||||
GPIO.setwarnings(False)
|
||||
GPIO.setmode(GPIO.BOARD)
|
||||
import SDL_Adafruit_ADS1x15
|
||||
|
||||
GPIO_TRIGGER = 18
|
||||
GPIO_ECHO = 24
|
||||
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
|
||||
GPIO.setup(GPIO_ECHO, GPIO.IN)
|
||||
def signal_handler(signal, frame):
|
||||
print( 'You pressed Ctrl+C!')
|
||||
sys.exit(0)
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
|
||||
def get_water_level():
|
||||
GPIO.output(GPIO_TRIGGER, True)
|
||||
time.sleep(0.00001)
|
||||
GPIO.output(GPIO_TRIGGER, False)
|
||||
|
||||
startTime = time.time()
|
||||
StopTime = time.time()
|
||||
|
||||
while GPIO.input(GPIO_ECHO) == 0:
|
||||
startTime = time.time()
|
||||
while GPIO.input(GPIO_ECHO) == 1:
|
||||
StopTime = time.time()
|
||||
|
||||
TimeElapsed = StopTime - startTime
|
||||
distance = (TimeElapsed * 34300) / 2
|
||||
|
||||
return int(distance)
|
||||
ADS1115 = 0x01 # 16-bit ADC
|
||||
|
||||
print(get_water_level())
|
||||
# Select the gain
|
||||
# gain = 6144 # +/- 6.144V
|
||||
gain = 4096 # +/- 4.096V
|
||||
# gain = 2048 # +/- 2.048V
|
||||
# gain = 1024 # +/- 1.024V
|
||||
# gain = 512 # +/- 0.512V
|
||||
# gain = 256 # +/- 0.256V
|
||||
|
||||
# Select the sample rate
|
||||
# sps = 8 # 8 samples per second
|
||||
# sps = 16 # 16 samples per second
|
||||
# sps = 32 # 32 samples per second
|
||||
# sps = 64 # 64 samples per second
|
||||
# sps = 128 # 128 samples per second
|
||||
sps = 250 # 250 samples per second
|
||||
# sps = 475 # 475 samples per second
|
||||
# sps = 860 # 860 samples per second
|
||||
|
||||
# Initialise the ADC using the default mode (use default I2C address)
|
||||
adc = SDL_Adafruit_ADS1x15.ADS1x15(ic=ADS1115)
|
||||
while (1):
|
||||
|
||||
# Read channels in single-ended mode using the settings above
|
||||
|
||||
print ("--------------------")
|
||||
voltsCh0 = adc.readADCSingleEnded(0, gain, sps) / 1000
|
||||
rawCh0 = adc.readRaw(0, gain, sps)
|
||||
print ("Channel 0 =%.6fV raw=0x%4X dec=%d" % (voltsCh0, rawCh0, rawCh0))
|
||||
voltsCh1 = adc.readADCSingleEnded(1, gain, sps) / 1000
|
||||
rawCh1 = adc.readRaw(1, gain, sps)
|
||||
print ("Channel 1 =%.6fV raw=0x%4X dec=%d" % (voltsCh1, rawCh1, rawCh1))
|
||||
voltsCh2 = adc.readADCSingleEnded(2, gain, sps) / 1000
|
||||
rawCh2 = adc.readRaw(2, gain, sps)
|
||||
print ("Channel 2 =%.6fV raw=0x%4X dec=%d" % (voltsCh2, rawCh2, rawCh2))
|
||||
voltsCh3 = adc.readADCSingleEnded(3, gain, sps) / 1000
|
||||
rawCh3 = adc.readRaw(3, gain, sps)
|
||||
print ("Channel 3 =%.6fV raw=0x%4X dec=%d" % (voltsCh3, rawCh3, rawCh3))
|
||||
print ("--------------------")
|
||||
|
||||
time.sleep(0.5)
|
Loading…
Reference in New Issue