Recent Changes - Search:

ENTER NEW DAILY DATA HERE:

PmWiki

pmwiki.org

edit SideBar

October1-ArmFunctionAndMore

FUNCTION TO MOVE ELMO'S ARM (and some examples) 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)

/* AUTHOR: mike DATE: Oct 01 2008 PURPOSE: 1. Flash an LED three times when this code is loaded onto the arduino. 2. Perform some arm movements by calling the moveArm function.

  • /

//Verification Pin: int ledPin = 13; //On-board LED to verify when code is loaded onto Arduino.

//Motor Driver Control Pins: int pwmPin = 3; //Arduino Digital Pin 3 connects to

                // Motor Driver L293 pin 1 (driver enable).
                // Sending a PWM signal to the driver enable is how
                // we control speed. 

int armMtrPosPin = 2; //Arduino Digital Pin 2 connects to

                      // Motor Driver L293 pin 2 (driver 1 in).
                      // Driver 1 output connects to "top" of arm motor.

int armMtrNegPin = 4; //Arduino Digital Pin 4 connects to

                      // Motor Driver L293 pin 7 (driver 2 in).
                      // Driver 2 output connects to "bottom" of arm motor.

//Motor Sensor Feedback Pins: int armSensorRed = 7; //Arduino Digital Pin 7 connects to red wire.

                      // Red wire goes low when arm is at 0 degrees.

int armSensorBrown = 8; //Arduino Digital Pin 8 connects to brown wire.

                        // Brown wire goes low when arm is at -20 degrees.

int armSensorYellow = 9; //Arduino Digital Pin 9 connects to yellow wire.

                         // Yellow wire goes low when arm is at 90 degrees.

int armSensorGreen = 10; //Arduino Digital Pin 10 connects to green wire.

                         // Green wire goes low when arm is at 150 degrees.

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

void setup() {

  //Set INPUT and OUTPUT pins, and enable internal pull-up resistors:
  pinMode(ledPin, OUTPUT);
  pinMode(pwmPin, OUTPUT);
  pinMode(armMtrPosPin, OUTPUT);
  pinMode(armMtrNegPin, OUTPUT);
  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

  //Make sure motor is OFF:
  digitalWrite(pwmPin, LOW);

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

//************************************************************************ //START MOVING ELMO //FIRST, INITIALIZE ELMO'S ARM POSITION BY MOVING HIS ARM TO GREEN

  moveArm(armSensorGreen, forward, 130); //Move Elmo's arm forward, slowly,
                                         // until you reach armSensorGreen

  delay(2000); //Wait 2 seconds for audience's attention.

//NOW WE TELL ELMO WHAT ARM MOVEMENTS TO PERFORM

  moveArm(armSensorRed, reverse, 255);
  moveArm(armSensorYellow, forward, 200);
  moveArm(armSensorRed, reverse, 175);
  moveArm(armSensorGreen, forward, 150);
  moveArm(armSensorBrown, reverse, 120);
  delay(400);
  moveArm(armSensorGreen, forward, 100);

}

void loop() {

  //There is nothing to loop! All code is executed only once, at startup.

}

//******************************************************************* //FUNCTION DEFINITIONS:

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);

}

ARM MOTOR TESTS AND TWO ELMOS USING XBEE

SWITCH-CASE FUNCTION

int motor1Pin = 2; // H-bridge leg 1 int motor2Pin = 3; // H-bridge leg 2 int speedPin = 10; // H-bridge enable pin int armyellow = 6; int yellow=0; int armred = 5; int red = 0; int armgreen = 7; int green = 0; int armbrown = 8; int brown = 0; int switchpin = 9; int val=0; int flag=0;

int xbeeval=0;

void setup() {

  // set all the other pins you're using as outputs:
  Serial.begin(9600);
  pinMode(armyellow, INPUT);
  pinMode(armred, INPUT);
  pinMode(armgreen, INPUT);
  pinMode(armbrown, INPUT);

  digitalWrite(armyellow, HIGH);
  digitalWrite(armred, HIGH);
  digitalWrite(armgreen, HIGH);
  digitalWrite(armbrown, HIGH);

  pinMode(switchpin, INPUT);
  pinMode(motor1Pin, OUTPUT); 
  pinMode(motor2Pin, OUTPUT); 
  pinMode(speedPin, OUTPUT);

}

void loop() {

  // first turn motor one direction:
  yellow = digitalRead(armyellow);
  red = digitalRead(armred);
  green = digitalRead(armgreen);
  brown = digitalRead(armbrown);
  val=digitalRead(switchpin);

  if(Serial.available()){
    xbeeval = Serial.read();
    if(xbeeval=='H') {
      val=digitalRead(switchpin);

      switch(red){
      case 0:
        if(flag==1)
        { 
          break; 
        }
        else {
          digitalWrite(speedPin, HIGH);
          //   delay(100);
          //   digitalWrite(speedPin, LOW);
          //   delay(100);

          //   analogWrite(speedPin, 110);  
          //  digitalWrite(speedPin, HIGH);
          digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
          digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
        }
      case 1:
        break;
      }
  //    switch(yellow){
  //    case 0:
  //      digitalWrite(speedPin, HIGH);  
  //      digitalWrite(motor1Pin, HIGH);   // set leg 1 of the H-bridge low
  //      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
    //    flag=1;
  //    case 1:
  //      break;
  //    }
  //    switch(brown){
  //    case 0:
  //      digitalWrite(speedPin, LOW);
  //      flag=0;
  //    case 1:
  //      break;
  //    }

        switch(yellow){
      case 0:
        digitalWrite(speedPin, LOW);
        flag=0;
      case 1:
        break;
     }


    }
  }
  Serial.print(xbeeval);

}

WHILE FUNCTION

int motor1Pin = 2; // H-bridge leg 1 int motor2Pin = 3; // H-bridge leg 2 int speedPin = 4; // H-bridge enable pin int armyellow = 6; int yellow=0; int armred = 5; int red = 0; int armgreen = 7; int green = 0; int armbrown = 8; int brown = 0; int switchpin = 9; int val=0; int flag=0; void setup() {

  // set all the other pins you're using as outputs:
  pinMode(armyellow, INPUT);
  pinMode(armred, INPUT);
  pinMode(armgreen, INPUT);
  pinMode(armbrown, INPUT);

  pinMode(switchpin, INPUT);
  pinMode(motor1Pin, OUTPUT); 
  pinMode(motor2Pin, OUTPUT); 
  pinMode(speedPin, OUTPUT);

}

void loop() {

  // first turn motor one direction:
    yellow = digitalRead(armyellow);
     red = digitalRead(armred);
    green = digitalRead(armgreen);
    brown = digitalRead(armbrown);

while(red==0){ //if(flag==1) //{ break; } //else {

    digitalWrite(speedPin, HIGH);  
    digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
    digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high

//} }

while(yellow==0){

    digitalWrite(speedPin, HIGH);  
    digitalWrite(motor1Pin, HIGH);   // set leg 1 of the H-bridge low
    digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
    flag=1;

} while(brown==0){

    digitalWrite(speedPin, LOW);
    flag=0;

} }

CODE FOR MOTOR TEST

int motor1Pin = 2; // H-bridge leg 1 int motor2Pin = 3; // H-bridge leg 2 int speedPin = 8; // H-bridge enable pin int ledPin = 13; //LED

void setup() {

  // set all the other pins you're using as outputs:
  pinMode(motor1Pin, OUTPUT); 
  pinMode(motor2Pin, OUTPUT); 
  pinMode(speedPin, OUTPUT);
  pinMode(ledPin, OUTPUT);

   // blink the LED 3 times. This should happen only once.
  // if you see the LED blink three times, it means that the module
  // reset itself,. probably because the motor caused a brownout
  // or a short.
  blink(ledPin, 3, 100);

  // set speedPin high so that motor can turn on:
  digitalWrite(speedPin, HIGH); 

}

void loop() {

  // first turn motor one direction:
    digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
    digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
  // then wait 100msec, and turn the motor the other direction:
      delay(500);
    digitalWrite(motor2Pin, LOW); // Stop the motor
      delay(500);
    digitalWrite(motor1Pin, HIGH);   // set leg 1 of the H-bridge low
    digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
    delay(500);
  } 

/*

  blinks an LED
 */

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);
  }

}

Edit - History - Print - Recent Changes - Search
Page last modified on October 16, 2008, at 05:24 PM