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

23. November 2011

Arduino-Steuerung via Processing

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. Ich habe die Ampelübung nochmals aufgenommen, hier aber die drei Lichter in Processing mit einem Mouse-Over dargestellt. Wenn sich die Maus über einem der Kreise befindet, dann leuchtet die entsprechende LED auf.

Arduino-Code

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

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

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

void setup()
{
  Serial.begin(9600);
  pinMode(LED_PIN1,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); // ...
      //Serial.println("THIRD: "+thirdValue);
      //fourthValue = atoi(d); // add another line if needed

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

void loop()
{

  analogWrite(LED_PIN1, firstValue);
  analogWrite(LED_PIN2, secondValue);
  analogWrite(LED_PIN3, thirdValue);

  readSerial(); // read the values available at the serial port

  /*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
  */
}

Processing-Code


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

float firstValue = 0;
float secondValue = 0;
float thirdValue = 0;

void setup()
{
  smooth();
  background(0);
  size(500, 500);
  println(Serial.list()); // Prints the list of serial available devices (Arduino should be on top of the list)
  myPort = new Serial(this, Serial.list()[0], 9600); // Open a new port and connect with Arduino at 9600 baud
}

void draw()
{
  // RED
  firstValue=0;
  fill(102,0,0);
  if (mouseOver(width/2, 100, 100/2)) {
    firstValue = 255;
    fill(255,0,0);
  }
  ellipse(width/2, 100, 100,100);
  // YELLOW
  thirdValue=0;
  fill(102,102,0);
  if (mouseOver(width/2, 250, 100/2)) {
    thirdValue = 255;
    fill(255,255,0);
  }
  ellipse(width/2, 250, 100,100);
  // GREEN
  secondValue=0;
  fill(0,102,0);
  if (mouseOver(width/2, 400, 100/2)) {
    secondValue = 255;
    fill(0,255,0);
  }
  ellipse(width/2, 400, 100,100);

  myPort.write(str(firstValue));
  myPort.write("|");
  myPort.write(str(secondValue));
  myPort.write("|");
  myPort.write(str(thirdValue));
  myPort.write("|");
  myPort.write("\r");
  delay(10);
}

boolean mouseOver(int x, int y, int r) {
  //println(x+" : "+y+" : "+r);
  if (mouseX>x-r && mouseXy-r ) {
    return true;
  } else {
    return false;
  }
}