#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,16,2);
//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
}
}
}