From dffac60962d49a12fec35e5d586d391d3b2ddca5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ebbe=20Ba=C3=9F?= Date: Tue, 12 Jul 2022 11:03:02 +0200 Subject: [PATCH] test with original code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ebbe Baß --- test.py | 75 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 27 deletions(-) diff --git a/test.py b/test.py index 578e7d7..dcf57e5 100644 --- a/test.py +++ b/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()) \ No newline at end of file +# 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) \ No newline at end of file