Arduino vending machine

Hello guys, I'm new to the forum and don't know if this post is in the right place, hope it is.

First of all, my english is not very good, so sorry for that.

I have to do a vending machine with arduino, I already have the different components and I'm trying the code I did but it doesn't work.

I will try to explain how it works (the vending machine):

First, the machine will show in the display a message (Press a boton). Then we press one of the botons (there are just 2). The display shows the money to insert. We insert it and then the product falls down. It only works that way.

Now I will try to explain the code:

First I put the setup for everything to work ok. The loop funciont just puts the first and last display messages, sets values for the next functions and tells them which product must fall down. The function dirua is the "main" function. It makes sure that the money inserted is correct for that product and if it is, that product will drop. Next the function zorroa, is the one that makes sure the coinselector takes the right pulses, dont really know if it has to have a while to always give an answer as I stated there in the code. Last I have two other functions, one for the coinselector to count the pulses, and other one to show messages in the display.

Feel free to ask any question.
Thanks in advance.

EDIT: When I load the program, the display shows to press a buton, I press it, and it stops in the message "Insert X". I insert that coin but it doesnt get it so I guess the error is in function zorroa (as the coinselector is working well).

Junto03.ino (2.92 KB)

  if (aux = true)

Whoops

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What model Arduino are you using?

Thanks.. Tom.. :slight_smile:

Hello and thanks for the quick responses.

Here goes the code:

#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
//#ifdef U8X8_HAVE_HW_I2C
//#include <Wire.h> Do I need it if my display runs SPI?
//#endif

U8G2_SH1106_128X64_NONAME_F_4W_HW_SPI u8g2(U8G2_R0,10,9,8);

const int coin=2;
volatile int pultsu_kopurua=0;

#include <Servo.h>
Servo ser1;
Servo ser2;

int botoia1=6;
int botoia2=7;

int txanpona1=0.5;
int txanpona2=1;
int txanpona3=2;
int prezio1=0.5;
int prezio2=2;

int prezioa=0;
int sartutakoa=0;
int txanpona=0;

bool aux=false;
bool aux1=false;
bool aux2=false;

void setup(){

pinMode(coin,INPUT_PULLUP);

Serial.begin (115200);

attachInterrupt(digitalPinToInterrupt(coin),pultsuak_zenbatu,FALLING);

u8g2.begin();

u8g2.setFont(u8g2_font_ncenB08_tr);

pinMode(botoia1,INPUT_PULLUP);
pinMode(botoia2,INPUT_PULLUP);

ser1.attach(4);
ser2.attach(5);

}

void loop(){
irudikatu("Sakatu botoia");
while(aux2==false){
int balioa1=digitalRead(botoia1);
int balioa2=digitalRead(botoia2);
if(balioa1==false){
prezioa=prezio1;
aux=true;
dirua();
aux2=true;
}
if(balioa2==false){
prezioa=prezio2;
dirua();
aux2=true;
}
}
irudikatu("Eskerrik asko");
delay(5000);
aux2=false;
}

void dirua(){
while (sartutakoa<prezioa){
irudikatu("X falta da"); // How can I make X to be : sartutakoa - txanpona?
zorroa();
sartutakoa=sartutakoa+txanpona;
aux1=false;
}
if(aux==true){
ser1.write(0);
delay(4000);
ser1.write(90);
}
else{
ser2.write(0);
delay(4000);
ser2.write(90);
}
sartutakoa=0;
txanpona=0;
aux=false;
}

void zorroa (){
while (aux1=false){ // Do I need this to always get a response of the function?
int pultsu_kopurua_aux=pultsu_kopurua;
delay(300);
if(pultsu_kopurua==pultsu_kopurua_aux){
if (pultsu_kopurua==1){
txanpona=txanpona1;
aux1=true; // Por lo de arriba
}
else if (pultsu_kopurua==10){
txanpona=txanpona2;
aux1=true; // Por lo de arriba
}
else if (pultsu_kopurua==20){
txanpona=txanpona3;
aux1=true; // Por lo de arriba
}
}
pultsu_kopurua=0;
}
}

void pultsuak_zenbatu(){
pultsu_kopurua++;
}

void irudikatu (char* katea){
u8g2.clearBuffer();
u8g2.drawStr(0,14,katea);
u8g2.sendBuffer();
}

Now I cant take the photo or draw it, as soon as I can, I will post it here. For now I can say that it is a display (with u8g2 library), coinselector, 2 servos and 2 buttons (to drop 2 products), and the arduino.

The arduino Im using is the MEGA.

Feel free to ask any question.

Thanks in advance!

  while (aux1=false){    // Do I need this to always get a response of the function?
  int pultsu_kopurua_aux=pultsu_kopurua;
  delay(300);
  if(pultsu_kopurua==pultsu_kopurua_aux){
    if (pultsu_kopurua==1){

You can't seem to decide whether to use one = or two, in if/while statements. Read up on the differences (again, if necessary).

Here goes the code:

If you have read this before posting a programming question then you obviously took no notice of it. Please make it eaiser to provide help by following the advice in the sticky