Wednesday, September 3, 2014

Big Egg + Arduino +IR sensor = EasterEgguino [Full Tutorial]




I was looking for a way to use a servo and an IR sensor and came up with this project.

The egg contains candy and when a candy seeking person gets close enough, the LEDs will chase and the top part of the egg opens. It remains open for about 3 seconds and then closes.

I used an Ardweeny and all the wiring is just bread boarded instead of soldering everything together. I may yet do that .



Step 1: Stuff needed


The main items used are:
1. Ardweeny   -   a small Arduino compatible Microcontroller from Solarbotics ..  This is inexpensive but you are required to solder the pieces to the included PCB.
2. A small hobby servo - I used a Futaba GWS03n
3. an IR sensor - I used a Sharp GP2Y0A -
4. you will need the cable that goes to the sensor also
5. 8 Leds
6. 74HC595 shift register to do the LED chasing.
7. 7805 voltage regulator
8. AC adapter to provide the 9 volt current required
9. A big egg(and some smaller ones) - from the Hobby Lobby.

Also
1. hot glue
2. some decorative items - the ears,eyes from a hobby shop or a toy store
3. wire, soldering iron


Step 2: Build the egg part


1.   hot glued 3 of the smaller eggs to the bottom to form a base.  Using tape to hold in place worked well.

2.    added a hinge to attach the top part to the bottom part.

3. Cut a piece of scrap wood to fit inside the egg. This is screwed to the egg and will be used to mount the servo.


4. glue a smaller piece of wood to the semi-circular piece.  Attach the servo to the smaller piece with a zip tie.

5. cut another scrap piece of wood into a circle which fits nicely near the bottom of the egg.  The bread board and battery will be mounted on this.  Hot glue this in place .

6. Make an arm for the servo.  This will depend on where you place the servo, size of egg.  I cut a pice out of an old CD case and spray painted it black.

7. attached the IR sensor to what would become the front .

8. need to cut a piece which fits in the egg but just a couple of inches down.  This becomes the shelf upon which the candy will be placed.

Step 3: The electronics part

1. The Ardweeny

This comes as a kit from Solarbotics. It includes instructions on how to assemble.

Once assembled it can be used right on the bread board which is what I did.

Connections that need to be made to the Arduino are:
IR sensor
black wire to gnd
red wire to 5v
yellow wire to analog pin 0

Servo
- I have tried to power the servo in various ways but it seems it needs its own power so I
provided 3 AA batteries for power
black wire to gnd
red wire to batteries
white wire to digital pin 9 on ardweeny

74HC595 shift register
- I wanted to have the LEDs chase but did not know how to do this. I found the following
http://www.arduino.cc/en/Tutorial/ShiftOut

I followed the instructions there and it worked.

I must admit I am not completely sure how it works but it does work.

Some other issues:
- had to play around with the servo locations to get it to open but not open too far or not enough

- used a routine" millis"  to give the appearance of the chaser working continuously as the lid opens.
(first video was before this refinement and you will see the chasing does not work but once)

- I power the Ardweeny with an AC adapter . I had hoped I could use it to avoid all batteries but when I try to include the servo , it just is not enough power to open the lid.   I do not know how long the batteries will last.

-




Step 4: The code

Below is the code I used :
I started with the servo sweep example and added to it .

// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.
// modified 4/11 c. dubois
// Made a candy server from a big plastic egg
// idea:
// IR sensor will tell when a hand reaches for the egg and will
// signal servo to open the egg.
// shift register is used to do a LED chase when the egg opens
//
// updates
// v3 - 4/24/11  add routines to get LEDS to  chase continually


#include <Servo.h>
// distance sensor
 int sensePin = 0;

// Servo
 Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created
int pos = 0;    // variable to store the servo position

// 74HC595 shift register
 //Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;


// other variables
int ledToDisplay;
int posServo;

long previousMillis = 0;        // will store last time Chase  was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)



void setup()
{
   //set pins to output so you can control the shift register
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

//   Serial.begin(9600);

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






void loop()
{

  // read the sensor
  // if getting close , thwn want to open the egg and start the leds.
  //  Leds will continue to chase until egg closes

 
  int val = analogRead(sensePin);
//  Serial.println(val, DEC);

//   map the val to a servo position - pos
// int val = analogRead(0);
//   pos = map(val, 0, 1023, 0, 100);

// open egg and turn on LEDs if close
  if(val > 225)
  {

 

  // 2. open egg
  posServo = 0;
   for(posServo = 0; posServo < 24; posServo += 1)  // goes from 0 degrees to 65 degrees
  { 

     // keep chase going
  runChase();

  // move servo
       myservo.write(posServo);              //  open top, wait 4 secs, close top and wait 5 secs
     delay(25);
  }


 // run a loop for 3 seconds to avoid using delay

 for (long xx=0; xx <= 200000; xx++){
     // keep chase going
  runChase();
 
 }


    // close the lid
     for(posServo = 24; posServo >=0; posServo -= 1)  // goes from 0 degrees to 65 degrees
  {

   
       // keep chase going
  runChase();


  // move servo
    myservo.write(posServo);       // then can read again
    
    delay(25);
        }
  }
  
  else
  {
    myservo.write(0);              // tell servo to go to position in variable 'pos'
    delay(25);

  }



}







// my Functions



void ledChase ()
{
   // 1. chase leds
  
     for (int n = 1; n < 9;   n++ ) {
        ledToDisplay = pow(2,n) ;
    // take the latchPin low so
    // the LEDs don't change while you're sending in bits:
 //   Serial.println(n);
 //   Serial.println(ledToDisplay);
 //    delay(1500);
  
    digitalWrite(latchPin, LOW);
    // shift out the bits:
    shiftOut(dataPin, clockPin, MSBFIRST, ledToDisplay);

    //take the latch pin high so the LEDs will light up:
    digitalWrite(latchPin, HIGH);
    // pause before next value:
    delay(50);
     }
  
    // now chase back in other direction
  for (int n = 1; n < 9;   n++ ) {
     ledToDisplay = pow(2,n) ;
    // take the latchPin low so
    // the LEDs don't change while you're sending in bits:
 //   Serial.println(n);
 //   Serial.println(ledToDisplay);
 //    delay(1500);
  
    digitalWrite(latchPin, LOW);
    // shift out the bits:
    shiftOut(dataPin, clockPin, LSBFIRST, ledToDisplay);

    //take the latch pin high so the LEDs will light up:
    digitalWrite(latchPin, HIGH);
    // pause before next value:
    delay(50);
  }

}

void runChase ()
{
   unsigned long currentMillis = millis();
   if(currentMillis - previousMillis > interval) {
  ledChase();
  previousMillis = currentMillis;
  }
}

Step 5: All done - another video


This shows the insides and the LED chase working.

0 comments:

Post a Comment