Assignment 11.2

Post date: Aug 17, 2010 3:9:4 AM

This one isn't very exciting, but I figured I'd better turn it in and get started on my final. It turns two motors one way when the cursor is on the white rectangle in the processing window and the other way when the cursor is on the red rectangle. Here are my codes:

Arduino:

int val; // set up variables

const int motor1Pin1 = 4;

const int motor1Pin2 = 3;

const int motor2Pin1 = 5;

const int motor2Pin2 = 6;

const int enable1Pin = 8;

const int enable2Pin = 9;

void setup() {

pinMode(motor1Pin1, OUTPUT); // set motor pins to output

pinMode(motor1Pin2, OUTPUT);

pinMode(motor2Pin1, OUTPUT);

pinMode(motor2Pin2, OUTPUT);

pinMode(enable1Pin, OUTPUT);

pinMode(enable2Pin, OUTPUT);

digitalWrite(enable1Pin, HIGH); // set enable pins to high

digitalWrite(enable2Pin, HIGH);

Serial.begin(9600); // begin serial communication at 9600 bps

}

void loop() {

if(Serial.available()) { // if data is available,

val = Serial.read(); // read it and store it in val

}

if (val == 1) { // if val is this,

digitalWrite(motor1Pin1, HIGH); // turn the motors one way

digitalWrite(motor1Pin2, LOW);

digitalWrite(motor2Pin1, HIGH);

digitalWrite(motor2Pin2, LOW);

}

if (val == 2) { // if val is this,

digitalWrite(motor1Pin1, LOW); // turn the motors the other way

digitalWrite(motor1Pin2, HIGH);

digitalWrite(motor2Pin1, LOW);

digitalWrite(motor2Pin2, HIGH);

}

}

Processing:

import processing.serial.*; //import the processing serial library

Serial myPort; // set up variables

int val;

void setup() {

size(200, 200); // set screen size

myPort = new Serial(this, Serial.list()[0], 9600); // begin serial communication at 9600 bps

background(0); // set background color

}

void draw() {

val = mouseX; // set val equal to the mouse position in the x plain

if(val <= 100) { // if val is less than or equal to 100,

myPort.write(1); // write 1 to the serial port

println(1); // and print 1

}

if(val >= 101) { // if val is greater than or equal to 101,

myPort.write(2); // write 2 to the serial port

println(2); // and print 2

}

stroke(255); // set stroke color

fill(255); // set fill color

rect(0, 0, width/2, height); // draw a rectangle equal to half the window

fill(225, 0, 0);

stroke(225, 0, 0);

rect(width/2, 0, width, height);

}

and here's a Fritzing pic of my circuit: