Leave Me Alone Box Final – Toby Nalder, Dom Heyland

Over the last week we have worked on a number of things for our final working box.

We previously had the stepper motor and the servo connected to two different Arduino’s. When connecting both motors to the same Arduino we added an LED so that once the switch was pushed on, the LED would light up the inside of the box. When then later added a further 3 LED’s in a cold white color so that the box would really light up.

Firstly the construction of the box. After altering our models to ensure that each component was correctly placed, this allowed for the arm to work most efficiently.

Our switch had been very temperamental as the torque coming from the stepper motor was so low. To overcome this we needed a stronger force from the stepper motor. However when trying to increase this with code the motor stopped working. As a result of this we then attached a knob to extend the switch reduced the forced needed for the stepper arm.

After creating the first 3 cases last week so it turned itself off as normal and did nothing when the switch remained off, we wanted to add in more cases to make the box more exciting and further the user experience. The 2 other cases we created made the box lid peep open half way, close and then turn the switch off as normal. And the other case used the servo to aggressively shake the box lid a number of times, then waiting and finally turning the switch off as usual. To give the box an unpredictable nature, we coded it so that the 3 cases are selected randomly by the Arduino.

 

In this video is so each of the three cases are in order to clearly show what it does. However normally the box’s actions would be randomized to make it more fun and engaging.

 

This is another video of the box working in the dark, as you can see the 4 LED’s light up the box very effectively.

Flow Chart of Final Code;

flow chart

Our Final Working Code;

#include <Stepper.h>
#include <Servo.h>

int switchPin = 2;
int stepsPerRevolution = 500;
int switchState = 0;
int currentState = 0;
int lastState = 0;
int ledPin = 13;

int speedx = 2; //servo speed

int switchCase = 0;

int numOfShakes = 30;
int maxShakeAng = 90; //set ate close angle - but can be adjusted
int shakeSpeed = 60; // the more you increase the slower the shake

int countCase = 1; // for not randomizing

Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);
Servo myservo;

void setup() {
 myStepper.setSpeed(36);
 Serial.begin(9600);
 pinMode(switchPin, INPUT);
 pinMode(ledPin, OUTPUT);
 myservo.attach(6);


}

void loop() {
 switchState = digitalRead(switchPin);
 currentState = switchState;
 if (currentState == lastState) switchCase = 0;
 if (currentState == 1 && lastState == 0) switchCase = countCase;//random (1, 4); // choose from random case 1, 2 or 3
 if (currentState == 0 && lastState == 1) switchCase = 4;
 switch (switchCase)
 {

case 0: // do nothing

break;

case 1: // open box- arm rotates
 Serial.println(switchCase);
 digitalWrite (ledPin, HIGH);
 for (int o = 90; o >= 0; o -= speedx)// open lid
 {
 myservo.write(o);
 }
 stepsPerRevolution = 500;
 Serial.println("clockwise");
 myStepper.step(stepsPerRevolution);
 break;

case 2: // LED is on- open half- close half- case 1
 Serial.println(switchCase);
 digitalWrite (ledPin, HIGH);
 for (int oh = 35; oh >= 20; oh -= speedx)// open lid halfway
 {
 myservo.write(oh); delay(10);
 }
 for (int ch = 0; ch <= 90; ch += speedx)// close lid halfway
 {
 myservo.write(ch); delay(10);
 }
 for (int o = 90; o >= 0; o -= speedx)// open lid
 {
 myservo.write(o); delay(10);
 }
 stepsPerRevolution = 500;
 Serial.println("clockwise");
 myStepper.step(stepsPerRevolution);
 break;

case 3: // shake lid
 Serial.println(switchCase);
 digitalWrite (ledPin, HIGH);
 for (int i = 0; i < numOfShakes ; i++)// shake lid // generic for loop - runs for number of Shakes
 {
 int ranAng = random(maxShakeAng); // randomises angle between 0 to maxShakeAngle value
 Serial.println(ranAng); // shows random
 myservo.write(ranAng); //opens at a random angle
 delay(shakeSpeed);
 myservo.write(90); //close for a sec
 delay(shakeSpeed);
 }
 delay (2000); // angry pause
 for (int o = 90; o >= 0; o -= speedx)// open lid
 {
 myservo.write(o); delay(2);
 }
 stepsPerRevolution = 500;
 Serial.println("clockwise");
 myStepper.step(stepsPerRevolution);
 break;

case 4: // close Box - The switch is off - LED is off
 Serial.println(switchCase);
 digitalWrite (ledPin, LOW);
 stepsPerRevolution = 500;
 Serial.println("counterclockwise");
 myStepper.step(-stepsPerRevolution);
 for (int c = 0; c <= 90 ; c += speedx)// close lid
 {
 myservo.write(c);
 delay(10);
 }
 countCase++; // go to next open case
 break;
 }
 lastState = currentState;


if(countCase>3) countCase = 1; // reset counter
}

Fritzing Diagram:

FFFFFFFFReizing_bb

We are extremely pleased with the outcome of this project and have genuinely enjoyed doing it. Since the last project we have learnt lot’s more about coding and how there are endless possibilities for what Arduino’s can do.

This project has lot’s of potential for further development, by adding even more cases and inputs/outputs to make it do more actions. Ideas for the future would be to add wheels on the bottom for it to move around, and also proximity sensors so it could move away as your hand got near it.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s