Jim's
Tutorials

Fall 2011
course
navigation

sept 30

newsoftserial

So you talked about the issue of using the USB on the arduino to program it, and having processing using the serial line, and not having access to the serial itself.
If you're talking about the arduino program console, this may not help (it may only use the serial lines that are hardwared into the arduino).
But in more general terms, it is possible to use other pins on the arduino for serial input/output, with a software library : http://arduiniana.org/libraries/newsoftserial/ . Ben L used this last semester for his ardunino/xbee/sensor project.
- Jim

jim says

A few code notes:
In the first part, rather than the long if :
int greys[] = {0, 20, 40, 60, 80, 100, 120, 140}; // ... fill(greys[val]);
In the second part, rather than the long if : eyebrownum = reading1 + 2 * reading2 + 4 * reading3;

aaron

I've been trying to revive a few of the things I created last semester to varying degrees of success. The eyebrow switches work right out of the box thanks to my earlier semi-exhaustive documentation.
It turns out that the file I had written a few weeks back that made the teeth open and closed is lost somewhere. I guess I uploaded the wrong file. I hadn't changed too much so it shouldn't be much trouble to rewrite but it makes me realize that backing things up is a good thing.
I'd like to get the eyebrow switch to be read by processing but I haven't spent too much time on that yet.
Here's the processing sketch. It's an edited version of the example sketch in Serial I/O: SimpleRead in the processing application's built-in examples.
/** * Simple Read * * Read data from the serial port and change the color of a rectangle * when a switch connected to a Wiring or Arduino board is pressed and released. * This example works with the Wiring / Arduino program that follows below. */ import processing.serial.*; Serial myPort; // Create object from Serial class int val; // Data received from the serial port void setup() { size(1000, 1000); // I know that the first port in the serial list on my mac // is always my FTDI adaptor, so I open Serial.list()[0]. // On Windows machines, this generally opens COM1. // Open whatever port is the one you're using. String portName = Serial.list()[0]; myPort = new Serial(this, portName, 9600); } void draw() { if ( myPort.available() > 0) { // If data is available, val = myPort.read(); // read it and store it in val } background(255); // Set background to white if (val == 0) { // If the serial value is 0, fill(0); // set fill to black } else if (val == 1) { fill(20); } else if (val == 2) { fill(40); } else if (val == 3) { fill(60); } else if (val == 4) { fill(80); } else if (val == 5) { fill(100); } else if (val == 6) { fill(120); } else if (val == 7) { fill(140); } else { // If the serial value is not 0, fill(204); // set fill to light gray } rect(50, 50, 900, 900); } /* // Wiring / Arduino Code // Code for sensing a switch status and writing the value to the serial port. int switchPin = 4; // Switch connected to pin 4 void setup() { pinMode(switchPin, INPUT); // Set pin 0 as an input Serial.begin(9600); // Start serial communication at 9600 bps } void loop() { if (digitalRead(switchPin) == HIGH) { // If switch is ON, Serial.print(1, BYTE); // send 1 to Processing } else { // If the switch is not ON, Serial.print(0, BYTE); // send 0 to Processing } delay(100); // Wait 100 milliseconds } */
...And here's the arduino sketch:
float freq; float prev; int inPin1 = 2; // the number of the input pin int inPin2 = 3; int inPin3 = 4; int outPin1 = 9; // the number of the output pin int outPin2 = 10; int outPin3 = 11; int reading1; // the current reading from the input pin int reading2; int reading3; int eyebrow_num; void setup() { pinMode(inPin1, INPUT); pinMode(inPin2, INPUT); pinMode(inPin3, INPUT); pinMode(outPin1, OUTPUT); pinMode(outPin2, OUTPUT); pinMode(outPin3, OUTPUT); Serial.begin(9600); // for testing purposes. } void loop() { reading1 = digitalRead(inPin1); reading2 = digitalRead(inPin2); reading3 = digitalRead(inPin3); //------- //DEBUG if(reading1 == LOW){ //Serial.print("reading1 is LOW\n"); } if(reading2 == LOW){ //Serial.print("reading2 is LOW\n"); } if(reading3 == LOW){ //Serial.print("reading3 is LOW\n"); } //------- // 000 if(reading1 == HIGH && reading2 == HIGH && reading3 == HIGH){ eyebrow_num = 0; } // 001 else if(reading1 == HIGH && reading2 == HIGH && reading3 == LOW){ eyebrow_num = 1; } // 010 else if(reading1 == HIGH && reading2 == LOW && reading3 == HIGH){ eyebrow_num = 2; } // 011 else if(reading1 == HIGH && reading2 == LOW && reading3 == LOW){ eyebrow_num = 3; } // 100 else if(reading1 == LOW && reading2 == HIGH && reading3 == HIGH){ eyebrow_num = 4; } // 101 else if(reading1 == LOW && reading2 == HIGH && reading3 == LOW){ eyebrow_num = 5; } // 110 else if(reading1 == LOW && reading2 == LOW && reading3 == HIGH){ eyebrow_num = 6; } // 111 else if(reading1 == LOW && reading2 == LOW && reading3 == LOW){ eyebrow_num = 7; } delay(20); // Only ask for the value every 10ms if Serial.available()) { Serial.println(eyebrow_num, DEC); } }
http://cs.marlboro.edu/ courses/ fall2011/jims_tutorials/ aaron/ sept_30
last modified Friday September 30 2011 3:06 pm EDT