Pololu Motor Control Program

/*

This program was developed by Students at the IIT College of Architecture for the control of “Simocc: Blanket”, interactive installation spring 07.

*/

#include <SimpleMessageSystem.h>

/*
—- SimpleMessageSystem Example 1 —-
Control Arduino board functions with the following messages:

r a -> read analog pins
r d -> read digital pins
w d [pin] [value] -> write digital pin
w a [pin] [value] -> write analog pin

Base: Thomas Ouellet Fredericks
Additions: Alexandre Quessy

*/

//initialize polling
int xTrack = 0;
int yTrack = 0;

//delineation of zones in pixel coordinates
int zone_A_x1 = 0;
int zone_A_y1 = 0;
int zone_A_x2 = 250;
int zone_A_y2 = 175;

int zone_B_x1 = 125;
int zone_B_y1 = 0;
int zone_B_x2 = 352;
int zone_B_y2 = 175;

int zone_C_x1 = 0;
int zone_C_y1 = 150;
int zone_C_x2 = 250;
int zone_C_y2 = 288;

int zone_D_x1 = 150;
int zone_D_y1 = 125;
int zone_D_x2 = 352;
int zone_D_y2 = 288;

int zoneAactive = 0;
int zoneBactive = 0;
int zoneCactive = 0;
int zoneDactive = 0;

//motor and tracking variables

//pins
int motorA_a = 2;
int motorA_b = 3;
int motorB_a = 4;
int motorB_b = 5;
int motorC_a = 6;
int motorC_b = 7;
int motorD_a = 8;
int motorD_b = 9;

int motorAup = 3000;
int motorAdn = 2000;
int motorBup = 3000;
int motorBdn = 2000;
int motorCup = 3000;
int motorCdn = 2000;
int motorDup = 3000;
int motorDdn = 2000;

int motorAstate = 0;
int motorBstate = 0;
int motorCstate = 0;
int motorDstate = 0;

void poll() {
if (messageBuild() > 0) { // Checks to see if the message is complete and erases any previous messages
xTrack = messageGetInt();
yTrack = messageGetInt(); // Call the writepin function
}
}

void parseZones() {

if ( (xTrack > zone_A_x1 ) && (yTrack > zone_A_y1  ) && (xTrack < zone_A_x2 ) && (yTrack < zone_A_y2  ) ) {
zoneAactive = 1;
} else {
zoneAactive = 0;
}

if ( (xTrack > zone_B_x1 ) && (yTrack > zone_B_y1  ) && (xTrack < zone_B_x2 ) && (yTrack < zone_B_y2  ) ) {
zoneBactive = 1;
} else {
zoneBactive = 0;
}

if ( (xTrack > zone_C_x1 ) && (yTrack > zone_C_y1  ) && (xTrack < zone_C_x2 ) && (yTrack < zone_C_y2  ) ) {
zoneCactive = 1;
} else {
zoneCactive = 0;
}

if ( (xTrack > zone_D_x1 ) && (yTrack > zone_D_y1  ) && (xTrack < zone_D_x2 ) && (yTrack < zone_D_y2  ) ) {
zoneDactive = 1;
} else {
zoneDactive = 0;
}

}

void setup()
{

// The following command initiates the serial port at 9600 baud. Please note this is VERY SLOW!!!!!!
// I suggest you use higher speeds in your own code. You can go up to 115200 with the USB version, that’s 12x faster
pinMode(motorA_a,OUTPUT);
pinMode(motorA_b,OUTPUT);
pinMode(motorB_a,OUTPUT);
pinMode(motorB_a,OUTPUT);
pinMode(motorC_a,OUTPUT);
pinMode(motorC_a,OUTPUT);
pinMode(motorD_a,OUTPUT);
pinMode(motorD_a,OUTPUT);

Serial.begin(9600); //Baud set at 9600 for compatibility, CHANGE!

}

void loop () {

poll();
parseZones();

if (zoneAactive == 1) {

if (motorCstate == 0) {
digitalWrite(motorC_a, HIGH);
digitalWrite(motorC_b, LOW);
delay(motorCup);
digitalWrite(motorC_a, LOW);
digitalWrite(motorC_b, LOW);
motorCstate = 1;
}
} else {
if (motorCstate == 1) {
digitalWrite(motorC_a, LOW);
digitalWrite(motorC_b, HIGH);
delay (motorCdn);
digitalWrite(motorC_a, LOW);
digitalWrite(motorC_b, LOW);
motorCstate = 0;
}
}

if (zoneBactive == 1) {
if (motorDstate == 0) {
digitalWrite(motorD_a, HIGH);
digitalWrite(motorD_b, LOW);
delay (motorDup);
digitalWrite(motorD_a, LOW);
digitalWrite(motorD_b, LOW);
motorDstate = 1;
}
} else {
if (motorDstate == 1) {
digitalWrite(motorD_a, LOW);
digitalWrite(motorD_b, HIGH);
delay (motorDdn);
digitalWrite(motorD_a, LOW);
digitalWrite(motorD_b, LOW);
motorDstate = 0;
}
}

if (zoneCactive == 1) {
if (motorBstate == 0) {
digitalWrite(motorB_a, HIGH);
digitalWrite(motorB_b, LOW);
delay (motorBup);
digitalWrite(motorB_a, LOW);
digitalWrite(motorB_b, LOW);
motorBstate = 1;
}
} else  {
if (motorBstate == 1) {
digitalWrite(motorB_a, LOW);
digitalWrite(motorB_b, HIGH);
delay (motorBdn);
digitalWrite(motorB_a, LOW);
digitalWrite(motorB_b, LOW);
motorBstate = 0;
}
}

if (zoneDactive == 1) {
if (motorAstate == 0) {
digitalWrite(motorA_a, HIGH);
digitalWrite(motorA_b, LOW);
delay (motorAup);
digitalWrite(motorA_a, LOW);
digitalWrite(motorA_b, LOW);
motorAstate = 1;
}
} else {
if (motorAstate == 1) {
digitalWrite(motorA_a, LOW);
digitalWrite(motorA_b, HIGH);
delay (motorAdn);
digitalWrite(motorA_a, LOW);
digitalWrite(motorA_b, LOW);
motorAstate = 0;
}
}

}

One Response to Pololu Motor Control Program

  1. Pingback: Assignment 4 « arch497 - physical computing (IIT)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s