I need help with my code

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_amount
10, 0b00100000, false, 4, 0);
}

Sorry if the information i give was not enough, but i really need help

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Please post your sketch, using code tags when you do. This prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

Before asking questions:


If your sketch was generated with AI like ChatGPT or by someone else tell us at the beginning of your post.

  • We often ask questions before offering solutions.

Please respond with the appropriate information requested.


  • Questioners know what their hardware and software looks like but the volunteers here do not. You need to fully explain what’s not working, and how it is supposed to work.
  • Before asking your questions, please read all the posting guidelines, follow these guidelines when you post your questions.

Hardware

  • Schematic diagrams are the universal language of electronics.
  • We use schematics to communicate hardware design. All components and connections are illustrated.
  • As always, show us a good schematic of your proposed circuit. Hand drawn schematics are acceptable.
  • Show us several good image views of your actual wiring.
  • Give WEB links to your major components.

Software

  • For readability, place { and } on separate lines by themselves, place each line of code on a separate line.
  • In the Arduino IDE, use Ctrl T or CMD T to format your code, then copy the complete sketch.
  • Use the < CODE / > icon from the ‘posting menu’ to attach your copied sketch.


  • When you follow the posting guidelines, volunteers can be more effective.
  • When we have a schematic we can speak the schematic language in helping you.
  • More volunteers will help when you properly post your questions.
  • Do some research into: non blocking TIMERS and State Machine programming techniques.

  • Avoid using while() unless it quickly moves on. i.e. while() can block other normal code execution, making your sketch stall and be unresponsive.

If this is your final year project, and you still only copy/paste programs, and you do not know how to use the IDE errors to correct your copy/paste mistakes... why would you expect to receive a diploma? Take an incomplete and re-take the course, from the first day. You have learned nothing.

Program 1.

#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);
}

Program 2

//Mario's Ideas111
//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_amount, 0b10000000, true, 2, 2);
  else
    display.showNumberDecEx(total_amount, 0b00100000, false, 4, 0);
}

Works for me (for the impulse counter I used magic numbers).
Stop excusing yourself. No Arduino, no peace.
Screenshot from 2024-09-20 16-51-01

this is one of the first times i´ve use arduino. I am in an eletrician/industrial maintenence course and i need a project to finish highschool... my professor told to all of us that we should do something different that we never used, and try to learn. And i´m trying. I am already making a code regardless if its works, but you should really be thinking before you post some type of things on internet. Not that i was offended, it´s just a way to show your worth and not let anger or superiority take control on you. Peace dude, hope you´re alright (btw arduino ide doesn´t show any error, the only problem is that the code is not running as fast as it should, it´s taking a long time to do a full loop on the code.

and

makes me think you have combined two sketches, the combination compiles and runs, but slowly.

Please note how code looks when posted using code tags, then post your combined, compiles and works (slowly) sketch.

An easy way to post code is to press the <CODE/> button in the message composition window and do what it says

type or paste code here

Before you do, it would be good to use the IDE Auto Fomart tool on the code.

We need to see what you did to know where you went wrong. For instance thjs

  // Wait until a coin is detected
  while (true) {
    if (digitalRead(coinDetector) == LOW) { // If a coin is detected, exit the from the while loop
      break;
    }
  }

certainly can't be part of a well-functioning solution. How did you handle that show-stopper?

a7

The way the button is configured, the program (concatenation of the two) gets through without button interaction. Pin re-use throughout the concatentation.

You are the one facing a final project with copy/paste as your only tool. Talk to your instructor about waiting until the last moment to unload your burden on anyone but yourself.

Programming Electronics Academy released a video on combining sketches... to save all those words.

2 Likes

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