Moe
YaBB Newbies
Offline
Posts: 8
Sweden
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(); } } }