Python Forum
Python script runs on startup but does not register keystrokes.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python script runs on startup but does not register keystrokes.
I am using a raspberry pi zero to control a servo, using a wireless keyboard for a remote. I have an Adafruit 16 channel servo control, using Raspbian Jessie.

I modified the (open source) test script to accept keystrokes and change direction on the servo. I was hoping for something that would stop when I released the button but I have settled for a stop on up or down arrow when correct position is reached.

The script works well, and I've added it to /etc/rc.local with a 10-second delay. The zero boots to the terminal, the script starts with the familiar blank screen and flashing cursor. But it does not register keystrokes.

When I start the script manually it works fine, even if I ctrl=c out and start again. When I press up, the last text I typed in terminal (from my previous session) appears on the screen.

It seems that the keyboard is tied to the terminal, but I need it tied to the application.

I have the script below if it helps. As mentioned before, I did modify it from the Adafruit script.


# Simple demo of the PCA9685 PWM servo/LED controller library.
# This will move channel 0 from min to max position repeatedly.
# Original Author: Tony DiCola
# License: Public Domain

import time
import Adafruit_PCA9685
import curses

# Uncomment to enable debug output.
#import logging

# Initialise the PCA9685 using the default address (0x40).
pwm = Adafruit_PCA9685.PCA9685()

# Configure min and max servo pulse lengths
servo_min = 385  # Min pulse length out of 4096 150
servo_max = 400  # Max pulse length out of 4096 600
servo_off = 394

# Helper function to make setting a servo pulse width simpler.
def set_servo_pulse(channel, pulse):
    pulse_length = 20    # 1,000,000 us per second
    pulse_length //= 60       # 60 Hz
    print('{0}us per period'.format(pulse_length))
    pulse_length //= 4096     # 12 bits of resolution
    print('{0}us per bit'.format(pulse_length))
    pulse *= 1000 #1000
    pulse //= pulse_length
    pwm.set_pwm(channel, 0, pulse)

# Set frequency to 60hz, good for servos.

screen = curses.initscr()

	while True:
		char = screen.getch()
		if char == ord('q'):
		elif char == curses.KEY_LEFT:
			pwm.set_pwm(0, 0, servo_min)
		elif char == curses.KEY_RIGHT:
			pwm.set_pwm(0, 0, servo_max)
		elif char == curses.KEY_DOWN:
			pwm.set_pwm(0, 0, servo_off)
		elif char == curses.KEY_UP:
			pwm.set_pwm(0, 0, servo_off)

    curses.nocbreak(); screen.keypad(0); curses.echo()
Just for a quick check try setting up wasd for up/down/left/right to determine if the keyboard is communicating at all.
(Sep-06-2018, 07:34 PM)Vysero Wrote: Just for a quick check try setting up wasd for up/down/left/right to determine if the keyboard is communicating at all.

Ok - I did try this, and the output displayed on the screen as well, instead of being registered as program inputs.
Ok, I have it fixed now. I kept looking around and added the command line to the ~/.bashrc instead of rc.local. It seems it was stuck in limbo between background and foreground.

Possibly Related Threads…
Thread Author Replies Views Last Post
  Importing a function from another file runs the old lines also dedesssse 6 232 Jul-06-2021, 07:04 PM
Last Post: deanhystad
  pexpect startup help korenron 2 812 Apr-27-2021, 07:23 AM
Last Post: korenron
  Error when running script on startup in Linux NoahTheNerd 0 403 Mar-07-2021, 04:54 PM
Last Post: NoahTheNerd
  how to change the range of read CSV file every time python file runs greenpine 6 763 Dec-08-2020, 10:11 PM
Last Post: greenpine
  Runs perfect in Python but fails to print last statement when converted to .exe. Help sunil422 3 732 Aug-13-2020, 01:22 PM
Last Post: deanhystad
  Run script on startup in Debian with systemd? MrGlasspoole 5 1,248 Jul-12-2020, 11:48 AM
Last Post: MrGlasspoole
  How to kill a bash script running as root from a python script? jc_lafleur 4 1,400 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  How to create an Excel app that runs Python? felipe0216 3 739 May-31-2020, 01:19 AM
Last Post: ibutun
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 825 May-28-2020, 05:27 PM
Last Post: micseydel
  Login and Register system finndude 1 940 Apr-24-2020, 10:05 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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