Browse files

added listener and pwm example

  • Loading branch information...
1 parent c01be39 commit 33a9892d6a77e496d83d27e23c8df436be0e8614 @svdgraaf committed Apr 25, 2012
Showing with 63 additions and 0 deletions.
  1. +24 −0 pwm_test/input.py
  2. +39 −0 pwm_test/pwm_test.ino
View
24 pwm_test/input.py
@@ -0,0 +1,24 @@
+import serial
+s = serial.Serial('/dev/tty.usbmodemfd131')
+
+import termios, fcntl, sys, os
+fd = sys.stdin.fileno()
+
+oldterm = termios.tcgetattr(fd)
+newattr = termios.tcgetattr(fd)
+newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
+termios.tcsetattr(fd, termios.TCSANOW, newattr)
+
+oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
+fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
+
+try:
+ while 1:
+ try:
+ c = sys.stdin.read(1)
+ print repr(c)
+ s.write(c)
+ except IOError: pass
+finally:
+ termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
+ fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
View
39 pwm_test/pwm_test.ino
@@ -0,0 +1,39 @@
+int pwmPin = 9; // LED connected to digital pin 9
+int current_power = 100;
+
+void setup() {
+ Serial.begin(9600);
+}
+
+void loop() {
+ if (Serial.available()) {
+ delay(100);
+ int power = current_power;
+
+ while (Serial.available() > 0) {
+ char c = Serial.read();
+ if(c == 'w')
+ {
+ power = power + 1;
+ }
+ if(c == 's')
+ {
+ power = power - 1;
+ }
+
+ if(c != 's' && c != 'w')
+ {
+ power = 0;
+ }
+ }
+
+ if(power != current_power)
+ {
+ analogWrite( pwmPin, power);
+ Serial.println(power);
+ current_power = power;
+ }
+ }
+}
+
+

0 comments on commit 33a9892

Please sign in to comment.