Month: November 2012

3D Demo

Simple 3D Cube Demo

import processing.opengl.*;

float inc = 0.1;

void setup() 
{
  noStroke();
  size(600, 600, OPENGL);
}

void draw() 
{
  background(0);
  lights();
  stroke(255,0,0);
  strokeWeight(5);
  fill(0,255,0);
  inc +=0.01;
  //pushMatrix();
  translate(width/2, height/2,-0);
  rotateX(inc);
  //rotateY(inc);
   // rotateZ(inc);
  //  rotateZ(inc);
  box(150);
  //popMatrix();
}


import processing.opengl.*;

PFont font;
float incx = 0.1;
float incy = 0.1;
float incz = 0.1;

void setup() 
{
  noStroke();
  size(600, 600, OPENGL);
  smooth();
  font = createFont("Arial Bold",48);

  
}

void draw() 
{
  background(0);
  lights();
  stroke(255,0,0);
  strokeWeight(5);
  fill(0,255,0);
  incx +=0.01;
  pushMatrix();
  translate(width/2+100, height/2+100,0);
  
  //rotateX(inc);
  //rotateY(inc);
   rotateZ(incx);
 
  box(150);
  popMatrix();
  pushMatrix();
  
  stroke(255, 50);
  translate(200, 200, 0);
  //rotateX(incx);
  rotateY(incx);
  fill(mouseX * 2, 0, 160);
  sphereDetail(30);
  //sphereDetail(mouseX / 4);
  sphere(100);
  //println(frameRate);
  popMatrix();
  
  textFont(font,36);
  // white float frameRate
  fill(255);
  //text(frameRate,20,580);
  // gray int frameRate display:
  fill(200);
  text(int(frameRate),20,580);
  

}




Advertisements

Installing Processing

The main web site for processing is:

http://processing.org/

To down load the processing software to your PC or MAC – use

http://processing.org/download/

If you have a Windows PC – use the Windows 32-bit version.   You will then download a file call Processing -2.0x-windows32.zip.  If you have Windows 7 – this will be in your downloads folder

Create a folder call c:\dev – you can do this from the Windows Explorer

Now use right click on the processing zip file and extract all.  Then move the extracted folder to c:\dev

So you now should have:

c:\dev\processing-2.0b6

From your Windows explorer- it should look like this:

Now to run – just click on Processing.exe – If you have Windows 7 – you can pin this to the task bar.

 

 

 

 

 

 

 

 

 

 

Robot Processing

import guicomponents.*;

// Example by Tom Igoe

int delayTime=1000;
GWSlider sdr1, sdr2, sdr3, sdr4, sdr5, sdr6, sdr7;



import processing.serial.*;

// The serial port:
Serial myPort;       
void setup()
{
  
  size(600, 240); 
// List all the available serial ports:
println(Serial.list());

// Open the port you are using at the rate you want:
myPort = new Serial(this, Serial.list()[2], 115200);
  
// Send a capital A out the serial port:
myPort.clear();

  robotInit();

  sdr1 = new GWSlider(this,"blue18px",20,20,260);
  sdr1.setValueType(GWSlider.INTEGER);
  sdr1.setLimits(1500, 750, 2250);
  sdr1.setRenderValueLabel(false); 
  sdr1.tagNo=1;

  sdr2 = new GWSlider(this,"blue18px",20,80,260);
  sdr2.setValueType(GWSlider.INTEGER);
  sdr2.setLimits(1500, 750, 2250);
  sdr2.setRenderValueLabel(false); 

  sdr2.tagNo=2;

}



void robotInit()
{

  // Main lever
myPort.write("#4 P1000 T500\n");
myPort.write(13);
delay(500);

// Main Arm Raise - 1500 middle
myPort.write("#16 P1500 T500\n");
myPort.write(13);
delay(500);


// Gripper Rotate
myPort.write("#20 P750 T500\n");
myPort.write(13);
delay(500);

// Gripper
myPort.write("#24 P500 T500\n");
myPort.write(13);
delay(2000);
myPort.write("#24 P2250 T500\n");
myPort.write(13);
delay(500);


//GRiiper Lever
myPort.write("#28 P2250 T500\n");
myPort.write(13);
delay(2000);

// Main Rotate 500 - 2250
myPort.write("#0 P500 T500");
myPort.write(13);
delay(2000);

// Main Rotate 500 - 2250
myPort.write("#0 P2250 T500");
myPort.write(13);

}

void draw()

{
 
   background(200,200,255);
  
  
}

void handleSliderEvents(GSlider slider) {
  //println(slider.tagNo);
  //println("integer value:" + slider.getValue() + " float value:" + slider.getValuef());
  
  if ( slider.tagNo == 1 ) 
  {
   
    myPort.write("#0 P" +slider.getValue()+  "T500");
    myPort.write(13);
    //delay(500);

      
    
  }
  
  
  if ( slider.tagNo == 2 ) 
  {
   
    myPort.write("#24 P" +slider.getValue()+  "T500");
    myPort.write(13);
    //delay(500);

      
    
  }
  
  
}

Recap – Step By Step 15th November 2012

This is a recap app.. Shows variables and the IF statement working. Lets go through it and see whats going on when the code is running

// Variables

int count=0;
int p1x=50;
int p1y=50;
int lenx=450;
int leny=450;

void setup()
{

    size(500,500);
    count=0;
}
void draw()

{
  
    println("Hello I am in draw again this many times!!! " + count);
  
    count = count + 1;
    
    if (count > 500 )
    {
        count = 0;
  
    }    

    if (count > 0 && count < 100 ) 
    {
       fill(255,0,0);
       rect(p1x,p1y,lenx,leny); 
    }
  
    if (count > 100 && count < 200 ) 
    {
       fill(0,255,0);
       rect(p1x,p1y,lenx,leny); 
    }
  
    if (count > 200 && count < 300 ) 
    {
       fill(0,0,255);
        rect(p1x,p1y,lenx,leny);
    }
   if (count > 300 && count < 400 ) 
    {
       fill(255,255,255);
        rect(p1x,p1y,lenx,leny);
    }
   if (count > 400 && count < 500 ) 
    {
       fill(0,0,0);
        rect(p1x,p1y,lenx,leny);
    }
  
}

Inside a box

This week we started looking at variables and the if statement

We were looking at detecting if the cursor is inside a box. Very useful for things like detecting if the user clicks on a button

This bit of code uses both variables and the if

Lets say we have a box. We know the top left point and the bottom right point. We will call these (p1x,p1y) and (p2x,p2y)

  // Variables here
  int p1x=10
  int p1y=10;
  int p2x=100;
  int p2y=100;

  if (mouseX > p1x  && mouseX < p2x && mouseY > p1y &&  mouseY < p2y ) {
       // Do this code
       fill(255,0,0);
       rect(p1x,p1y,100,100);

  }

  else {

       // dark background here....
       fill(0,0,0);

  }

}

Assignment 3

This assignment will be the first start point in designing and writing your own applications ( apps )

It uses the basics of code, variables and the IF command. Once you get to understand how to use these basic code tools – you can write more complex apps.

The challenge is to change the colour of a shape like the Microsoft logo

 

Except that when you have the cursor go into any of the four squares it changes colour.

As a help I have used the IF command here. I have not used variables – but you can use variables for a position as well as a colour

 



// Variables

void setup()
{
  
 // set the size
  size(500,500); 
  
}
void draw()
{
 background(150);
  
  if (mouseX> 250  ) 
  {
    
    if ( mouseY > 250 )
    {
      
      fill(255,0,0);
      rect(250,250,250,250);
   
    }
    
  }
}

Circle Stopper

Nice the first app that has an if statement – draw a circle 100 times and then stops

int James_x;
int James_y;
int count;
void setup()
{
count=0;

size(800,800);
background(255,255,255);
James_x=250;
James_y=25;
}
void draw(){
// if (mousePressed){
//fill(250,0,0);
// }else
stroke(0);

count=count+1;
fill(0,500,500);
if(count < 100)
ellipse(mouseX,mouseY,100,100);

//fill(500,0,0);
//rect(mouseX,mouseY,50,500);
//fill(0,250,250);
//ellipse(mouseX,mouseY,500,50);
}