Part1: http://dredx.com/electronics/?p=199
Part2:
Please keep in mind python uses indented blocks to show if code belongs to a block so if you copy the code directly be sure if adding new code you use spaces to lineup code not tabs
The text editor I would recommend for python code is Notepad++ but any text editor will do.
Now on to the code:
This is the arduino sketch we will be sending data to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /* This Program will recieve data from the computer over serial and process it as different commands depending on the leading byte */ // Pin 13 has an LED connected on most Arduino boards. // give it a name: int led[] = {13,8}; int current; int state; // the setup routine runs once when you press reset: void setup() { // initialize the digital pin as an output. pinMode(led[0], OUTPUT); pinMode(led[1], OUTPUT); Serial.begin(9600); } // the loop routine runs over and over again forever: void loop() { if (Serial.available() > 0) { while (Serial.peek() == 'L') { //check for the character that signifies that this will be a command Serial.read(); //remove the L off the serial buffer current = Serial.parseInt(); Serial.read(); //remove the 'S' that we used as a integer seperator off the serial buffer state = Serial.parseInt(); //store our expected integer into state digitalWrite(led[current],state); // set the state of the LED } while (Serial.available() > 0){ //Discard everything that we didn't expect Serial.read(); } } } |
And here is the python code we will be writing in the video…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import serial #library to interact with serial port.. very similar to arduino version import time #for delays import msvcrt #to detect key presses print "Initalizing Serial port..." arduino = serial.Serial(2,9600,timeout = 1) # 2=Com3 on windows always a good idea to sepcify a timeout incase we send bad data time.sleep(2) #wait for initialize print "Initialization Complete" while 1: # Like Arduino for loop makes the program loop forever led0 = 0 #state to set LED0 led1 = 1 #state to set LED1 command = 'L0S%s' % led0 #Add state of LED0 to command string command += 'L1S%s' % led1 #Add state of LED1 to command string command += '\n' #add newline character to command to signify end of command arduino.write(command) #send command time.sleep(.5) #sleep for .5 second command = 'L0S1L1S0\n' #set command all on one line arduino.write(command) #send command time.sleep(.5) #sleep for .5 second if msvcrt.kbhit(): #if there was a keyboard press keypress = ord(msvcrt.getch()) if keypress == 113: # if the key pressed = 'q' break #exit the loop print 'Sorry to see you go.' |
also I thought someone might find this code useful, I wont be using this for the tutorial because it will be tougher to explain quickly but it shows how to set a True/False(aka On/Off) variable to the opposite value without multiple if statements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import serial #library to interact with serial port.. very similar to arduino version import time #for delays import msvcrt #to detect key presses print "Initalizing Serial port...\n" arduino = serial.Serial(2,9600,timeout = 1) # 2=Com3 on windows always a good idea to sepcify a timeout incase we send bad data time.sleep(2) #wait for initialize print "Ready for Input\n" led0 = 0 led1 = 0 while 1: # Like Arduino for loop makes the program loop forever if msvcrt.kbhit(): #if there was a keyboard press keypressed = ord(msvcrt.getch()) if keypressed == 49: # if the key pressed = '1' if led0 == 0: #if led0 is off arduino.write('L0S1\n') #send command to turn it on led0 = 1 #set variable to indicate led0 is on elif led0 == 1: #if led0 is on arduino.write('L0S0\n') #send command to turn it off led0 = 0 #set variable to indicate led0 is off elif keypressed == 50: # if the key pressed = '2' led1 = int(not led1) #set state oposite current making sure it stays as integer instead of boolean command = 'L1S%s\n' % led1 arduino.write(command) #send command elif keypressed == 51: # if the key pressed = '3' led0 = int(not led0) #set state oposite current making sure it stays as integer instead of boolean led1 = int(not led1) command = 'L0S%sL1S%s\n' % (led0,led1) arduino.write(command) elif keypressed == 113: # if the key pressed = 'q' break #exit the loop print 'Sorry to see you go.' |
Discussion ¬