Tuesday, July 21, 2009

Code and further work


So in order to start this off right, I wanted to establish the very most basic of communication and method to verify that communication. Essentially I setup a breadboard with 3 LED's and each LED is wired to the Arduino on a different digital pin. This will let verify that what the python is telling the Arduino, is really occuring.

As you can see on the left in the photo, the setup is simple. The "red" is the connection for the Arduino to the LED's(through resistors) and then the black coming from the LED's are the ground. That goes to a shared ground, which then is wired back to the arduino's onboard ground. Very straightforward.

Here is a video I made of the setup in action. Sorry for the bad quality on all of my media.. its coming from a G1 phone, and thus pretty horrible.



As you can see, I now have a successful setup that allows a python "script" to command my Arduino in a very basic form. At the very bottom of this post I have both the arduino code and the python script. In order to run the python script you will need Python(http://www.python.org/) and also the Python Serial module(http://pyserial.sourceforge.net/). The code is designed to run a linux box, so keep that in mind. The only change you would need to make on a windows box is in the '/dev/ttyUSB0' section. You would need to change that to COMX, where X is the arduino's communication port.

I am writing this assuming that you have basic programming knowledge, which is all I have myself. It is a quick and dirty example of how this could work. I say quick and dirty because the Python script does no actual "thinking" it just sends the arduino a letter, waits a second, sends another, waits a bit more, and sends a third letter. In the Python code you will notice at the start it has a delay put in and that is because Python will exectue so fast that the serial buffers can't sync up.

Hopefully this is pretty self-explanatory. If you have any questions just comment and I will get to them ASAP. This is just the very very first step in the setup. Next comes building some functions for both the arduino and the Python. I am currently planning on a basic "start" section where the Python and arduino can negoitate with eachother about what type of error checking. This is done for scaling purposes; as the code base gets larger I want to have it already capable of a 'strict' error system that minimizes lost data. Then I will have a second one that is 'speed orientated' and whos only goal will be to make sure that it is discarding damaged chunks of code.

Please note, I generally won't be posting twice a day, but this is the first day and I am a pot of coffee in. :) Thanks all! Heres the code:


Python:

import serial
import time

ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=0)

print "Current port location:"
print ser.portstr
time.sleep(1.75)

print ser.readline()
time.sleep(.1)

ser.write("P")
time.sleep(1.5)

ser.write("Q")
time.sleep(1.5)

ser.write("R")
time.sleep(1.5)

ser.close()


Now, onto the Arduino "sketch":

int led1 = 31;
int led2 = 33;
int led3 = 35;
int InputSer = 0; // led1-3 are just variables for which pins are connected to LED's on a breadboard.
void setup()
{
Serial.begin(9600); //Rather straightforward, 9600 baud rate for this serial connection.
Serial.println("Adruino Python serial system. Ard Client. Build #3");
pinMode(led1, OUTPUT); // These are setting the pins to be used as OUTPUT's(and thus power the LED's).
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}


void loop() {

if (Serial.available() > 0) { //This is a test to see if anything is in the buffer. If so:

InputSer = Serial.read(); //This will read the next byte in the serial buffer.
Serial.println(InputSer); //This prints that variable, only done for debugging.
if(InputSer == 80){ // P to hit LED1 on
digitalWrite(led1, HIGH); // sets the LED on
delay(1000);
digitalWrite(led1, LOW);
}
if(InputSer == 81){ // Q to hit LED2 on
digitalWrite(led2, HIGH); // sets the LED on
delay(1000);
digitalWrite(led2, LOW);
}
if(InputSer == 82){ // R to hit LED3 on
digitalWrite(led3, HIGH); // sets the LED on
delay(1000);
digitalWrite(led3, LOW);
}
// Above you will see that I have a few different things that may need explaining. First of all, the 80, 81, and 82. This is what P, Q, and R are in "byte" form. These are just some numbers I guessed at random..
// If you are curious what numbers == what letters, just hit alt + XXXX. Alt 0080 is P. 81 is Q, 82 is R.
}
}



No comments: