Processing arduino 3

From Medien Wiki
/**
 * Funktionsweise wie Beispiel 2, aber diesmal wird der Poti zum drehen genutzt  
 */


import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port
float[] inNumber;

Invader bob;
int bobsize = 280;
int bobparts = 11;

void setup() 
{
  size(500, 500);
  smooth();
  String portName = Serial.list()[1];
  myPort = new Serial(this, portName, 9600);
  myPort.bufferUntil('\n');
  
  bob = new Invader(bobparts);
 }

void draw(){
  background(255);

  if(inNumber != null) {
   bob.col = int(map(inNumber[1],570,919,0,255));                 // set fill to light gray
   pushMatrix();
   translate(width/2,height/2);
   rotate(map(inNumber[0],0,1023,0,TWO_PI));
   bob.render(-bobsize/2,-bobsize/2,bobsize);  
   popMatrix();
  }
}

void serialEvent(Serial myPort) {
  String inString = myPort.readStringUntil('\n');
  if(inString != null) {
    inString = trim(inString);
    inNumber = float(split(inString, ' '));
    }
}

class Invader {
  int[] invaders;

  int invader = 5;
  int invaderDiv = getInvaderDiv(invader);
  int invaderCount = getInvaderCount(invader);
  
  int recursive = 0;

  color col = color(0);
  
  Invader() {    
    init();
  }

  Invader(int i) {
    invader = i;
    invaderDiv = getInvaderDiv(invader);
    invaderCount = getInvaderCount(invader);
    init();  
  }

  Invader(int i, int r) {
    recursive = r;
    invader = i;
    invaderDiv = getInvaderDiv(invader);
    invaderCount = getInvaderCount(invader);
    init();  
  }

  Invader(int i, color c, int r) { 
    recursive = r; 
    col = c; 
    invader = i;
    invaderDiv = getInvaderDiv(invader);
    invaderCount = getInvaderCount(invader);
    init();
  }
  
  void init() {
    invaders = new int[invaderCount];
    for(int i = 0;i<invaderCount;i++) {
      invaders[i] = floor(random(2));
    }  
  }
    
  void render(float x, float y, float s) {   
    
    float blocksize = s / invader;
    int y2 = 0;
    int x2 = 0;
    
    noStroke();
    fill(col);
    
    pushMatrix();
      translate(x+blocksize*(invaderDiv-1),y);
      
      for(int i = 0;i<invaderCount;i++) {
        if(invaders[i] == 1) {        
          if(recursive > 0) {
            Invader in = new Invader(invader-2,recursive-1);
            in.render(x2*blocksize,y2*blocksize,blocksize);           
          } else {
            rect(x2*blocksize,y2*blocksize,blocksize,blocksize);
          }          
        }
        y2++;
        if((i+1)%invader==0) {
          x2++;
          y2 = 0;
        }        
      } 

      x2 = -1;
      y2 = 0;
      for(int i = invader;i<invaderCount;i++) {
        if(invaders[i] == 1) {
        if(recursive > 0) {
          Invader in = new Invader(invader-2,recursive-1);
          in.render(x2*blocksize,y2*blocksize,blocksize);           
        } else {
          rect(x2*blocksize,y2*blocksize,blocksize,blocksize);
        } 
        }
        y2++;
        if((i+1)%invader==0) {
          x2--;
          y2 = 0;
        }        
      }   
    popMatrix();
  }
  
  int getInvaderDiv(float i) {
    return ceil(i/2);
  } 
  
  int getInvaderCount(int i) {
    return i*invaderDiv;
  }
 
}   
   

/* Arduino Code

int potPin = 0;                       
int photoPin = 1;    
void setup() {
  Serial.begin(9600);    
}

void loop() {
  Serial.print(analogRead(potPin));
  Serial.print(" ");  
  Serial.print(analogRead(photoPin));  
  Serial.println();  
  delay(100);                          
}

*/