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

Week 5: Leave Me Alone Box- Toby and Dom

In the penultimate week of our arduino project, we actually managed to make a working model! By figuring out the correct code to turn the stepper motor a set amount of degrees, it turns the switch off and returns back into the box after doing so.

When first testing this sketch model, we found that the stepper motor didn’t have enough torque to flick the switch off. This is why we had to add an extension onto the switch to increase the moment force, making it easier to turn.

Below is a youtube link to the video of it working:

Here is the code we are currently using:

#include <Stepper.h>


const int switchPin = 2;
const int stepsPerRevolution = 500;
int switchState = 0;
int currentState = 0;
int lastState = 0;

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

void setup() {
 // put your setup code here, to run once:
 myStepper.setSpeed(36);
 Serial.begin(9600);
 pinMode(switchPin, INPUT);
}

void loop () {
 switchState = digitalRead(switchPin);
 //Serial.println(switchState);
 if (switchState == 1)
 {
 Serial.println("clockwise");
 myStepper.step(stepsPerRevolution);
 currentState = 1;
 lastState = 0;
 }
 if (switchState == 0)
 {
 //Serial.println("counterclockwise");
 myStepper.step(-stepsPerRevolution);
 currentState = 0;
 lastState = 1;
 }
 delay(500);

if (currentState == 0 && lastState == 0)
 {
 switchState == digitalRead(switchPin);
 }

}

Our next step is to make the arm stop after one rotation from turning the switch off to return into the box. We also need to combine the set up for the servo into the same arduino and breadboard as well as write up one script of code with cases for different actions.

Week 4: Leave Me Alone Box – Dom and Toby

This week we have been busy creating a sketch model of our box. We successfully setup and coded a servo so that it can open the lid of the box when the potentiometer is turned. The potentiometer allows the box lid to be raised different heights, but we will replace the potentiometer with code in our final model so that the only input is the switch.

This is the code we used:

#include <Servo.h>
Servo myservo;
int potpin = 0;
int val;
void setup()
{
myservo.attach(9);
}
void loop()
{
val = analogRead(potpin);

val = map(val, 0, 1023, 0, 100);

myservo.write(val);

delay(15);
}

This is the setup we used:

5U4XPK1HDL4xZZxd9_M-LYD0vXU-k5bOoMq4jnCRvovoVKXOT0zifN1PwjrPrNhjV1DyLKX2g6yFUcO207I58O8b-nSvoX4h-OHL87OsBX4Ic5fZ_T5dn3h2iwtkJ93DBjcWmzhQxE2aZtr3ULlqnZ07OxyqQOO0Hs_K94fl-ORRmbTtfR_0wVxXyrbteZRFKBGLpSxVsa2CX32WRKjp5U691VujW-r5CZ6FoJlC8ZfccC8wdaLeetxB5n39Xi4bRhpFfxoSau5k24-RJjUPtppvNiwhWTyLJkKuDAisxYzYGdcSktFuNV90-dKWfN8dw5KC-wUZFanqP4tvaieSMHDfuyPwpOPTOJSXp_LEbpXLkED4ha8La3GQmzgJ5P8n7JxEGKXKdlXMDB5mDCkOKDO4Hx52DxzCn8LJfbrFCtQN9P0T16KM-NI1Hn4R6dRxFVa4ZNvaJzb-6L62TEpLIt-r9y9xdYpehxB02mRuPrEO7vj1UgJPaxLc4A0sR5btVa-CXqMX3jcq-8dirNJRvnJt0eLJX-EqIfZpKmoNb8IQ1ewesRw3kfQvJZXYhHN3qRmuai-mQ9qJBUmF1x7gcy4FY41F24JQDucU-r7MsnIFfJ4l8TZz=s659-no.jpg

This is a YouTube video of it working:

Week 5; Dom Heyland, Toby Nalder

During our final week we have continued to work and refine our code for our array of lights which will illuminate once a light sensor is covered. Playing around with different input and output numbers we where able to achieve a smooth transition from dark to light which looked affective on our light display.

int ledPin = 9;
int potPin = 0;
int minLight = 0;
int maxLight = 600;
int val = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin, OUTPUT);
  pinMode(potPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(analogRead(potPin));
  val = analogRead(potPin);
  if (val > minLight && val < maxLight)
  {
    val = map (val, minLight, maxLight, 255, 0);
  }
  else
  {
    val = 0;
  }
  analogWrite(ledPin, val);
}

Each LED runs parallel and is control by a single pin. Once our light display had been soldered together, when testing it with the 5volt output each LED was lit. However some where brighter than others, as not all LED’s where the same.

 

img_0366

When it came to including the light sensor into the circuit , once covered not all lights where clearly lit up as shown.

img_0368

 

After trying to fix the code we found that the led step up was the problem due to not all the lights being the same. Because the LED’s we used had different voltage ratings, the resistors we calculated to use were too big to allow enough current through to properly light them up. We then replaced our original setup with a larger breadboard, with all the same LED’s. Using this gave no uncertainties with the connections through our soldering and conductive tape. The 33 ohm resistors are still slightly too large to achieve full brightness, and wouldn’t allow current to any more than 10 LED’s.

 

img_0374

 

img_0375

With our final model, we achieved a good firefly effect, where a field of LED’s hidden behind black fabric, illuminate when the light sensor is covered by a hand so it’s in the dark. The darker it is the brighter the LED’s are thanks to a PWM pin.

Future improvements we would make are to use less powerful resistors allowing more LED’s to shine brighter and to add extra light sensors so that the LED’s can illuminate in an array.

 

Week 4: Progression – Dom Heyland, Toby Nalder

At the stage in the project, we have set up our Arduino with a single light sensor and resistor. We wanted to fully understand how the sensor worked before moving on and adding in several LED Lights and multiple Sensors.

Using the torch on the iPhone and covering the sensor with a hand, we where able receive different readings from the sensor through the serial monitor.

Code used to test the sensor:

int potPin = 8;

void setup() {
  // put your setup code here, to run once:
pinMode (potPin, INPUT);
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
Serial.println(analogRead(potPin));
delay(1000);
}

Our readings varied from a range of 260-1010

img_3330

We then moved on to try and include a caterpillar into our circuit to control 8 LEDs through 3 outputs. After researching into how the caterpillar is used we then tried to replicate an existing code we found. This wasn’t successful as only 1 of the 8 LEDs blinked.

 

 

int latchPin = 5;
int clockPin = 6;
int dataPin = 4;
 
byte leds = 0;

void setup() 
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
}
 
void loop() 
{
  leds = 0;
  updateShiftRegister();
  delay(500);
  for (int i = 0; i < 8; i++)
  {
    bitSet(leds, i);
    updateShiftRegister();
    delay(500);
  }
}
 
void updateShiftRegister()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds);
   digitalWrite(latchPin, HIGH);
}

 

learn_arduino_fritzing1

image1-1

Towards the end of the session we managed to make a LED glow when it got dark (by covering up the light sensor). This was achieved using analog read and writes so that the LED changed brightness depending on the light in the sensor, and the range can easily be changed in the code. This is close to the end result we want.

Before the deadline next week we need to recreate this set up with multiple led’s connected to more light sensors to give the field of firefly’s effect.

Below is the code we used, the breadboard setup and a picture of it working.

int ledPin = 9;
int potPin = 0;
int minLight = 0;
int maxLight = 400;
int val = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin, OUTPUT);
  pinMode(potPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(analogRead(potPin));
  val = analogRead(potPin);
  if (val > minLight && val < maxLight)
  {
    val = map (val, minLight, maxLight, 255, 0);
  }
  else
  {
    val = 0;
  }
  analogWrite(ledPin, val);
}


Week 3: Firefly Project – Dom Heyland, Toby Nalder

Week 3 Report:

At this stage, we’ve learnt the function of using a button to switch a single LED on/off. In our last session the code began to get more challenging, we coded the Arduino to give different light output strengths, which where controlled by the amount of times the button was pressed.

Following this we also wired in a light sensor, which caused numbers on the serial monitor to rise or fall depending on how much light was getting into the sensor. This was useful research into how we are going to carry out our firefly project.

 

Light Sensor

 

 

Following on from this we have researched into possible hardware we can use to create the array of light when multiple sensors are covered.

Below is an example of a dot matrix we intent to use to replicate the light of firefly’s.

Dot Matrix

 

We look forward to learning more code in next weeks session and are also going to try and purchase the dot matrix and light sensors needed.