Recent Changes - Search:

ENTER NEW DAILY DATA HERE:

PmWiki

pmwiki.org

edit SideBar

October1-Elmos1stRoutine

We successfully made Elmo's arm move back and forth, using each of the four arm sensor positions. Here is the code:

/* \\
AUTHOR: mike & soyoung \\
DATE: Sep 30 2008 \\
PURPOSE: \\
1. Flash an LED three times when this code is loaded onto the arduino.\\
2. Make Elmo's arm do a little routine:\\
   A. Don't do anything until someone puts Elmo's arm at 90 degrees (yellow).\\
   When his arm is at 90 degrees:\\
   B. Lift it up to 0 degrees (red), pause 400 milliseconds, then\\
   C. Lower it back to 90 degrees (yellow), pause 800 milliseconds, then\\
   D. Lift it up to -20 degrees (brown), then\\
   E. Lower it back to 150 degrees (green), and stop moving.\\
   F. Repeat from A.\\
*/\\

//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 red=1;\\
int brown=1;\\
int yellow=1;\\
int green=1;\\

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\\

}

void loop() {

  //A. WATCH FOR WHEN ELMO'S ARM IS AT 90 DEGREES:
  yellow = digitalRead(armSensorYellow);
  while(yellow==HIGH) {
  yellow = digitalRead(armSensorYellow);
  }
  //AND WHEN ELMO'S ARM REACHES 90 DEGREES, DO THIS:
  analogWrite(pwmPin,0); //turn motor off
  red = digitalRead(armSensorRed); // Read value of next target sensor
  //REVERSE MOTOR DIRECTION:
  digitalWrite(armMtrPosPin, LOW);
  digitalWrite(armMtrNegPin, HIGH);    
  analogWrite(pwmPin,210); //The number, 210, sets motor speed.
                             // You can pick any number from 100 (slowest)
                             // to 255 (fastest).

 //B. WATCH FOR WHEN ELMO'S ARM IS AT 0 DEGREES:
  while(red==HIGH) {
  red = digitalRead(armSensorRed);
  }
  //AND WHEN ELMO'S ARM REACHES 0 DEGREES, DO THIS:
  digitalWrite(pwmPin,LOW); //a different way of turning the motor off...
  yellow=digitalRead(armSensorYellow); // Read value of next target sensor
  //REVERSE MOTOR DIRECTION:
  digitalWrite(armMtrPosPin,HIGH); //~armMtrPosPin;
  digitalWrite(armMtrNegPin, LOW); //~armMtrNegPin;
  delay(400);
  analogWrite(pwmPin,180);

  //C. WATCH FOR WHEN ELMO'S ARM IS AT 90 DEGREES:
  while(yellow==HIGH) {
  yellow = digitalRead(armSensorYellow);
  }
  //AND WHEN ELMO'S ARM REACHES 90 DEGREES, DO THIS:
  analogWrite(pwmPin,0);
  brown = digitalRead(armSensorBrown); // Read value of next target sensor
  //REVERSE MOTOR DIRECTION:
    digitalWrite(armMtrPosPin, LOW); 
    digitalWrite(armMtrNegPin, HIGH);
    delay(800);
    digitalWrite(pwmPin,HIGH);


  //D. WATCH FOR WHEN ELMO'S ARM IS AT -20 DEGREES: 
  while(brown==HIGH) {
  brown = digitalRead(armSensorBrown);
  }
  //AND WHEN ELMO'S ARM REACHES -20 DEGREES, DO THIS:
  digitalWrite(pwmPin,LOW); // Turn motor off.
  green=digitalRead(armSensorGreen); // Read value of next target sensor
  //REVERSE MOTOR DIRECTION:
  digitalWrite(armMtrPosPin, HIGH);
  digitalWrite(armMtrNegPin, LOW);
  delay(100);
  analogWrite(pwmPin,255);

  //E. WATCH FOR WHEN ELMO'S ARM IS AT 150 DEGREES: 
  while(green==HIGH) {
  green = digitalRead(armSensorGreen);
  }
  //F. STOP THE MOTOR. ROUTINE IS DONE UNTIL SOMEONE MOVES
  //ELMO'S ARM BACK TO 90 DEGREES.
      digitalWrite(pwmPin,LOW);

}

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

The next goal is to make this code easier to edit by creating intuitive functions.

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