Evan Rust
Published © GPL3+

Candy Dispenser with Google Assistant

By using IFTTT, the Google Assistant API, and a Particle Photon, you can make a machine that gives out candy upon your request!

IntermediateFull instructions provided10 hours23,095

Things used in this project

Hardware components

Photon
Particle Photon
×1
Arduino Mega 2560
Arduino Mega 2560
×1
Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
Speaker: 3W, 4 ohms
Speaker: 3W, 4 ohms
×1
AIY Voice
Google AIY Voice
×1
Voice HAT for Raspberry Pi
×1
Mic Array
×1
Arcade Button and Micro Switch
×1
Servo (Continuous Rotation)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Male Header 40 Position 1 Row (0.1")
Male Header 40 Position 1 Row (0.1")
×1
Female Header 8 Position 1 Row (0.1")
Female Header 8 Position 1 Row (0.1")
×1
SparkFun APDS-9960
×1
Standard LCD - 16x2 White on Blue
Adafruit Standard LCD - 16x2 White on Blue
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×2

Software apps and online services

Arduino IDE
Arduino IDE
IFTTT Assistant Service
Particle Build Web IDE
Particle Build Web IDE
Assistant SDK
Google Assistant SDK

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

Auger

This is from Thingiverse for the Automated Cat Feeder: http://www.thingiverse.com/thing:27854 You will need a 1.5" T-joint PVC pipe as well.

Button Holder

This holds the actual button in place.

Button

This slides into the button holder piece. It is what the user presses. Make sure it can slide freely.

Switch Holder

It attaches on the side of the button holder and the tact switch is mounted on it.

Schematics

Schematic

Connect as-is. Sorry if it's messy.

Code

Arduino Mega Code

C/C++
Copy and Paste for the Arduino Mega.
#include <Wire.h>
#include <SparkFun_APDS9960.h>
#include <LiquidCrystal.h>
#include <Servo.h>

#define LARGE_AMOUNT 4000 //time to dispense candy in ms
#define SMALL_AMOUNT 2500

LiquidCrystal lcd(12,11,5,4,3,2);

Servo auger;

SparkFun_APDS9960 apds = SparkFun_APDS9960();

uint8_t proximity_data = 0;
int LB = 6;
int RB = 7;
bool LBS = false;
bool RBS = false;
int candy_amount = 0;

void setup(){
	if ( apds.init() ) {
    Serial.println(F("APDS-9960 initialization complete"));
  } else {
    Serial.println(F("Something went wrong during APDS-9960 init!"));
  }
  if ( !apds.setProximityGain(PGAIN_2X) ) {
    Serial.println(F("Something went wrong trying to set PGAIN"));
  }
  if ( apds.enableProximitySensor(false) ) {
    Serial.println(F("Proximity sensor is now running"));
  } else {
    Serial.println(F("Something went wrong during sensor init!"));
  }
  lcd.begin(16,2);
  lcd_reset();
  lcd.print("Initializing");
  auger.attach(9);
  pinMode(LB, INPUT);
  pinMode(RB, INPUT);
  Serial.begin(9600);
  delay(5000);
  lcd_reset();
  lcd.print("Running");
  delay(4000);
  lcd_reset();
	lcd.print("Google Powered");
  lcd.setCursor(0,1);
  lcd.print("Candy Dispenser");
  auger.write(90);
  auger.detach();
}

void loop(){
  int state = analogRead(A0);
  LBS = digitalRead(LB);
  RBS = digitalRead(RB);
  
	if (state > 60 || LBS == true || RBS == true){
  Serial.println("active");
  Serial.print(LBS);
  Serial.print(",");
  Serial.println(RBS);
  LBS = false;
  RBS = false;
  delay(1000);
  lcd_reset();
    lcd.print("How much candy?");
    lcd.setCursor(0,1);
    lcd.print("A Little   A LOT");
    while(LBS == false && RBS == false){
      LBS = digitalRead(LB);
      RBS = digitalRead(RB);
    }
    if(LBS){
      candy_amount = SMALL_AMOUNT;
    }
    else if(RBS){
      candy_amount = LARGE_AMOUNT;
    }
  while(proximity_data != 255){
  if ( !apds.readProximity(proximity_data) ) {
    Serial.println("Error reading proximity value");
  } else {
    
    lcd_reset();
    lcd.print("Waiting for cup");
    lcd.setCursor(0,1);
    lcd.print("Distance: ");
    lcd.print(proximity_data);
  }
  delay(400);
  }
  lcd_reset();
  lcd.print("Cup found!");
  delay(1000);
  
  for(int x=5;x>0;x--){
    if ( !apds.readProximity(proximity_data) ) {
    Serial.println("Error reading proximity value");
  } else {}
    if(proximity_data != 255){
      lcd_reset();
      lcd.print("Please replace");
      lcd.setCursor(0,1);
      lcd.print("the cup");
      delay(2000);
      while(proximity_data != 255){
  if ( !apds.readProximity(proximity_data) ) {
    Serial.println("Error reading proximity value");
  } else {
    
    lcd_reset();
    lcd.print("Waiting for cup");
    lcd.setCursor(0,1);
    lcd.print("Distance: ");
    lcd.print(proximity_data);
  }
  delay(400);
  }
  lcd_reset();
  lcd.print("Thanks for");
  lcd.setCursor(0,1);
  lcd.print("replacing it");
  delay(1000);
    }
    else{
    lcd_reset();
  lcd.print("Pouring in: ");
  lcd.print(x);
  delay(1000);
    }
  }
  lcd_reset();
  lcd.print("Pouring candy...");
    delay(1000);
    auger.attach(9);
		auger.write(20);
		delay(candy_amount);
		auger.write(90);
    auger.detach();
		
   lcd_reset();
   lcd.print("Enjoy!");
   delay(4000);
   lcd_reset();
 lcd.print("Google Powered");
  lcd.setCursor(0,1);
  lcd.print("Candy Dispenser");
  proximity_data = 8;
	}
}

void lcd_reset(){
	lcd.clear();
	lcd.setCursor(0,0);
}

Particle Photon Code

C/C++
Copy and Paste
#include "application.h""

int pin = 7;
char charBuf[50];
int out_pin = 5;

void setup() {
    pinMode(out_pin,OUTPUT);
    pinMode(pin, OUTPUT);
    Particle.function("give_candy", candy);
    Serial.begin(9600);
}

void loop() {

}

int candy(String command){
    command.toCharArray(charBuf, 49);
    char number[6];
    sprintf(number, "%c" "%c" "%c", charBuf[0],charBuf[1],charBuf[2]);
    int length1 = (int)number[0] - 48;
    int length2 = (int)number[1] - 48;
    int length3 = (int)number[2] - 48;
    
    int length = length1 * 100 + length2 * 10 + length3;
    digitalWrite(out_pin,HIGH);
    delay(100);
    digitalWrite(out_pin,LOW);
    
    for(int x=0; x<5; x++){
        digitalWrite(pin,HIGH);
        delay(length);
        digitalWrite(pin, LOW);
        delay(length);
    }
    return 0;
}

Credits

Evan Rust

Evan Rust

122 projects • 1087 followers
IoT, web, and embedded systems enthusiast. Contact me for product reviews or custom project requests.

Comments