#include <LiquidCrystal.h>
#include <Servo.h>
#include <Wire.h>
//Constants don't change
const int returnButton = 13; //Orange button to return coins
const int scrollButton = 8; //Pink button to scroll through options
const int selectButton = 12; //Putple button to select options
int lastScrollState = HIGH;
int cents = 0; //The amount of money the user put into the machine
int productNumber = 0;
LiquidCrystal lcd_1(7, 6, 5, 4, 3, 2);
Servo returnServo;
//Runs the motors that accept the dollars
void dollarMotors(){
Wire.beginTransmission(0x20);//adresse A1+A2+A3 masse
Wire.write(0x3F);
Wire.endTransmission();
delay(2000);
Serial.println("Accepted dollars");
Wire.beginTransmission(0x20);
Wire.write(0xFF);
Wire.endTransmission();
Serial.println("Dollar motors off");
delay(1000);
}
//Dispenses the product
void dispenseProduct(int productNumber){
byte motorNumber = 1 << productNumber;
Serial.print("Spinning motor");
Serial.println(productNumber);
Wire.beginTransmission(0x20); // PCF address
Wire.write(~motorNumber); // "~" inverts the bits
Wire.endTransmission();
delay(3000); // gives it time to turn
Wire.beginTransmission(0x20);
Wire.write(0xFF); // all motors off
Wire.endTransmission();
Serial.println("Done dispensing");
delay(2000);
}
//Lights the corresponding LED
void productLight(int productNumber){
byte ledNumber = 1 << productNumber;
Serial.print("LED on");
Serial.println(ledNumber);
Wire.beginTransmission(0x21); // PCF address
Wire.write(~ledNumber); // "~" inverts the bits
Wire.endTransmission();
}
void finishTransaction(){
dispenseProduct(productNumber - 1);
cents = cents - 200;
returnCoins();
reset();
}
//Allows the consumer to scroll through the options
//Then select the option & return change
void scroll(){
int scrollState = digitalRead(scrollButton); // read the button pin
if(scrollState != lastScrollState){ // see if it changed
lastScrollState = scrollState; // remember where it is
if(scrollState == LOW){ // if LOW it is pressed
Serial.println("Select pressed");
if(productNumber >= 6){
productNumber = 0;
productLight(productNumber);
}
Serial.print("Choice ");
Serial.println(productNumber);
lcd_1.clear();
lcd_1.setCursor(1, 0);
lcd_1.print("You've chosen:");
lcd_1.setCursor(4, 1);
lcd_1.print("Choice ");
lcd_1.setCursor(11, 1);
lcd_1.print(productNumber + 1);
productLight(productNumber);
Serial.println(productNumber);
productNumber++; // add 1 every time you press the button
//if(digitalRead(selectButton) == LOW){
//}
}
delay(20);
}
}
//resets the vending machine after the return button has been pressed
void reset(){
Wire.beginTransmission(0x21);
Wire.write(0xFF);
Wire.endTransmission();
lcd_1.clear();
lcd_1.setCursor(1, 0);
lcd_1.print("Please insert:");
lcd_1.setCursor(1, 1);
lcd_1.print("Dollar or Coin");
delay(100);
}
char* makePrice(int value){
static char msgPrice[8];
int dollarVal = value / 100;
int centVal = value % 100;
snprintf(msgPrice, 8, "$%d.%02d ", dollarVal, centVal);
char* msgPointer = msgPrice;
return msgPointer;
}
void countMoney(){
lcd_1.clear();
lcd_1.setCursor(0,0);
lcd_1.print("You've inserted:");
lcd_1.setCursor(0,1);
lcd_1.print(makePrice(cents));
delay(500);
}
//This function runs anytime the orange return button is pressed
void returnCoins(){
if(cents > 0){
lcd_1.clear();
lcd_1.setCursor(2,0);
lcd_1.print("Returning $$");
while(cents != 0){
returnServo.write(90);
delay(500);
returnServo.write(0);
delay(500);
cents = cents - 25;
}
reset();
delay(100);
}
}
void setup(){
Serial.begin(9600);
Wire.begin();
lcd_1.begin(16, 2);
reset();
returnServo.write(0);
returnServo.attach(9);
pinMode(13,INPUT_PULLUP);
pinMode(12,INPUT_PULLUP);
pinMode(8,INPUT_PULLUP);
}
void loop(){
scroll();
if(digitalRead(selectButton) == LOW && cents >= 200){
finishTransaction();
}
if(digitalRead(returnButton) == LOW){
Serial.println("Returning coins...");
returnCoins();
Serial.println("coins returned");
}
if(analogRead(A1) < 500){
dollarMotors();
cents = cents + 100;
Serial.println("added 1.00");
countMoney();
}
if(analogRead(A0) < 500){
cents = cents + 25;
Serial.println("added .25");
countMoney();
delay(100);
}
}