Salve, ho questo codice che sto scrivendo e funziona..ma volevo solo ridurre i numeri del valore n ,
invece mettere 0.3002, 0.2391 .. vorrei mettere 0.1, 0.2 etc.. vi posto il codice .
Sto usando arduino mega con firmata standard.
from pyfirmata import ArduinoMega, util
from time import *
board = ArduinoMega('COM3')
button = board.get_pin('d:2:i')
led = board.get_pin('d:9:p')
analog = board.get_pin('a:0:i')
it = util.Iterator(board)
it.start()
def main():
cmd = input("on, off, blink , button , analog , fade : ")
if(cmd == 'on'):
led.write(1)
sleep(0.5)
main()
elif (cmd == 'off'):
led.write(0)
sleep(0.5)
main()
elif(cmd == 'blink'):
try:
while True:
led.write(1)
sleep(0.5)
led.write(0)
sleep(0.5)
except KeyboardInterrupt:
led.write(0)
sleep(0.5)
main()
elif(cmd == 'analog'):
try:
analog.enable_reporting()
while True:
n = analog.read()
print("" + str(n) + "")
if n == 1.0:
print("\nTrovato")
elif n == 0.0:
print("\ninizio....")
elif n == 0.3548:
print("Bomba!!!!!!!!!!!!!!!")
sleep(.1)
except KeyboardInterrupt:
sleep(.5)
print("exit")
main()
elif (cmd == 'fade'):
try:
while True:
brightness = analog.read()
print ("Setting brightness to %s" % brightness)
led.write(brightness)
sleep(0.5)
if brightness == 0.0:
print("led spento")
elif brightness == 1.0:
print("led acceso")
except KeyboardInterrupt:
sleep(.5)
print("exit")
main()
elif (cmd == 'button'):
try:
button.enable_reporting()
number = 0
while True:
if str(button.read()) == 'True':
led.write(1)
number += 1
print("\nPremuto " + str(number))
else:
led.write(0)
sleep(.5)
except KeyboardInterrupt:
led.write(0)
number = 0
sleep(.5)
print("exit")
print("\nPremuto " + str(number))
sleep(1)
main()
if __name__=='__main__':
try:
main()
except KeyboardInterrupt:
print("\n in uscita...")
led.write(0)
sleep(0.5)
exit()
ad questo punto:
elif(cmd == 'analog'):
try:
analog.enable_reporting()
while True:
n = analog.read()
print("" + str(n) + "")
if n == 1.0:
print("\nTrovato")
elif n == 0.0:
print("\ninizio....")
elif n == 0.3548:
print("Bomba!!!!!!!!!!!!!!!")
sleep(.1)
except KeyboardInterrupt:
sleep(.5)
print("exit")
main()
come faccio fare che legga solo un solo numero .. come fosse un float.
Grazie mille e buona serata e inizio settimana