2016-07-17 12 views
2

Ich versuche, einige Neopixel über Python mit einem Arduino verbunden zu steuern und bin in ein Problem laufen. Zu diesem Zweck leuchten sie auf, wenn das Arduino das Zeichen "H" oder "L" über die serielle Schnittstelle empfängt.Interrupt While-Schleife mit Benutzereingaben (Steuern Neopixel über Arduino und Python 2.7)

Meine ursprüngliche Skript war:

import serial 
import time 

ser = serial.Serial('/dev/ttyACM0', 9600) 
#this is necessary because once it opens up the serial port arduino needs a second 
time.sleep(3) 

ser.write('H') 

Während es gut funktioniert, wenn ich es in die Python-Konsole eingegeben haben, wandten sich die Lichter ca. 3 Sekunden in aus, wenn ich es als Skript ausgeführt. Nachdem einige graben tun, es sah aus wie eine Arbeit um war nur das letzte Stück in eine while-Schleife zu drehen, um die serielle Verbindung nicht geschlossen wurde:

import serial 
import time 

ser = serial.Serial('/dev/ttyACM0', 9600) 
#this is necessary because once it opens up the serial port arduino needs a second 
time.sleep(1) 

while True: 
    ser.write('H') 
    time.sleep(3) 

Dies hielt das Licht an, aber ein neues Problem geschaffen. Wenn ich die Lichter will nach Benutzereingabe ändern kann ich es tun, wenn:

import serial 
import time 

ser = serial.Serial('/dev/ttyACM0', 9600) 
#this is necessary because once it opens up the serial port arduino needs a second 
time.sleep(1) 

choice= raw_input("1 or 2?") 


if choice == "1": 

    while True: 

     ser.write('H') 
     time.sleep(3) 


elif choice == "2": 

    while True: 

     ser.write('L') 
     time.sleep(3) 

Aber dann wird das Skript nur in der Sub-Schleife stecken. Wie halte ich den Subloop an (d. H. Halte das Licht an), warte aber auch darauf, auf eine neue Benutzereingabe zu reagieren?

danke!

+0

Diese könnten helfen: http://stackoverflow.com/questions/32369495/how-to-wait-for-an-input-without-blocking-timer-in-python, http: // Stackoverflow. com/questions/16828387/how-to-accept-user-input-ohne-warten-in-python, http://stackoverflow.com/questions/31340/how-do-threads-work-in-python-and- what-are-common-python-threading-spezifische-pit –

Antwort

2

Dies ist die Lösung, die ich selbst gefunden habe.

import serial 
import time 

ser = serial.Serial('/dev/ttyACM0', 9600) 

#this is necessary because once it opens up the serial port arduino needs a second 
time.sleep(2) 

#ask for the initial choice 
choice= raw_input("1 or 2?") 

#keeps the loop running forever 
while True: 

    #if the initial choice is 1, do this 
    while choice == "1": 

     #send the H signal to the arduino 
     ser.write('H') 

     #give the user a chance to modify the chioce variable 
     #if the variable is changed to 2, the while condition will no longer 
     #be true and this loop will end, giving it an oppotunity to go 
     #do the second while condition. 
     #pending the decision the light will stay on 
     choice= raw_input("1 or 2?") 


    while choice == "2": 

     ser.write('L') 

     choice= raw_input("1 or 2?")