## Car Example

You can download the sketch here: http://aaron-sherwood.com/carExample.zip

Car sportsCar;
Car miniVan;

void setup(){
size(640, 480);
rectMode(CENTER);
sportsCar = new Car(255,0,0,100,15,15);
miniVan = new Car(150,50,200,150,112,2);
}

void draw(){
background(255);
sportsCar.display();
sportsCar.move(0);
miniVan.display();
miniVan.move(2);
}

class Car{
int speed;
int w;
int h;
int red, green, blue;
float x, y;
float angle;
int radius=200;

Car(int r, int g, int b, int W, int H, int s){
speed=s;
w=W;
h=H;
red=r;
green=g;
blue=b;
x = 0;
y = random(height);
angle=0;
radius=200;
}

void move(int mode){
x+=speed;
if (mode==0){
if(x>width || x<0){ speed*=-1; } } if (mode==1) { if (x>width){
x=0;
}
}

if (mode==2) {
x = width/2 + radius * cos(angle);
y = height/2 + radius * sin(angle);
angle+=.01;
}
}

void display(){
fill(red,green,blue);
rect(x, y, w, h);
}

}

## Motor Circuit

In this circuit the diode is wired in parallel with the motor. The voltage comes from 5V, goes into the motor, and the motor goes into the collector of the transistor (left pin). The diode goes from the ground side of the motor to the positive side of the motor, with the black stripe on the positive side. The emitter of the transistor goes to ground.
The schematic also has a resistor going into the base of the transistor, from the Arduino pin. This is ideal with our small transistor to limit the current a little to the base. It says 220 ohm resistor, but your 330 ohm resistor should be fine.