|
From: | Lisa Thierbach |
Subject: | Re: "real" time inputs? |
Date: | Thu, 21 Jul 2011 20:26:20 -0400 |
Doug, I'd love to, but a warning, it is long. For the Arduino code, I just pulled Example 7 from: I set up a simple circuit using a light sensitive resister. Then I used a program written in Processing (see processing.org). For this I pulled out what I wanted from Example 08A from the website above and put in a println. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ import processing.serial.*; Serial port; int i; String buffer = ""; // Accumulates characters coming from Arduino int light = 0; // light level measured by the lamp void setup() { println("in setup"); String arduinoPort = Serial.list()[0]; port = new Serial(this, arduinoPort, 9600); // connect to Arduino } void draw() { if (port.available() > 0) { // check if there is data waiting int inByte = port.read(); // read one byte if (inByte != 10) { // if byte is not newline buffer = buffer + char(inByte); // just add it to the buffer } else { // newline reached, let's process the data if (buffer.length() > 1) { // make sure there is enough data // chop off the last character, it's a carriage return // (a carriage return is the character at the end of a // line of text) buffer = buffer.substring(0,buffer.length() -1); // turn the buffer from string into an integer number light = int(buffer); println(light); // clean the buffer for the next read cycle buffer = ""; // We're likely falling behind in taking readings // from Arduino. So let's clear the backlog of // incoming sensor readings so the next reading is // up-to-date. port.clear(); } } } } ++++++++++++++++++++++++++++++++++++++++++++++++++++ Again, I didn't write the above code, so don't give me credit for it. It came from the makezine website. The processing gui has an option to make a stand alone application from the code, which I did. As a warning, the code produces a window containing a useless gray box. Someday, I intend for it to show the incoming data, but for now it is just a gray box. I then called this application from Octave and read in the results: ++++++++++++++++++++++++++++++++++++++++++++++++++++ skip = 50; buffer = 1024; fid = popen("./get_data/application.linux32/get_data", "r"); for(i = 1:skip) s = fgets(fid); if(s == -1) fprintf(stdout, "Problem reading in data"); return end end [d, count] = fscanf(fid, "%d", buffer); plot(1:buffer, d) return;
++++++++++++++++++++++++++++++++++++++++++++++++++++ Skipping some lines at the beginning is necessary because the processing application spits out some lines of text before the actual code begins to run. By far, the biggest issue I had was with the processing application in that the serial libraries were having conflicts with each other and not able to write into the directories they wanted to. I fixed the problem by downloading the binaries of the libraries here: make sure to get version rxtx-2.1-7-bins-r2 Then installing it following the instructions: For the issue with file permissions with the serial locks I had to give myself write permission to the directories: /var/spool/uucp /var/lock I hope this helps, or gives someone an idea of where to go next. Once I get this code doing something more interesting, I can put it on my website. |
[Prev in Thread] | Current Thread | [Next in Thread] |