Wednesday, October 17, 2012

Arduino + Processing: first go.

THE CODE:


//CCLab Home work week_07
//Cupcake & Strawberry - array - radius - oscillation of an object - mouse
//Processing + Arduino

//new tab
PImage straw1 /*straw2*/;
// array -  first give it a name then a legnth
ArrayList Strawberry;

//Arduino button
import processing.serial.*;
import cc.arduino.*;
int pin = 5;
Arduino arduino;

int sensorValue;

void setup() {
  smooth();
  size(600, 600);
  
  arduino = new Arduino(this, Arduino.list()[0], 57600);
  noFill();
  noStroke();
  for (int i = 0; i <= 13; i++){
  arduino.pinMode(i, Arduino.INPUT);
 }
  
  //initialize first step
 Strawberry = new ArrayList();
 Strawberry.add(new Strawberry(width/2, height/2));

  straw1 = loadImage("straw1.png");
  //straw2 = loadImage("straw2.png");
    
}

void draw() {
  background(255);
  
  //Arduino button
  if (arduino.digitalRead(pin) == Arduino.HIGH){
     Strawberry.add(new Strawberry(mouseX, mouseY));
     
}
    else { 
      fill(0);
      noStroke();
    }
  
PImage myImage = loadImage("cake.png");
imageMode(CENTER);
image(myImage, width/2, height/2);

//I'm going to have a array of strawberry
 for(int i = 0; i < Strawberry.size(); i ++){
   Strawberry myStrawberry = (Strawberry) Strawberry.get(i);
   //this where you put it
   //draw the array
   myStrawberry.move();
   myStrawberry.display();
  
 }
   
//ellipse(280, 240, arduino.analogRead(pin), arduino.analogRead(pin));
  
    println(arduino.digitalRead(pin));
    
}

 /*void mousePressed(){
 Strawberry.add(new Strawberry(mouseX, mouseY));
 //telling what to do - add - calling the contucttor
 }*/

THE CLASS CODE:

class Strawberry {
  //Strawberry images
  PImage straw1 /*straw2*/;
  //color c;
  float xPos;
  float yPos;
  float ySpeed; //speed up and down
  float acc; //gathering speed

  Strawberry(float tempX, float tempY) { //same as class
  //strawberry(float tempYSpeed) { //feeding my donut //to make 1 a different speed
  straw1 = loadImage("straw1.png");
  //straw2 = loadImage("straw2.png");
  


    //c = color(0, 0, 0);
    xPos = random(0, width);
    yPos = height/2;
    ySpeed = 2; //tempYSpeed// take away the # value to change the speed
    acc = random(0.05, 0.3);
  }

  void display() { //can add aggument to change the color
    noStroke();//

   image(straw1, xPos, yPos, 100, 100);
   //image(straw2, xPos, yPos, 50, 50);

    }

  void move() {
    ySpeed += acc;
    yPos += ySpeed;
    
    if ((yPos >= height) || (yPos == 0)) {
      ySpeed *= -1;
      
     
      
    }
  }
}


No comments:

Post a Comment