Week 6 Lab: Escape Room Box

Part 1: Make a Servo Class


#include <Servo.h>

class Sweeper{
    Servo servo;
    int servoPin;
    int startPos;
    int endPos;
    int pos; //varying position
    int increment; //how much to change per time
    int updateInterval;
    unsigned long previousMillis;

    public:
    Sweeper(int interval){
        updateInterval = interval;
        startPos = 25;
        pos = startPos;
        endPos = 170;
        increment = 1;

    }

    void Attach(int pin){
        servo.attach(pin);
    }

    void Detach(){
        servo.detach();
    }

    void Open(){
        if(millis() - previousMillis > updateInterval){
        previousMillis = millis();
        pos += increment;
        servo.write(pos);
        if((pos >= endPos || pos <= startPos)){
            increment = -increment;
        }
      }
    }
};

Sweeper boxServo(10);

void setup() {
    boxServo.Attach(9);
}

void loop() {
    boxServo.Open();
}

        
    

Part 2: Add the Button Class



#include <Servo.h>

class Button{
    int buttonPin;
    int buttonState;

    public:
    Button(int pin){
    buttonPin = pin;
    pinMode(buttonPin, INPUT_PULLUP);
        buttonState = HIGH;
    }

    int Update(){
    buttonState = digitalRead(buttonPin);
    return buttonState;
    }
};

class Sweeper{
    Servo servo;
    int servoPin;
    int startPos;
    int endPos;
    int pos; //varying position
    int increment; //how much to change per time
    int updateInterval;
    unsigned long previousMillis;
    
    public:
    Sweeper(int interval){
    updateInterval = interval;
    startPos = 25;
    pos = startPos;
    endPos = 170;
    increment = 1;

    }

    void Attach(int pin){
    servo.attach(pin);
    }

    void Detach(){
    servo.detach();
    }

    void Open(){
    if(millis() - previousMillis > updateInterval){
        previousMillis = millis();
        pos += increment;
        servo.write(pos);
        if((pos >= endPos || pos <= startPos)){
        increment = -increment;
        }
    }
    }
};
    
Sweeper boxServo(10);
Button button1(5);
Button button2(6);

void setup() {
    Serial.begin(9600);
    boxServo.Attach(9);
}

void loop() {
    Serial.print("Button 1: ");
    Serial.print(button1.Update());
    Serial.print(" Button 2: ");
    Serial.println(button2.Update());
    
    boxServo.Open();

}
            
    

Part 3: Let's Code the Buttons to Move the Servo



#include <Servo.h>
        
class Button{
  //Member Variables
  int buttonPin;
  int buttonState;

  //Constructor
  public:
  Button(int pin){
    buttonPin = pin;
    pinMode(buttonPin, INPUT_PULLUP);
    buttonState = HIGH;
  }

  int Update(){
    buttonState = digitalRead(buttonPin);
    return buttonState;
  }

};

class Sweeper{
  Servo servo;
  //Member variables
  int servoPin;
  int pos; //Varying position
  int startPos;
  int endPos;
  int increment;
  int updateInterval;
  unsigned long previousMillis;

  //Constructor
  public:
  Sweeper(int interval){
    startPos = 30;
    endPos = 170;
    pos = startPos;
    updateInterval = interval;
    increment = 1;
  }

  void Attach(int pin){
    servo.attach(pin);
    servo.write(startPos);
  }

  void Detach(){
    servo.detach();
  }

  void Open(){
    if(millis() - previousMillis > updateInterval){
      previousMillis = millis();
      pos += increment;
      servo.write(pos);
      if(pos >= endPos){
        increment = 0;
      }
    }
  }

};

Sweeper boxServo(10);
Button button1(5);
Button button2(6);

int success = 0;

void setup() {
  Serial.begin(9600);
  boxServo.Attach(9);
}

void loop() {
  Serial.print("Button 1: ");
  Serial.print(button1.Update());
  Serial.print(" Button 2: ");
  Serial.println(button2.Update());

  if(button1.Update() == 0 && button2.Update() == 0){
    success = 1;
  }
  if(success == 1){
    boxServo.Open();
  }
  
}