Loop ( How I fix it to remember button than when >= $2.50 it then spins motor

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
  }
}


 

it is not really clear to me what your code shall do.
The description above does not specify

  • what kind of motor?
  • connected to which IO-pins?
  • how many motors?
  • how to decide on what conditions which motor should spin?

So without detailed information the advice that I can give is reduced to:

define variables that hold whatever values you need to remember and write if-conditions that check for the conditions that must be matched to make the motors spin.

Your question is a bit unspecific
this is a preprogrammed text so some questions might not fit to your problem.
for good advice some more details should be provided by you

Posting these informations will speed up solving your problems and finishing your project

To save time this is a pre-defined text. If you already have answered some of the questions below tap on your shoulder saying "that's already done"

Describe what you want to do all in all and give an overview about your project

Describe your knowledge-level about programming

Describe your knowledge-level about electronics

Describe what measurement-equipement do you have

  • a digital multimeter?
  • an oscilloscope?
  • a logic analyser?

What EXACT type of microcontroller are you using?

What computer are you using? (Windows, Mac or something else)

If you have a hardware-problem upload a handdrawn schematic and / or upload pictures of the wiring

If you are using some additional library add a link to the library and its documentation

If you use additional hardware add a link to the datasheet and/or a link to where you have bought the hardware

if the software behaves different than you expect it

describe in normal words what you do expect the software to do and what behaviour do you see
A even more detailed description on how to get good help is here

best regards Stefan

i think you need to consider the following

  • do you want to accept case before a selection is made
  • do you want a button to cancel and return any collected case

in the code below, buttons are monitored using an internal function that handles debounce.

buttons are enumerated a a switch statement processes the buttons.

"select" tracks the selected item. it is also used to determine when to accept cash as well as when to check if there's a sufficient amount deposited.

one button is allocated for returning any deposited cash.

lcdDisp() deals with displaying strings.

// vending machine

#undef MyHW
#ifdef MyHW
# include "sim.h"
//                  return   select       coins
byte pinsBut [] = { A1,      A4, A4, A2,  A5, A5, A3, A5 }; // have 3 buttons
# define N_BUT   sizeof(pinsBut)

// ----------------------------------------
#else
# include <Servo.h>
# include <Wire.h>
# include <LiquidCrystal_I2C.h>
# include <mechButton.h>

byte pinsBut [] = {  1,  2,  3,  4,  5,  6,  7,    8 };
#endif

// -----------------------------------------------------------------------------
LiquidCrystal_I2C lcd (0x27, 16, 2);

byte servopins [] = {17, 18, 19, 20};
#define  NumberOfServos     sizeof(servopins)

// enum values below correspond to pinsBut [] above
enum              { R,  S0, S1, S2, C2, C1, C0_5, C0_2, NONE };
int select = NONE;


Servo servos [NumberOfServos];
int servodelays [NumberOfServos] = {1500, 100, 2450, 1300}; // DELAYS FOR EACH SERVO

float   cash;

char    s0 [20];
char    s1 [20];

enum { Off = HIGH, On = LOW };

// -----------------------------------------------------------------------------
void
reset (void)
{
    select = NONE;
    cash   = 0;
    digitalWrite (LED_BUILTIN, Off);
    lcdDisp ((char *)"Select option");
}

// -----------------------------------------------------------------------------
void
lcdDisp (
    char *s )
{
    lcd.init ();
    lcd.setBacklight (1);
    lcd.setCursor (1, 0);
    lcd.print (s);
}

// -----------------------------------------------------------------------------
byte butState [N_BUT];

int
chkButtons ()
{
    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        byte but = digitalRead (pinsBut [n]);

        if (butState [n] != but)  {
            butState [n] = but;

            delay (10);     // debounce

            if (On == but)
                return n;
        }
    }
    return -1;
}

// -----------------------------------------------------------------------------
void
monitorButtons (void)
{
    byte but = chkButtons ();

    switch (but)  {
    case R:
        cashDrop ();
        break;

    case S0:
    case S1:
    case S2:
        select = but;
        digitalWrite (LED_BUILTIN, On);
        break;

    case C2:
        cashUpdate (2.0);
        break;

    case C1:
        cashUpdate (1.0);
        break;

    case C0_5:
        cashUpdate (0.5);
        break;

    case C0_2:
        cashUpdate (0.2);
        break;
    }
}

// -----------------------------------------------------------------------------
void
cashDrop ()
{
    Serial.println (__func__);
    reset ();
}

// -----------------------------------------------------------------------------
void
cashUpdate (
    float  coin )
{
    if (NONE == select)
        return;

    cash += coin;

    dtostrf (cash, 4, 2, s1);
    sprintf (s0, "$%s", s1);
    lcdDisp (s0);

    digitalWrite (LED_BUILTIN, On);
    delay (200);
    digitalWrite (LED_BUILTIN, Off);
}

// -----------------------------------------------------------------------------
void
loop ()
{
    monitorButtons ();

    if (NONE != select && cash >= 2.50) {
        lcdDisp ((char *)"Dispense");
        servos [select].writeMicroseconds (1000);  // rotate
        delay (servodelays [select]);              // delay for that servo
        servos [select].writeMicroseconds (1500);  // stop

        reset ();
    }
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);

    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        pinMode (pinsBut [n], INPUT_PULLUP);
        butState [n] = digitalRead (pinsBut [n]);
    }

    digitalWrite (LED_BUILTIN, Off);
    pinMode      (LED_BUILTIN, OUTPUT);

    reset ();
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.