Hada Amira
Published © GPL3+

Candy Dispenser

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

BeginnerFull instructions provided20 hours22,776
Candy Dispenser

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
servo (180degrees turn)
×1
Standard LCD - 16x2 White on Blue
Adafruit Standard LCD - 16x2 White on Blue
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
PHPoC Shield for Arduino
PHPoC Shield for Arduino
×1
IR Distance Sensor
×1
Switch Power Button
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Real Life Schematic

It looks a bit complicated, but follow as how the color wiring is.

Code

Candy Dispenser

Arduino
//------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");  
} 

Credits

Hada Amira

Hada Amira

0 projects • 5 followers

Comments