Schedule‎ > ‎

Lab 19: An Elevator


YouTube Video

Objectives

  • Learn to control a servo motor
  • Use a sorting algorithm to do work

Academic Honesty

Read the Scholastic Honesty Policy and Assignment Integrity policies of the syllabus. Here are some clarifications for this particular assignment:

  • You are expected to work with your Pair Programming Partner.
  • You may NOT give a copy of your code to anyone else. 
  • You must breadboard the circuits with your pair programming partner.
  • You may NOT look at another student's code until you complete and submit this assignment.
  • You may get help if you get stuck, but only if they do not show or tell you the code to type

Project A: A Sprinkler (sprinkler.ino)  5 pts.

In this project you'll connect the servo using the circuit pictured to the right. When you're done with Project A you should have confidence that your servo is connected properly and you can control it. 

Put the single arm on your servo motor.

Just like the dance your sprinkler has two phases, fast sweep and slow sweep. Start with basic code for the servo library:

#include <Servo.h> 

int servoPin = 9; 
Servo myservo;  // create servo object to control a servo  
int pos = 0;    // variable to store the servo position 
 
void setup() 
  myservo.attach(servoPin);  // attaches the servo on pin 9 to the servo object 
 
void loop() 
  for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 

The above code moves the servo smoothly. A sprinkler moves in jerks. 

Specifications
  • The servo should do both the fast sweep and the slow sweep. 
  • The slow sweep should
    • Stop at positions: 0, 20, 40, 60, 80, 100, 120, 140, 160, 180 
    • Pause for 300 milliseconds between positions
  • The fast sweep should
    • Stop at positions: 170, 160, 150, 140, 130, 120, 110, 100, 90, 80, 70, 60, 50, 40, 30, 20, 10
    • Pause for 100 milliseconds between positions
  • Submit this part in a file called Sprinkler.ino

Project B: A Servo Motor Elevator (elevator.ino)  25pts

When you press the call button on an elevator the floor number you are on is stored in a list. The elevator has to keep track of the list so it doesn't leave people behind, but it doesn't visit the floors in the order the call button was pushed. That would be terribly inefficient because the elevator would pass people who are waiting. Instead, the elevator does work in order of floors. In this part you will make your servo work like an elevator. Start with the following code: 

#include <ArduinoSTL.h>
#include <Servo.h>
using namespace std;
int servoPin = 9;
Servo myservo;  // create servo object to control a servo
int pos = 0;    // variable to store the servo position
//Elevator myElevator(0, &myServo);     //create your instance of class Elevator here

void setup() {
  Serial.begin(9600);

  myservo.attach(servoPin);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  pos = 0;  //always start out at floor 1
  myservo.write(pos);   //You'll convert this to a method call to your object of Class Elevator
  cout << "Enter floor numbers (zero when you're done):" << endl;
  vector<int> todo;
  int floor;
  bool done = false;
  while (!done) {
    cin >> floor;
    if (floor != 0)  {
      todo.push_back(floor);
    }  else  {
      done = true;
    }
  }
  cout << "you entered: ";
  //printVector(todo);    //you will implement this function
  //put your code here and in separate functions and class methods to keep loop() small
}


The above code reads a list of integers. 

Specifications

  1. There are 10 floors in the building.
  2. A positive integer means "going up".  Ex.  4 means go up to floor 4
  3. A negative integer means "going down" Ex. -2 means go down to floor 2
  4. Your elevator always starts on the first floor 
  5. Your elevator travels to all of the "going up" destinations first 
  6. Then your elevator travels to all of the "going down" destinations.  -2 means you want to go down to floor 2.  To go down to floor 2,  the elevator must have gone to a floor higher than 2 (like floor 3).  So the largest positive number must be larger than the absolute value of the largest negative number.  3 > |-2|
  7. Stop for 1 second at each floor.
  8. See the pseudocode below.  You do not have to implement it this way.
  9. You must implement this using a Class Elevator (see a sample header below).
  10. Be sure that you write functions for modular parts of your code.  loop() should be reduced mostly to getting info from the user and function calls. 
  11. Turn in your code as Elevator.ino

Pseudocode for Elevator:


in loop()
    AFTER cout << "you entered: ";
    print out the vector of floors the user entered
    declare 2 new vectors:  up and down
    put the positive floors in vector up
    put the negative floors in vector down
    sort both vectors from smallest to largest (any sort you choose is ok)
    move the elevator up through the up vector settings
    move the elevator down through the down vector settings

class method Elevator::moveElevator(vector, up or down flag)
   loop through the vector elements
       if flag is up direction
          convert floor to degrees between 0 to 180 (Ex. 10th floor = 180deg and 1st floor = 0deg)
          myservo->write(degrees)
       if flag is down direction   
          get rid of negative numbers by multiplying each element by -1
          convert floor to degrees between 0 to 180
          myservo->write(degrees)


SAMPLE CLASS ELEVATOR HEADER FILE
#ifndef ELEVATOR_H
#define ELEVATOR_H
#include "Arduino.h"
#include <Servo.h>
#include <ArduinoSTL.h>
using namespace std;

class Elevator  {
  public:
    Elevator(int position = 0, Servo *servo = 0);
    void moveElevator(int position);  //move Elevator to one position
    void moveElevatorMultiple(vector<int> &data, bool up);
                    //move Elevator to multiple positions,  up can be true or false
    bool Elevator::checkFloors(vector<int> &dataup, vector<int> &datadown);
                //Extra Credit:  check to make sure the floor numbers make sense
  private:
    int _position;
    Servo* _myServo;
};
#endif

Extra Credit: 5 pts

 Add error checking to make sure that the "going down" numbers make sense. -2 means you want to go down to floor 2.  To go down to floor 2,  the elevator must have gone to a floor higher than 2 (like floor 3).  So the largest positive number must be larger than the absolute value of the largest negative number.  3 > |-2|. 

If this error has happened,  print "Your biggest negative floor must be less than your biggest positive floor,  please re-enter".  Then restart with the prompt for the user to enter floor numbers.

You also need to check to see if the user has entered a floor >10 or < -10 as the building only has 10 floors.

Grading Criteria (30 + 5 pts Extra Credit):  Part A 5pts   Part B 25pts + 5 pts Extra Credit



Comments