2

I am trying to retrive my data from a sensor through BeagleBone Black. I got reading and volts, but since I allready use time for time.sleep(). time.sleep() gets syntax error when I try to incorporate start_time.

start_time = time.time()
seconds = (time.time() -start_time)
print('%f\t%f\%s' % (reading, volts, seconds)

This is the code block I try to write.

import Adafruit_BBIO.ADC as ADC
import time

sensor_pin = 'P9_40'

    ADC.setup()
    
    print('Reading\t\tVolts')
    
    while True:
        reading = ADC.read(sensor_pin)
        volts = reading * 1.800
        print('%f\t%f' % (reading, volts)
        time.sleep(0.5)

Why do I get syntax error and is there a better way to add time in seconds? Syntax Error:

File "pulse.py", line 15
    time.sleep(0.5)
       ^
SyntaxError: invalid syntax

2 Answers2

3

Your issue here is that you haven't closed the parentheses () on the line above the time.sleep.

It should read as below

print('%f\t%f' % (reading, volts))

You have the same issue in the print statement you used when calculating the time difference

HPringles
  • 1,042
  • 6
  • 15
  • This is considered **old style formatting**: https://realpython.com/python-string-formatting/#1-old-style-string-formatting-operator – Diego Torres Milano Dec 02 '21 at 19:57
  • It is, yes. However, the post was asking for the reasons behind the syntax error, not about the semantics of the code – HPringles Dec 04 '21 at 15:19
2

You can use an f-string literal

print(f'{reading} {volts} {seconds}')
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134