Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using a list of variables
#3
#!/usr/bin/env python3
########################################################################
# Filename    : UltrasonicRanging.py
# Description : Get distance via UltrasonicRanging sensor
# auther      : www.freenove.com
# modification: 2019/12/28
########################################################################
import RPi.GPIO as GPIO
import time

trigPin = 20
echoPin = 19
MAX_DISTANCE = 220          # define the maximum measuring distance, unit: cm
timeOut = MAX_DISTANCE*60   # calculate timeout according to the maximum measuring distance
GPIO.setwarnings(False)  # prevents some errors
def pulseIn(pin,level,timeOut): # obtain pulse time of a pin under timeOut                                                
    t0 = time.time()
    while(GPIO.input(pin) != level):
        if((time.time() - t0) > timeOut*0.000001):
            return 0;
    t0 = time.time()
    while(GPIO.input(pin) == level):
        if((time.time() - t0) > timeOut*0.000001):
            return 0;
    pulseTime = (time.time() - t0)*1000000
    return pulseTime

def getSonar():     # get the measurement results of ultrasonic module,with unit: cm                                                  
    GPIO.output(trigPin,GPIO.HIGH)      # make trigPin output 10us HIGH level
    time.sleep(0.00001)     # 10us
    GPIO.output(trigPin,GPIO.LOW) # make trigPin output LOW level
    pingTime = pulseIn(echoPin,GPIO.HIGH,timeOut)   # read plus time of echoPin                                          
    distance = pingTime * 340.0 / 2.0 / 10000.0     # calculate distance with sound speed 340m/s
    return distance

def setup():   #                                                                                                   
    GPIO.setmode(GPIO.BCM)      # use PHYSICAL GPIO Numbering
    GPIO.setup(trigPin, GPIO.OUT)   # set trigPin to OUTPUT mode
    GPIO.setup(echoPin, GPIO.IN)    # set echoPin to INPUT mode


def multi_signal_cleanup (trig, echo):  # averages 10 signals from ultrasound

    samples = [s1, s2, s3]
    for variable in samples:
        variable = getSonar()

    sample_ave = (s1+s2+s3)/3
    return sample_ave

def loop():    #                                                                                            
    while(True):
        distance = multi_signal_cleanup(20, 19) # get distance                                                                                            *3
        print ("The distance is : %.2f cm"%(distance))
        time.sleep(1)

if __name__ == '__main__':     # Program entrance
    print ('Program is starting...')
    setup()          #                                                                                                  
    try:
        loop()   #                                                                                              
    except KeyboardInterrupt:  # Press ctrl-c to end the program.
        GPIO.cleanup()         # release GPIO resource
This is a test code I am working on and not the whole robot code, but it is where I am getting the error.

thanks for the help.
Gary
Reply


Messages In This Thread
Using a list of variables - by Zane217 - Jun-09-2020, 12:06 PM
RE: Using a list of variables - by DreamingInsanity - Jun-09-2020, 12:09 PM
RE: Using a list of variables - by Zane217 - Jun-09-2020, 12:20 PM
RE: Using a list of variables - by Yoriz - Jun-09-2020, 12:35 PM
RE: Using a list of variables - by Zane217 - Jun-09-2020, 01:27 PM
RE: Using a list of variables - by pyzyx3qwerty - Jun-09-2020, 01:32 PM
RE: Using a list of variables - by Yoriz - Jun-09-2020, 01:37 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Advancing Through Variables In A List knight2000 0 564 May-13-2023, 03:30 AM
Last Post: knight2000
  Converting list to variables Palves 1 1,834 Sep-18-2020, 05:43 PM
Last Post: stullis
  Print variable values from a list of variables xnightwingx 3 2,729 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  add all variables to a list faszination_92 6 3,248 Apr-14-2020, 04:36 AM
Last Post: buran
  Creating a List with many variables in a simple way donnertrud 1 2,110 Jan-11-2020, 03:00 PM
Last Post: Clunk_Head
  2D Array/List OR using variables in other variable names? IAMK 4 3,976 Apr-16-2018, 09:09 PM
Last Post: IAMK
  list vs variables mcmxl22 2 3,220 Jan-27-2018, 10:00 AM
Last Post: Gribouillis
  list of user's variables in the interpreter nzcan 5 4,018 Jan-21-2018, 11:02 AM
Last Post: nzcan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020