I guys, i´m trying to make an arduino based vending machine for my final year project, and i have 2 codes, but i can´t put them all in one. One code is for the Vending Machine and it uses an infrared sensor to detect the coin but i want to put an coin aceeptor so that it will be more realistic. The another is for the coin aceptor. Can you guys please give me advice? i´ve been trying to put them in the same code all afternoon
Vending machine code
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
#include <Servo.h>
LiquidCrystal lcd(27, 26, 25, 24, 23, 22); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)
Servo servo1, servo2, servo3, servo4; // DS04-NFC motors
#define coinDetector 9
#define button1 13
#define button2 12
#define button3 11
#define button4 10
int buttonPressed;
void setup() {
lcd.begin(16, 2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
servo1.attach(4);
servo2.attach(5);
servo3.attach(6);
servo4.attach(7);
pinMode(coinDetector, INPUT);
// Activating the digital pins pull up resistors
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
// Vertical starting position
void loop() {
// Print "Insert a coin!" on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Insert a coin!");
// Wait until a coin is detected
while (true) {
if (digitalRead(coinDetector) == LOW) { // If a coin is detected, exit the from the while loop
break;
}
}
delay(10);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select your item");
lcd.setCursor(0, 1);
lcd.print(" 1, 2, 3 or 4?");
// Wait until a button is pressed
while (true) {
if (digitalRead(button1) == LOW) {
buttonPressed = 1;
break;
}
if (digitalRead(button2) == LOW) {
buttonPressed = 2;
break;
}
if (digitalRead(button3) == LOW) {
buttonPressed = 3;
break;
}
if (digitalRead(button4) == LOW) {
buttonPressed = 4;
break;
}
}
// Print "Delivering..."
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Delivering...");
// Depending on the pressed button, move the carrier to that position and discharge the selected item
switch (buttonPressed) {
case 1:
servo1.writeMicroseconds(2000); // rotate servo
delay(950);
servo1.writeMicroseconds(1500); // stop
delay(500);
break;
case 2:
servo2.writeMicroseconds(2000); // rotate
delay(950);
servo2.writeMicroseconds(1500); // stop
delay(500);
break;
case 3:
servo3.writeMicroseconds(2000); // rotate
delay(950);
servo3.writeMicroseconds(1500); // stop
delay(500);
break;
case 4:
servo4.writeMicroseconds(2000); // rotate
delay(950);
servo4.writeMicroseconds(1500); // stop
delay(500)
break;
}
lcd.clear(); // Clears the display
lcd.setCursor(0, 0);
lcd.print("Item delivered!"); // Prints on the LCD
delay(2000);
}
Coin aceptor code:
//Mario's Ideas
//Controlling coin acceptor with Arduino
#include <Arduino.h>
#include <TM1637Display.h>
#include <EEPROM.h>
// Module connection pins (Digital Pins)
#define CLK 3
#define DIO 4
TM1637Display display(CLK, DIO);
// variable use to measuer the intervals inbetween impulses
int i=0;
// Number of impulses detected
int impulsCount=0;
// Sum of all the coins inseted
float total_amount=0;
void setup() {
// pinMode(2, INPUT_PULLUP);
Serial.begin(9600);
display.setBrightness(0x0f);
// Interrupt connected to PIN D2 executing IncomingImpuls function when signal goes from HIGH to LOW
attachInterrupt(0,incomingImpuls, FALLING);
EEPROM.get(0, total_amount);
display.clear();
}
void incomingImpuls()
{
impulsCount=impulsCount+1;
i=0;
}
void loop() {
i=i+1;
Serial.print("i=");
Serial.print(i);
Serial.print(" Impulses:");
Serial.print(impulsCount);
Serial.print(" Total:");
Serial.println(total_amount);
if (i>=30 and impulsCount==1){
total_amount=total_amount+2;
impulsCount=0;
EEPROM.put(0, total_amount);
}
if (i>=30 and impulsCount==2){
total_amount=total_amount+1;
impulsCount=0;
EEPROM.put(0, total_amount);
}
if (i>=30 and impulsCount==3){
total_amount=total_amount+0.5;
impulsCount=0;
EEPROM.put(0, total_amount);
}
if (i>=30 and impulsCount==4){
total_amount=total_amount+0.2;
impulsCount=0;
EEPROM.put(0, total_amount);
}
if (i>=30 and impulsCount==5){
total_amount=total_amount+0.1;
impulsCount=0;
EEPROM.put(0, total_amount);
}
if(total_amount<10) display.showNumberDecEx(total_amount10, 0b10000000, true, 2, 2);
else
display.showNumberDecEx(total_amount10, 0b00100000, false, 4, 0);
}
Sorry if the information i give was not enough, but i really need help