Saturday 14 November 2009

Arduino - Homework 1

Many hours above Arduino..but isn't this always the case with programming? especially when we have to deal with a new (for us) technology and the only "debugging-hint" it has to show is Null Reference Exception. At least at the end of the day I had the result I wished for! A great relief, as I lost my faith in the way to be honest. =P

The homework we were assigned to do on Thursday has to do with the Arduino - Processing communication. We firstly have to read the values of 2 different sensors (in the Arduino) and then to pass these values successfully to Processing in which we will display them by changing the size of 2 ellipses.

The solution
Click on the images to enlarge.

Circuit



Arduino



Processing



How? Issue #1

In the program for Arduino we read the values of the potentiometer and the light sensor from the analog input and we send them to the program written in Processing via the serial port. The numbers we want to pass via the serial port are integers and the issue was: How can I send integers from Arduino and receive neither bytes, nor ASCII characters or anything else but integers in the Processing? Keeping the Arduino part simple, we send (print) the integers as they are in the Serial port. The integers are sent one by one (one byte each) in ASCII format. So for example if the integer 1023 is sent from Arduino, according to the ASCII table, Processing would receive the bytes: 49 48 50 51. The way implemented above for reading this steam, is firstly to convert these numbers to the digits they represent in string format (in this example "1", "0", "2", "3"), then to store each string of them to a larger string which keeps the numbers received and finally when the steam stops, to convert the string which stores the whole value to an integer.

How? Issue #2
The second issue was to send / receive / read 2 different values at the same time. For doing that, following Mr. Martin's hint, we take advantage of the fact that Processing receives ASCII characters; in Arduino if we use the print-line function to write at the stream (Serial.println) instead of the print function, at the end of the message 2 ASCII characters will be added: 10 (line feed) and 13 (carriage return). Each time we reach an ASCII character which is equal to 10 we know there isn't any other digit to be added to the number we are currently receiving. The next step is to identify which number is this. Was it the first or the second in the message sent from Arduino? For this we use a boolean variable (isNext). In the case where there are more integers to be seperated we would probably use a short integer in the place of the boolean variable.

No comments:

Post a Comment