Recent Changes - Search:

ENTER NEW DAILY DATA HERE:

PmWiki

pmwiki.org

edit SideBar

October2-ElmosMoveArmFunction

Created the moveArm function to move Elmo's arm with three simple arguments.

moveArm(armSensorcolor, direction, speed);

where:
color is either brown, red, yellow, or green
direction is either forward or reverse
speed is any number from 100 to 255 (100 = slowest, 255 = fastest)

Note on setting speed: as Elmo's battery dies, the slower speeds don't have enough juice to get the motor started. If you pick a speed that's too slow, you'll hear the motor trying to spin, but nothing is happening.

Most of the code is either declaring variables, or just comments. The heart of the program is the definition of the moveArm function, at the very end.

/*
AUTHOR: mike
DATE: Oct 01 2008
PURPOSE:
1. Flash an LED three times when this code is loaded onto the arduino.
2. Perform Elmo's Arm routine.
*/

//Verificatin Pin:
int ledPin = 13;

//Motor Output Pins:
int pwmPin = 3; // L293 pin 1 
int armMtrPosPin = 2; // L293 pin 2
int armMtrNegPin = 4; // L293 pin 7

//Motor Feedback Pins:
int armSensorRed = 7; //red wire
int armSensorBrown = 8; //white wire
int armSensorYellow = 9; //yellow wire
int armSensorGreen = 10; //grey wire

//Variables:
int forward=1;
int reverse=0;

void setup() {

  //Verificatin Pin:
  pinMode(ledPin, OUTPUT); //Set pin 13 as an output

  //Motor Output Pins:
  pinMode(pwmPin, OUTPUT); //Set pin 3 as an output
  pinMode(armMtrPosPin, OUTPUT); //Set pin 2 as an output
  pinMode(armMtrNegPin, OUTPUT); //Set pin 4 as an output
  digitalWrite(pwmPin, LOW); //Disable motor on startup
  digitalWrite(armMtrPosPin, HIGH); //Prepare for clockwise motor rotation
  digitalWrite(armMtrNegPin, LOW); //Prepare for clockwise motor rotation

  //Motor Feedback Pins:
  pinMode(armSensorRed, INPUT); //Set pin 7 as an input
  digitalWrite(armSensorRed, HIGH); //Enable internal pull-up resistor
  pinMode(armSensorBrown, INPUT);  //Set pin 8 as an input
  digitalWrite(armSensorBrown, HIGH);  //Enable internal pull-up resistor
  pinMode(armSensorYellow, INPUT);  //Set pin 9 as an input
  digitalWrite(armSensorYellow, HIGH);  //Enable internal pull-up resistor
  pinMode(armSensorGreen, INPUT);  //Set pin 10 as an input
  digitalWrite(armSensorGreen, HIGH);  //Enable internal pull-up resistor

  blink(ledPin,3,100);    //Call the blink routine, three quick blinks to show the code is loaded

//TO INITIALIZE ELMO'S ARM POSITION, MOVE HIS ARM TO GREEN
  moveArm(armSensorGreen, forward, 150); // Move Elmo's arm forward slowly
                     // until you reach armSensorGreen

//Elmo's Arm Routine:
// To use the "moveArm" function, you input three arguments:
// FIRST ARGUMENT - ANGULAR POSITION
// the first argument determines where Elmo's arm will move to.
// armSensorBrown = -20 degrees;
// armSensorRed = 0 degrees;
// armSensorYellow = 90 degrees;
// arm SensorGreen = 150 degrees;
// SECOND ARGUMENT - DIRECTION
// forward = move arm in positive angle direction;
// reverse = move arm in negative angle direction;
// Be careful that you tell Elmo the correct direction, for example:
// if the arm is currently stopped at red, and you want to move to
// yellow, the direction should be "forward."
// THIRD ARGUMENT - SPEED
// Speed is any number from 90 to 255 (255 is fastest). Depening
// on the health of the battery, 90 might be too slow to get the motor
// to move. If you pick a speed that is too slow, you'll hear the motor
// trying to spin.

// This little routine swings Elmo's arm back and forth twice,
// and repeats that pattern twice. Then it brings his arm
// all the way back, slowly, and all the way forward, slowly.

  int count;
  for (count=0;count<2;count++) {
    moveArm(armSensorRed, reverse, 255);
    moveArm(armSensorYellow, forward, 255);
    moveArm(armSensorRed, reverse, 255);
    moveArm(armSensorGreen, forward, 255);
  }
  moveArm(armSensorBrown, reverse, 150);
  delay(400);
  moveArm(armSensorGreen, forward, 180);
}

void loop() {
}

void blink(int whatPin, int howManyTimes, int milliSecs) {
  int i=0;
  for(i=0;i<howManyTimes;i++) {
    digitalWrite(whatPin, HIGH);
    delay(milliSecs/2);
    digitalWrite(whatPin, LOW);
    delay(milliSecs/2);
  }
}

void moveArm(int armSensorColor, int whichWay, int howFast) {
  //MOVE MOTOR IN DIRECTION OF "whichWay" (EITHER FORWARD OR REVERSE)
  int positivePin = whichWay;
  int negativePin = ~whichWay & 1;
  digitalWrite(armMtrPosPin, positivePin); 
  digitalWrite(armMtrNegPin, negativePin);
  analogWrite(pwmPin,howFast); // Turn motor on at speed "howFast"
  //WATCH FOR WHEN THE MOTOR REACHES THE NEXT TARGET SENSOR
  int sensor = 1;
  while(sensor==HIGH) {
  sensor = digitalRead(armSensorColor);
  }
  //AND WHEN ELMO'S ARM REACHES TARGET SENSOR, STOP:
  analogWrite(pwmPin,0);
}

One last thought. If Elmo's battery is really weak, but not so weak that it can't turn the motors, a bizarre thing happens: he starts to move, which drains the battery and pulls its voltage so low that the arduino shuts down, turning off the motor. As soon as the motor is off, the battery voltage shoots back up, turning the arduino back on and the program starts over again from the beginning.

To avoid wasting tons of batteries while working on Elmo, we can create our own 9V supply. We ordered an LM317 adjustable voltage regulator, and a heatsink (those motors probably suck a lot of current when they get stuck).

Edit - History - Print - Recent Changes - Search
Page last modified on October 03, 2008, at 06:31 PM