« Analog input example for pyFirmatapyFirmata GUI »

Dimming a LED with pyFirmata

There are a lot of tutorials available for dimming a LED with sketches. My approach uses pyFirmata to do it.

  1. import pyfirmata
  2.  
  3. # Time (approx. seconds) to get to the maximum/minimum
  4. DURATION = 5
  5. # Numbers of steps to get to the maximum/minimum
  6. STEPS = 10
  7.  
  8. # Creates a new board and define the pin
  9. board = pyfirmata.Arduino(/dev/ttyACM0)
  10. digital_0 = board.get_pin('d:11:p')
  11.  
  12. # Waiting time between the
  13. wait_time = DURATION/float(STEPS)
  14.  
  15. # Up
  16. for i in range(1, STEPS + 1):
  17. value = i/float(STEPS)
  18. digital_0.write(value)
  19. board.pass_time(wait_time)
  20.  
  21. # Down
  22. increment = 1/float(STEPS)
  23. while STEPS > 0:
  24. value = increment * STEPS
  25. digital_0.write(value)
  26. board.pass_time(wait_time)
  27. STEPS = STEPS - 1
  28.  
  29. board.exit()

Download the commented and extended script from here.

Permalink 02/20/12 07:35:00 pm, by fab Email , 89 words, Categories: General, Arduino ,

No feedback yet