1

This program is pretty simple. Whenever a user waves both hands across 2 PIR sensors, it should print out a message in console that displays which GPIO ports has being activated. The raspberry pi uses GPIO ports 4 and 17 to read data from the sensor.

I receive this error message whenever I try to print out displays the GPIO ports that has being activated.

enter image description here This is the error message.

Traceback (most recent call last):
  File "peter.py", line 27, in <module>
    print("GPIO pin %s is %s" % (sensor, new_state, sensor2, new_state2))
TypeError: not all arguments converted during string formatting

This is the code.

import RPi.GPIO as GPIO
import time

sensor = 4
sensor2 = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)
GPIO.setup(sensor2, GPIO.IN, GPIO.PUD_DOWN)

previous_state = False
current_state = False

previous_state2 = False
current_state2 = False

while True:
    time.sleep(0.1)
    previous_state = current_state
    previous_state2 = current_state2
    current_state = GPIO.input(sensor)
    current_state2 = GPIO.input(sensor2)
    if current_state2==True and current_state != previous_state2 and previous_state                                                                                    ate==False:

        new_state = "HIGH" if current_state else "LOW"
        new_state2 = "HIGH" if current_state2 else "LOW"

        print("GPIO pin %s is %s" % (sensor, new_state, sensor2, new_state2))
  • the first thing I notice is this line seems incomplete if current_state2==True and current_state != previous_state2 and previous_st – Steve Robillard May 26 '16 at 0:14
  • 3
    Your also passing 4 arguments to a print statement that expects only 2 – Steve Robillard May 26 '16 at 0:36
  • @SteveRobillard So I should remove sensor and sensor2? – peter pan May 28 '16 at 7:13
  • He meant your format string has only two %s. Add more %s or do two prints. One for sensor and one for sensor2. – PaulF8080 May 28 '16 at 15:06
0

The reason for the error is you have two format specifiers %s but are passing 4 values (sensor, new_state, sensor2, new_state2).

Change the last line form this:

print("GPIO pin %s is %s" % (sensor, new_state, sensor2, new_state2))

to this:

print("GPIO pin %s is %s" % (sensor, new_state))
print("GPIO pin %s is %s" % (sensor2, new_state2))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.