Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python + Arduino on/off the LED.
#define LED 13
// Using http://slides.justen.eng.br/python-e-arduino as refference
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
char serialListener = Serial.read();
if (serialListener == 'H') {
digitalWrite(LED, HIGH);
}
else if (serialListener == 'L') {
digitalWrite(LED, LOW);
}
}
}
import serial # you need to install the pySerial :pyserial.sourceforge.net
import time
# your Serial port should be different!
arduino = serial.Serial('/dev/tty.usbmodem1411', 9600)
def onOffFunction():
command = raw_input("Type something..: (on/ off / bye )");
if command =="on":
print "The LED is on..."
time.sleep(1)
arduino.write('H')
onOffFunction()
elif command =="off":
print "The LED is off..."
time.sleep(1)
arduino.write('L')
onOffFunction()
elif command =="bye":
print "See You!..."
time.sleep(1)
arduino.close()
else:
print "Sorry..type another thing..!"
onOffFunction()
time.sleep(2) #waiting the initialization...
onOffFunction()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment