I'm making a vending machine. I want it to ask for your option on a lcd screen and then u select either 1 of the 4 buttons then you have to pay $2.50 in coins which goes down a sorter which has ir sensors that then calculates the total. Then it spins the correct motor. I have already done the setup up, but how do I make the loop do what I want it to do because right now it doesn't
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <mechButton.h>
LiquidCrystal_I2C lcd (0x27, 16, 2);
mechButton p1dollar(6);
mechButton p2dollar(5);
mechButton p20cent (7);
mechButton p50cent(8);
bool needUpdate;
float cash;
int buttonpins[4] = {1, 2, 3, 4}; // PINS FOR THE BUTTONS
int servopins[4] = {17, 18, 19, 20}; // PINS FOR THE SERVOS
int buttonlock = false; // lOCKED BUTTON LOGIC
int buttonPressed = 99; // PRESSED BUTTON LOGIC
int numberofservos = 4; // number of servos
int numberofswitches = 4; // number of switches
int servodelays[4] = {1500, 100, 2450, 1300}; // DELAYS FOR EACH SERVO
int i; // various loops variable
int ledpin = 6;
Servo servos[4]; // ARRAY OF SERVOS
void setup () {
lcd.init();
lcd.setBacklight(1);
lcd.setCursor(1, 0);
lcd.print("Select option");
Serial.begin(9600);
for (i = 0; i < numberofswitches; i++)
{
pinMode(buttonpins[i], INPUT_PULLUP); // LOOP AND SET PINS
}
for (i = 0; i < numberofservos; i++)
{
servos[i].attach(servopins[i]); // LOOP AND ATTATCH SERVOS
}
p1dollar.setCallback(dollarClick);
p2dollar.setCallback(twoDollarClick);
p20cent.setCallback(twentyCentClick);
p50cent.setCallback(fiftyCentClick);
needUpdate = false;
cash = 0;
}
void dollarClick(void) {
if (p1dollar.trueFalse()) {
cash = cash + 1;
needUpdate = true;
}
}
void twoDollarClick(void) {
if (p2dollar.trueFalse()) {
cash = cash + 2;
needUpdate = true;
}
}
void twentyCentClick(void) {
if (p20cent.trueFalse()) {
cash = cash + 0.2;
needUpdate = true;
}
}
void fiftyCentClick(void) {
if (p50cent.trueFalse()) {
cash = cash + 0.5;
needUpdate = true;
}
}
void loop () {
for (i = 0; i < numberofswitches; i++)
{
if ( digitalRead(buttonpins[i]) == LOW && !buttonlock) // a button was pressed
{
buttonlock = true; // a button was pressed,lock out the others
buttonPressed = i; // the button pressed
}
}
if ( buttonlock == true) // was a button pressed ?
idle(); // Runs the magic.
if (needUpdate) {
lcd.setCursor(1,1);
lcd.print("$");
lcd.print(cash);
needUpdate = false;
}
{
if(cash>= 2.50);
servos[buttonPressed].writeMicroseconds(1000); // rotate
delay(servodelays[buttonPressed]); // delay for that servo
servos[buttonPressed].writeMicroseconds(1500); // stop
buttonlock = false; // next button please
}
}