Reputation: 49
I’m currently using a pivot pi to rotate a servo by clicking either the up or down arrow key but for some reason I have to click the up arrow key three times for it to move (same with the down arrow key) and I don’t know why, I just want to make it so that I have to just click it once for the servo to move.
from pivotpi import *
from time import sleep
import curses
stdscr = curses.initscr()
curses.cbreak()
stdscr.keypad(1)
stdscr.refresh()
key = ""
mp = PivotPi()
a = 0
while key != ord("e"):
key = stdscr.getch()
stdscr.refresh()
mp.angle(SERVO_1, a)
if key == curses.KEY_UP: #close
a += 180
print(a)
elif key == curses.KEY_DOWN: #open
a -= 180
print(a)
curses.endwin()
Upvotes: 1
Views: 110
Reputation: 183
I'll assume these characteristics from your code are unintentional:
mp.angle
is always called when any key except [e] is pressed.mp.angle
is always one keypress behind the printed angle.
a
is only passed after the first keypress.These could be fixed as follows:
from pivotpi import *
from time import sleep
import curses
stdscr = curses.initscr()
curses.cbreak()
stdscr.keypad(1)
stdscr.refresh()
key = ""
mp = PivotPi()
a = 0
mp.angle(SERVO_1, a) # initialise before listening for keypresses
while key != ord("e"):
key = stdscr.getch()
stdscr.refresh()
if key == curses.KEY_UP: #close
a += 180
elif key == curses.KEY_DOWN: #open
a -= 180
else:
continue
print('\r\n%d\r' % a) # print to the left side of the terminal
mp.angle(SERVO_1, a)
curses.endwin()
Upvotes: 1