knutEinar
Junior Member
Offline
Posts: 55
Norway
Re: 2 temps from same commPort MixUp
Reply #16 - Jan 11th , 2010, 6:34am
Here is some code I have used, for reading four pots with Arduino: Processing code: // /* Serial Call and Response in ASCII Language: Processing Reads in a string of characters from a serial port until it gets a linefeed (ASCII 10). Then splits the string into sections separated by commas. Then converts the sections to ints, and prints them out. created 2 Jun 2005 modified 14 Apr 2009 by Tom Igoe */ import processing.serial.*; // import the Processing serial library Serial myPort; // The serial port float fgcolor; // Fill color float xpos, ypos; // Starting position of the ball float _alpha; //int[] sensors = new int[4]; void setup() { size(640,480); // List all the available serial ports println(Serial.list()); // I know that the first port in the serial list on my mac // is always my Arduino module, so I open Serial.list()[0]. // Change the 0 to the appropriate number of the serial port // that your microcontroller is attached to. myPort = new Serial(this, Serial.list()[0], 9600); // read bytes into a buffer until you get a linefeed (ASCII 10): myPort.bufferUntil('\n'); // draw with smooth edges: smooth(); noStroke(); } void draw() { background(127,100,100); fill(fgcolor, _alpha); // Draw the shape ellipse(xpos, ypos, fgcolor, fgcolor); } // serialEvent method is run automatically by the Processing applet // whenever the buffer reaches the byte value set in the bufferUntil() // method in the setup(): void serialEvent(Serial myPort) { // read the serial buffer: String myString = myPort.readStringUntil('\n'); // if you got any bytes other than the linefeed: myString = trim(myString); // split the string at the commas // and convert the sections into integers: int sensors[] = int(split(myString, ',')); // print out the values you got: /* for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) { print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t\t\t"); }*/ // add a linefeed after all the sensor values are printed: /// println(); if (sensors.length == 4) { sensA = sensors[0]; sensB= map(sensors[1], 0,255,0,width); sensC = map(sensors[2], 0,255,0,height); sensD = sensors[3]; } // send a byte to ask for more data: myPort.write("A"); } // Arduino code: int firstSensor = 0; // first analog sensor int secondSensor = 0; // second analog sensor int thirdSensor = 0; // digital sensor int fourthSensor = 0; int inByte = 0; // incoming serial byte void setup() { // start serial port at 9600 bps: Serial.begin(9600); establishContact(); // send a byte to establish contact until receiver responds } void loop() { // if we get a valid byte, read analog ins: // i.e. 'handshaking' ?? if (Serial.available() > 0) { // get incoming byte: why?? inByte = Serial.read(); // not used for anything, is it? // read first analog input, divide by 4 to make the range 0-255: firstSensor = analogRead(0)/4; // delay 10ms to let the ADC recover: delay(10); // read second analog input, divide by 4 to make the range 0-255: secondSensor = analogRead(1)/4; // delay 10ms to let the ADC recover: delay(10); thirdSensor = analogRead(2)/4; delay(10); fourthSensor = analogRead(3)/4; delay(10); // send sensor values: Serial.print(firstSensor, DEC); Serial.print(","); Serial.print(secondSensor, DEC); Serial.print(","); Serial.print(thirdSensor, DEC); Serial.print(","); Serial.println(fourthSensor, DEC); } } void establishContact() { while (Serial.available() <= 0) { Serial.println("0,0,0,0"); // send an initial string (determining the length of the sensors array! delay(300); } }