Need Help putting code in a loop

This is the place for queries that don't fit in any of the other categories.

Need Help putting code in a loop

Postby Payney » Mon Apr 14, 2014 2:50 pm

so this is the thing.
I have just completed a code that combines the ultrasonic sensor and a LCD screen, where the readings from the ultrasonic can be viewed on the LCD screen.
although i am having difficulties in putting the code in a loop so the LCD continuously show the ultrasonic readings when there are changes.

I understand how while loops work as i have done them in the past, but for some reason every time i try to put one into this code, python doesn't seem to like it.
Please help

p.s i have clearly outlined in the code where the ultrasonic readings are shown on the LCD

Code: Select all
# test3.py
# Measure distance using an ultrasonic module
# in a loop.
#
# Author : Jack Payne
# Date   : /12/2013

# -----------------------
# Python libraries
# -----------------------
import time
import RPi.GPIO as GPIO
import numpy as np
# -----------------------
# Define functions
# -----------------------

def measure():
  # This function measures a distance

  GPIO.output(GPIO_TRIGGER, True)
  time.sleep(0.00001)
  GPIO.output(GPIO_TRIGGER, False)
  start = time.time()
 
  while GPIO.input(GPIO_ECHO)==0:
    start = time.time()

  while GPIO.input(GPIO_ECHO)==1:
    stop = time.time()

  elapsed = stop-start
  distance = (elapsed * 34300)/2

  return distance

def measure_average():
  # This function takes 3 measurements and
  # returns the average.

  distance1=measure()
  time.sleep(0.1)
  distance2=measure()
  time.sleep(0.1)
  distance3=measure()
  distance = distance1 + distance2 + distance3
  distance = distance / 3

  return distance

def simple_moveing_average(values,window):
  # This function takes 3 values from measure_average
  # adding them together and dividing by three. everytime
  # a new value is take, the sequence shifts one place.
 
  weights = np.repeat(1.0,window)/window
  smas = np.convolve(values,weights,'valid')
 
  return smas
 
# -----------------------
# Main Script
# -----------------------

# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)

# Define GPIO to use on Pi
GPIO_TRIGGER = 22
GPIO_ECHO    = 27

# Set pins as output and input
GPIO.setup(GPIO_TRIGGER,GPIO.OUT)  # Trigger
GPIO.setup(GPIO_ECHO,GPIO.IN)      # Echo

# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER, False)

# Wrap main content in a try block so we can
# catch the user pressing CTRL-C and run the
# GPIO cleanup function. This will also prevent
# the user seeing lots of unnecessary error
# messages.

final_measurement = measure_average ()
line1 = (": %.lf") % final_measurement
time.sleep(1)

# Define GPIO to LCD mapping
LCD_RS = 7
LCD_E  = 8
LCD_D4 = 25
LCD_D5 = 24
LCD_D6 = 23
LCD_D7 = 18
LED_ON = 15

# Define some device constants
LCD_WIDTH = 16    # Maximum characters per line
LCD_CHR = True
LCD_CMD = False

LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line

# Timing constants
E_PULSE = 0.00005
E_DELAY = 0.00005


def main():
  # Main program block

  GPIO.setmode(GPIO.BCM)       # Use BCM GPIO numbers
  GPIO.setup(LCD_E, GPIO.OUT)  # E
  GPIO.setup(LCD_RS, GPIO.OUT) # RS
  GPIO.setup(LCD_D4, GPIO.OUT) # DB4
  GPIO.setup(LCD_D5, GPIO.OUT) # DB5
  GPIO.setup(LCD_D6, GPIO.OUT) # DB6
  GPIO.setup(LCD_D7, GPIO.OUT) # DB7
  GPIO.setup(LED_ON, GPIO.OUT) # Backlight enable

  # Initialise display
  lcd_init()

  # Toggle backlight on-off-on
  GPIO.output(LED_ON, True)
  time.sleep(1)
  GPIO.output(LED_ON, False)
  time.sleep(1)
  GPIO.output(LED_ON, True)
  time.sleep(1)

  # Send some centred test
  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("Please wait....",2)
  time.sleep(3) # 3 second delay

 
  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("Preparing",2)
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string("Readings......",2)
 
  time.sleep(2) # 3 second delay

#=========================================================================
# BELOW IS THE ULTRASONIC OUTPUT ON THR LCD

  # Send some right justified text
  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("Liquid Level",2)
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string(line1,2)
  time.sleep(5)
#=======================================================================               
  # Turn off backlight
  GPIO.output(LED_ON, False)
       


def lcd_init():
  # Initialise display
  lcd_byte(0x33,LCD_CMD)
  lcd_byte(0x32,LCD_CMD)
  lcd_byte(0x28,LCD_CMD)
  lcd_byte(0x0C,LCD_CMD) 
  lcd_byte(0x06,LCD_CMD)
  lcd_byte(0x01,LCD_CMD) 

def lcd_string(message,style):
  # Send string to display
  # style=1 Left justified
  # style=2 Centred
  # style=3 Right justified

  if style==1:
    message = message.ljust(LCD_WIDTH," ") 
  elif style==2:
    message = message.center(LCD_WIDTH," ")
  elif style==3:
    message = message.rjust(LCD_WIDTH," ")

  for i in range(LCD_WIDTH):
    lcd_byte(ord(message[i]),LCD_CHR)

def lcd_byte(bits, mode):
  # Send byte to data pins
  # bits = data
  # mode = True  for character
  #        False for command

  GPIO.output(LCD_RS, mode) # RS

  # High bits
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x10==0x10:
    GPIO.output(LCD_D4, True)
  if bits&0x20==0x20:
    GPIO.output(LCD_D5, True)
  if bits&0x40==0x40:
    GPIO.output(LCD_D6, True)
  if bits&0x80==0x80:
    GPIO.output(LCD_D7, True)

  # Toggle 'Enable' pin
  time.sleep(E_DELAY)   
  GPIO.output(LCD_E, True) 
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False) 
  time.sleep(E_DELAY)     

  # Low bits
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x01==0x01:
    GPIO.output(LCD_D4, True)
  if bits&0x02==0x02:
    GPIO.output(LCD_D5, True)
  if bits&0x04==0x04:
    GPIO.output(LCD_D6, True)
  if bits&0x08==0x08:
    GPIO.output(LCD_D7, True)

  # Toggle 'Enable' pin
  time.sleep(E_DELAY)   
  GPIO.output(LCD_E, True) 
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False) 
  time.sleep(E_DELAY)   
   
if __name__ == '__main__':
  main()
 
Payney
 
Posts: 1
Joined: Mon Apr 14, 2014 2:35 pm

Re: Need Help putting code in a loop

Postby metulburr » Mon Apr 14, 2014 3:03 pm

python doesn't seem to like it.

what is the full traceback that you get?
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1562
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests