Willkommen auf unserem Seminar-Blog

Immer auf dem aktuellen Stand bleiben

Dieser Seminar-Blog befindet sich noch im Aufbau und wird in den kommenden Tagen entsprechend verfeinert.

Member Login

Lost your password?

Registration is closed

Sorry, you are not allowed to register by yourself on this site!

You must either be invited by one of our team member or request an invitation by email at viad.info {at} zhdk {dot} ch.

12 Processing -> Arduino

5. Dezember 2011

Bei vorhergehenden Aufgaben haben wir bereits Daten von Arduino an Processing geschickt. Hier werden nun Daten via Serial zurück an Arduino übertragen, um elektronische Bauteile steuern zu können.

#define BUFFER_SIZE 20 // actual size of the buffer for integer values: (numberOfValsToRead*6)+(numberOfValsToRead-1)
#define LED_PIN 3
#define LED_PIN2 11
#define LED_PIN3 10

char incommingBuffer[BUFFER_SIZE]; // buffer to store incomming values
char incomming; // primary buffer to store single incommning bytes
int incommingCounter = 0; // counter for counting the positions inside the buffer

int firstValue, secondValue, thirdValue; // fourthValue, fifthValue, ... // add more if needed

void setup()
{
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
pinMode(LED_PIN2, OUTPUT);
pinMode(LED_PIN3, OUTPUT);
}

void readSerial()
{
while(Serial.available())
{
incomming = Serial.read(); // read single incommning bytes

if(incomming != '\r') //if no carriage return is received proceed in reading the serial port
{
incommingBuffer[incommingCounter++] = incomming; // go on the next position in the buffer
}
else //read until a carriage ('\r') is received
{
incommingBuffer[incommingCounter] = '\0'; // set the last byte to NULL to sign it for the string operators

char *a = strtok(incommingBuffer, ",.;"); // split the string after delimiters into tokens
char *b = strtok(NULL, ",.;"); // ...
char *c = strtok(NULL, ",.;"); // ...
//char *d = strtok(NULL, ",.;"); // add another line if needed

firstValue = atoi(a); // convert the strings into integers
secondValue = atoi(b); // ...
thirdValue = atoi(c); // ...
//fourthValue = atoi(d); // add another line if needed

incommingCounter = 0; // reset the counter
memset(incommingBuffer, 0, BUFFER_SIZE); //overwrite the incommingBuffer
}
}
}

void loop()
{
readSerial(); // read the values available at the serial port
analogWrite(LED_PIN, firstValue);
analogWrite(LED_PIN2, secondValue);
analogWrite(LED_PIN3, thirdValue);
/* Serial.print(firstValue); // debugging
Serial.print("\t");
Serial.print(secondValue);
Serial.print("\t");
Serial.print(thirdValue);
Serial.print("\t");
//Serial.print(fourthValue); // add these lines if needed
Serial.println(); // send a carriage return for debugging
*/
}


import processing.serial.*;  // Import the Processing Serial Library for communicating with arduino
Serial myPort;

void setup()
{
size(500,500);
background(0);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
}

void draw()
{
noStroke();
fill(255,0,0);
triangle(width, 0, 0, height/2, width, height);
fill(0,255,0,180);
triangle(0, 0, width, height/2, 0, height);
fill(255,255,0,180);
triangle(0, height, width/2, 0, width, height);
fill(255);
rect(width/2,height/2,5,5);
myPort.write(str(int(map(mouseX,0,width,0,255))));
myPort.write(",");
myPort.write(str(int(map(mouseY,0,height,0,255))));
myPort.write(",");
myPort.write(str(int(map(mouseX,0,width,255,0))));
myPort.write("\r");
}