Project tutorial

Candy Dispenser © GPL3+

A simple candy dispenser to enjoy upon your request, as well as to practice Arduino and building skills!

  • 1,856 views
  • 3 comments
  • 10 respects

Components and supplies

Necessary tools and machines

09507 01
Soldering iron (generic)

Apps and online services

About this project

The Project

This is an easy way to create a candy dispenser using only IR sensor, servo, and pushbutton. It can be split up into these parts.

  • Making and using Arduino Uno R3
  • Coding and setting up the Arduino
  • Creating the casing and mechanical parts
  • Installing the electronics
  • Testing and usage

Here is a short demonstration video:

Writing the coding was the hardest part for me because I am still new to writing coding language but I manage to do it after multiple errors and help from Mr Google and friends. Writing codes is the longest time on this part of project.

Credit to the man who INSPIRED me to do this candy dispenser! This is the simplify project of .. https://www.hackster.io/gatoninja236/candy-dispenser-with-google-assistant-564724

Creating the Box and Mechanical Parts

This is fun part of the project to create. It took me at least 1 whole day to fully complete it after multiple errors and parts that don't fit.

Step 1

Lay Out Components

This might look very messy but this is the easiest way to sort out each components and test run each part to see if there's any error. I laid out different colors of wires so that its easier to look at. I laid out all components on a piece of cardboard and sort out which one goes first.

Step 2

Creating the Box

I draw a technical drawing of how the box should look like. Most of the parts are made out of Cardboard and some are made out of Acrylic. Then I measure each part.

  • Front & Back = 20cm x 16cm (Back is Acrylic)
  • Side - Side = 12cm x 16cm
  • Top & Bottom = 20cm x 12cm

Step 3

This is how it will look like after attach them all together (using masking tape first).

Then, I cut the components part:

  • LCD = 7.2cm x 2.5cm
  • Buttons = 1.2cm x 1.2cm

For the cup:

  • Length x Height = 9cm x 12cm
  • Width x Depth = 9cm x 9cm

Not to forget to cut out for the sensors too:

I made the dispenser into steps so that the candy doesn't fall right away:

Step 4

Laid out breadboard and Arduino behind each other. I use acrylics to stick them together. This way, it will save a lot of space.

Step 5

Start attaching the components on to the board. I start with LCD and buttons.

Next, after wired all the cables on the components, I attached them in the middle of the box.

Attaching the servo:

This is how it looks like from the back. I also add power button to power up the Dispenser using battery.

Step 6

The finish look.

Schematics

Real Life Schematic
It looks a bit complicated, but follow as how the color wiring is.
 mg 0642 g7ffe4mql9

Code

Candy DispenserArduino
//------LIBRARIES-----
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
//------LIBRARIES-----

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

Servo myservo;
int pos = 90; // servo start position
int buttonCLK = 7; //buttonClockwise pin
int buttonAntiCLK = 6; //buttonAntiClockwise pin
//int sensorPin = 2;             // IR sensor to be attached to digital pin NO.2
//int sensorState = 0;           // variable sensorState for storing statistics about the status of the sensor

int set = 0;
const int avoidPin = 5; //the ir obstacle sensor attach to pin 5

void setup()
{
  lcd.begin(); // initialize the LCD
  lcd.clear();
  
  myservo.attach(9); // Servo Digital 9
  pinMode(buttonCLK, INPUT);
  pinMode(buttonAntiCLK, INPUT);
  pinMode(avoidPin, INPUT); //set the avoidPin as INPUT
  
  Serial.begin(9600);  
  lcd.backlight(); // Turn on the blacklight
  lcd.setCursor(3,0);
  lcd.print("Welcome to");
  lcd.setCursor(3,1);
  lcd.print("Candy Land");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("How much candy?");
  lcd.setCursor(0,1);
  lcd.print("A Little  A Lot");
  delay(5000);

}

void loop()
{
  
  //----------THIS IS THE IF FUNCTIONS FOR BUTTONS-----------
  if(digitalRead(buttonCLK) == HIGH) // buttonClockwise is pressed
  {
    //TurnClockwise();
    //SensorDetection();
    lcd.clear();
    lcd.print("Please Place Cup");
    delay(2000);
    lcd.clear();
    set = set + 1;
    
  }

  

  if(digitalRead(buttonAntiCLK) == HIGH) // buttonAntiClockwise is pressed
  {
    //TurnAntiClockwise();
    //SensorDetection();
    lcd.clear();
    lcd.print("Please Place Cup");
    delay(2000);
    lcd.clear();
    set = set + 2;
  }

  if(set == 1)
  {
    IRCLK();
  }

  if(set == 2)
  {
    IRAntiCLK();
  }
  //----------THIS IS THE IF FUNCTIONS FOR BUTTONS-----------

  
}

void TurnClockwise()
{
  Serial.println("Candy 1 Being Dispensed");
  CandyText1(); //display Candy Type Text
  for(pos = 90; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
    {   
        myservo.write(pos);              // tell servo to go to position in variable 'pos'    
        delay(15);                       // waits 15ms for the servo to reach the position        
    }

    if(pos == 180)
    {
      delay(2000);
      ThankYouText();                  // display Thank You Text
    }
}

void TurnAntiClockwise()
{
  Serial.println("Candy 2 Being Dispensed");
  CandyText2(); //display Candy Type Text
  for(pos = 90; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
    {                              
        myservo.write(pos);              // tell servo to go to position in variable 'pos'  
        delay(20);                       // waits 15ms for the servo to reach the position        
    }
    if(pos == 180)
    {
      delay(3000);
      ThankYouText();                  // display Thank You Text
    }
  
}

void IRCLK()
{
  
  delay(1000); //create a delay of 1s
  boolean avoidVal = digitalRead(avoidPin); //read the value of pin4
  lcd.setCursor(0,0);
  lcd.print("Detecting Cup");
  lcd.setCursor(0,14);
  lcd.print("...");

  if(avoidVal == LOW) //if the value is LOW level
  {
    lcd.clear();
    lcd.print("Cup Placed");
    delay(2000);
    TurnClockwise();
  }
}

void IRAntiCLK()
{
  delay(1000); //create a delay of 1s
  boolean avoidVal = digitalRead(avoidPin); //read the value of pin4
  lcd.setCursor(0,0);
  lcd.print("Detecting Cup");
  lcd.setCursor(0,14);
  lcd.print("...");
  
  if(avoidVal == LOW) //if the value is LOW level
  {
    lcd.clear();
    lcd.print("Cup Placed");
    delay(2000);
    TurnAntiClockwise();
  }
  
}


void CandyText1()
{  
        lcd.clear();
        lcd.setCursor(4,0);
        lcd.print("Pouring");
        lcd.setCursor(1,1);
        lcd.print("Little Candy");
}

void CandyText2()
{  
        lcd.clear();
        lcd.setCursor(4,0);
        lcd.print("Pouring");
        lcd.setCursor(3,1);
        lcd.print("Lot Candy");
}

void ThankYouText()
{
        lcd.clear();
        lcd.setCursor(5,0);
        lcd.print("Thank");
        lcd.setCursor(6,1);
        lcd.print("You");
        delay(15);        
        software_Reset();
}

void software_Reset() // Restarts program from beginning but does not reset the peripherals and registers
{
  asm volatile ("jmp 0");  
} 

Comments

Similar projects you might like

Windows PC Lock/Unlock Using RFID

Project tutorial by Prasanth K S

  • 6,572 views
  • 6 comments
  • 29 respects

LED Cube

Project tutorial by Praditha Alwis

  • 298 views
  • 0 comments
  • 4 respects

Makers (Cubecon) #1 Infrared Transmit & Infrared Receive

Project in progress by Alpha

  • 119 views
  • 0 comments
  • 3 respects

Lie Detector

Project tutorial by Adaline Baskaran

  • 5,537 views
  • 5 comments
  • 16 respects

Arduino Keyboard

Project in progress by Gabriele Scordamaglia

  • 357 views
  • 2 comments
  • 4 respects
Add projectSign up / Login