Sunteți pe pagina 1din 5

#include <Wire.

h>
#include <LiquidCrystal_I2C.h>
// Constants

bool userSelect = false;


const int coinPin = 2; // Defined as the receiving pin from the coin machine.
int coinState = 0; // Determine the status of coinPin
volatile int credits = 0; // Define a variable to display the credit.
int delayTime = 0; // Define delay variable for number of onionskin feed duration
int resetPin = 12; // Define reset pin for resetting after complete function
int sensorPin = A0; // select the input pin for LDR
int sensorValue = 0; // variable to store the value coming from the sensor
const int laserPin = 6;
const int shortSizeFeedTime = 5000; // Duration for 2pc OnionSkin to pass feeder
const int longSizeFeedTime = 7500; // Duration for 3pc OnionSkin to pass feeder
const int ledpin = 13; // Define LED Pin at pin13
const int ButtonS = 4; // Define button for short Size OnionSkin
const int ButtonL = 5; // Define button for long Size OnionSkin
const int shortSize = 6; // Define pin for motor pin shortSize
const int longSize = 7; // Define pin for motor pin longSize

//I2C pins declaration for LCD


LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

//CoinInterrupt counter function


void coinInterrupt() {

// Each time a pulse is sent from the coin acceptor, interrupt main loop to add 1 cent and flip on the
LED
credits += 1;
digitalWrite(ledpin, HIGH);
digitalWrite(ledpin, LOW);
}

//Long OnionSkin Feeder function


void longSkinServe() {
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("VENDING:");
lcd.setCursor(0, 1);
lcd.print("LONG ONION SKIN");
delayTime = credits * longSizeFeedTime;
digitalWrite(longSize, HIGH);
delay(delayTime);
digitalWrite(longSize, LOW);
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("THANK YOU!");
lcd.setCursor(0, 1);
lcd.print("PLS. COME AGAIN!");
delay(3000);
digitalWrite(resetPin, LOW);
credits = 0;
userSelect = false;
}

//Short OnionSkin Feeder function


void shortSkinServe() {
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("VENDING:");
lcd.setCursor(0, 1);
lcd.print("SHORT ONION SKIN");
delayTime = credits * shortSizeFeedTime;
digitalWrite(shortSize, HIGH);
delay(delayTime);
digitalWrite(shortSize, LOW);
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("THANK YOU!");
lcd.setCursor(0, 1);
lcd.print("PLS. COME AGAIN!");
delay(3000);
digitalWrite(resetPin, LOW);
credits = 0;
userSelect = false;
}

void setup() {
Serial.begin(9600); // Begin Serial Connection to PC
// set up the LCD's number of columns and rows:
lcd.begin(16, 2); // initialize the lcd for 16 chars 2 lines, turn on backlight
lcd.print("STARTING UP...");
attachInterrupt(digitalPinToInterrupt(coinPin), coinInterrupt, RISING);
pinMode(ledpin, OUTPUT);
pinMode(resetPin, OUTPUT); //Set Reset PIN as OUTPUT
pinMode(shortSize, OUTPUT);
pinMode(longSize, OUTPUT);
// pinMode(laserPin, OUTPUT); //Laser PIN
pinMode(ButtonS, INPUT_PULLUP);
pinMode(ButtonL, INPUT_PULLUP);
delay(2000);

// Main loop
void loop() {
if (credits == 0) {//not yet running, there is no credit yet
lcd.clear();
lcd.print("ONION SKIN VENDO");
lcd.setCursor(0, 1);
lcd.write("-[INSERT COINS]-");
delay(3000);
lcd.clear();
lcd.print("-[INSERT COINS]-");
lcd.setCursor(1, 1);
lcd.write("P1 or P5 ONLY");
Serial.println("No Coin Inserted Yet...Waiting");
delay (3000);

//---Wait for Coin/Credit-----------


while (credits > 0 && userSelect == false) { //if coin inserted stay in servo enable loop

lcd.clear(); //Clear command all LCD screen


lcd.print("CREDITS:"); //Display LCD Display Message
lcd.setCursor(9, 0); //Set the cursor's starting position at position(8,0)
lcd.print(credits); //Displays the value of variable "credit" on the LCD screen.
lcd.setCursor(12, 0); //Set the cursor's starting position at position(8,0)
lcd.print("PHP"); //Display screen text LCD
lcd.setCursor(0, 1);
lcd.print("PRESS TO PROCEED");
Serial.println("Credits:");
Serial.print(credits);
if (digitalRead(ButtonS) == HIGH || digitalRead(ButtonL) == HIGH) {

if (digitalRead(ButtonS) == LOW) {
//execute short size motor
userSelect = true;
shortSkinServe();
}
else if (digitalRead(ButtonL) == LOW)
{
userSelect = true;
longSkinServe();
}
}
delay (80); // for while loop LCD refresh speed control
}

}
CIRCUIT DIAGRAM
Arduino Vending Machine to monitor coin slot input while waiting for user input

I have this project called Onionskin Vending machine. The function should be: when a coin is inserted,
the pulses from the coin acceptor (which is not accurate sometimes so i didn't use attachInterrupt) is
recorded by the Arduino and is shown to the user as Credits. then while still waiting for coin input, it will
ask the user to select a product.

EX. Scenario 1: The user drops a 1cent coin, then the machine will show the user its credit as 1cents and
will ask to press a button. the user will press a button then the machine will turn on a motor for (X)
seconds to dispense product and say thank you then resets again.

EX. Scenario 2: The user drops a 5cent coin, then the machine will show the user its credit as 5cents and
will ask to press a button. then the user will drop again a 1cent coin so the machine will show its credit
as 6cents and will again ask to press a button. the user will press a button then the machine will turn on
a motor for (6X) seconds to dispense product and say thank you then resets again.

Here is the code I wrote so far, but the problem is when activated, it only records the first cent/pulse
then goes on waiting for user input to press a button, even if I drop more coins, it will not count the new
coins inserted, only always the 1cent / 1 Credit is displayed.

EDIT: There is no target credit, 1cents would do. but if user drop 2cents, then 2 Sets of product should
be dispensed upon user selection of product. So target is the arduino should wait for coin slot input
while waiting for user input at same time.

S-ar putea să vă placă și