We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Arduino multiplexing (Read 2101 times)
Arduino multiplexing
Feb 10th, 2009, 9:31am
 
Hi.

My Arduino is having some problems in it's communication with processing. I'm using a multiplexer (will be more) to send 8 sensor values to arduino and then into processing.

EDIT:

changed the code completely and went for a call-and-response aproach instead. This works a bit better - processing reads all values sent from arduino. The thing is, processing prints a horde of 10's and 13's around the "real" values (the ones I want to use). I have no idea what these come from and I'd really appreciate it if someone could help me get rid of them.



Example output:

13      13      10      13      167      13      10      10      
13      10      181      10      13      13      10      13      
10      13      165      13      10      10      13      10      
165      13      10      10      13      10      13      10      
13      10      13      10      13      10      13      10      
10      198      184      10      13      13      10      10      
13      10      183      10      13      10      13      10      
183      10      13      13      10      13      10      13      
181      10      13      10      13      13      10      13



Arduino code:


int r0 = 0;      //value select pin at the 4051 (s0)
int r1 = 0;      //value select pin at the 4051 (s1)
int r2 = 0;      //value select pin at the 4051 (s2)
int row = 0;     // storing the bin code
int count = 0;    // just a count
int inByte = 0;         // incoming serial byte
int  bin [] = {000, 1, 10, 11, 100, 101, 110, 111};
long  inputValues [] = {0,0,0,0,0,0,0,0};

void setup() {
 // iniaialize the serial port:
//  Serial.begin(9600);
 beginSerial(9600);
   pinMode(2, OUTPUT);    // s0
   pinMode(3, OUTPUT);    // s1
   pinMode(4, OUTPUT);    // s2
   establishContact();  // send a byte to establish contact until Processing responds
}

void loop() {
   // if we get a valid byte, read analog ins:
 if (Serial.available() > 0) {
   // get incoming byte:
   inByte = Serial.read();
   
 // check all 8 channels
 for (int channel = 0; channel < 8; channel++) {
   row = bin[channel];      
   r0 = row & 0x01;
   r1 = (row>>1) & 0x01;
   r2 = (row>>2) & 0x01;
   digitalWrite(2, r0);
   digitalWrite(3, r1);
   digitalWrite(4, r2);
   
   delay(8);
   // read an analog input, divide the result by 4
   // to limit it to 0-255:
   inputValues[channel] = analogRead(5)/4;
   }
 
 for(int i = 0 ; i < 8 ; i++){
   Serial.print(inputValues[i], BYTE);
   }
 }
 // once you've printed all the values,
 // print a newline and carriage return:
 Serial.println();
 
}

void establishContact() {
while (Serial.available() <= 0) {
     Serial.print('A', BYTE);   // send a capital A
     delay(300);
 }
}




Processing:

import processing.serial.*;

Serial myPort;                       // The serial port
int[] serialInArray = new int[8];    // Where we'll put what we receive
int serialCount = 0;                 // A count of how many bytes we receive
boolean firstContact = false;        // Whether we've heard from the microcontroller


void setup() {
 size(480, 80);  // Stage size
 noStroke();      // No border on the next thing drawn

 // Print a list of the serial ports, for debugging purposes:
 println(Serial.list());

 String portName = Serial.list()[0];
 myPort = new Serial(this, portName, 9600);
}

void draw() {
 background(0);
 fill(255);
 // Draw the shape
 for(int i = 0 ; i < 8 ; i++){
   ellipse(40+i*50, 40, serialInArray[i]/8, serialInArray[i]/8);
 }
}

void serialEvent(Serial myPort) {
 // read a byte from the serial port:
 int inByte = myPort.read();
 // if this is the first byte received, and it's an A,
 // clear the serial buffer and note that you've
 // had first contact from the microcontroller.
 // Otherwise, add the incoming byte to the array:
 if (firstContact == false) {
   if (inByte == 'A') {
     myPort.clear();          // clear the serial port buffer
     firstContact = true;     // you've had first contact from the microcontroller
     myPort.write('A');       // ask for more
   }
 }
 else {
   // Add the latest byte from the serial port to array:
   serialInArray[serialCount] = inByte;
   print(inByte + "\t");
   serialCount++;
   delay(8);
   
   if (serialCount == 8) {
     // Send a capital A to request new sensor readings:
     myPort.write('A');
     // Reset serialCount:
     serialCount = 0;
     println();
   }
 }
}
Re: Arduino multiplexing
Reply #1 - Feb 15th, 2009, 7:10pm
 
Hi Moe,

Serial communication uses ascii encoding.  10 and 13 are ascii newline and carriage return  (http://www.asciitable.com/)

I think the call to

Serial.println();

after you output the array in Arduino code would likely account for all the 10,13 you are seeing.

steve

Re: Arduino multiplexing
Reply #2 - Feb 16th, 2009, 9:34pm
 
Hi. Yes, I noticed that. didn't think to look them up in an ascii table ^^. I did find a (rather clumsy) way around it, by just mapping 10 to 11 and 13 to 14 in the arduino code.. this means whenever one of my sensors are sending a 10, it automaticly changes to a 11. In processing, I just tell the program to ignore 10's and 13's.

I should probably mention that I tried to just omit the println() in arduino, but then everything just crashed. I don't understand why the line feed is needed, so if anyone knows, please tell.

Anyway.. I got both programs working now and today i managed to hook up another multiplexer to the circuit as well. If anyone wants to see the code, don't hesitate to ask. I'm sure there are more competent programmers who have done this before, but still..

Thanks for the answer, Steve.
Page Index Toggle Pages: 1