Help on very simple vending machine

Hey! So im trying to make a real simple vending machine looking program. So there need to be 2 buttons that count money, 1 button for 1 dollar and 1 button for 5 dollars. The soft drink costs 8 dollars. The amount of money is getting showed on a 7 segment display. When you count up to 8 dollars it need to stop adding up, you choose the drink with 2 push buttons, 1 for coca cola and 1 for fanta. You need to add up 8 dollars first before you can choose the drink. After you put in the money and choose the drink a green LED should light up and the whole thing should reset.

Interesting. What do you need?

I need help with the coding, the money counter should be easy, but i dont really know how to combine the rest together with choosing a drink, showing it on the 7 segment etc

I need help with the coding,

We will be happy to help. Please post the program that represents your best attempt and tell us in detail what it actually does and what you want it to do that is different. It will make it much easier to focus on the parts you need help with rather than wasting time on things that you can do.


Thanks man, but i dont really know how to start. I will get the money counter to work, but i dont know how to combine it with the 7 segment, how to choose the soda etc. Im all down to learn this, i got around 1 week to finish this project.

Welcome to the forum.

Please read the post at the start of any forum , entitled “How to use this Forum”.

You need to deal with your code in stages.
Can you tell us your electronics, programming, arduino, hardware experience?

What model Arduino are you using.

First write some code to recognise your money input buttons.
Then advance to add the count and add, using the Serial monitor to see your code working.
Then get your display to work, on its own, just counting up.
Then the two codes together so you can add your 5 and 1 dollars.
If you get each code working before combining you make sure you minimize the bugs that will inevitably occur.

It sounds a slow process but it is less frustrating and you get a better understanding of the code as you go.

Thanks… Tom… :slight_smile:

Hey, my experience is not great. Im using a Arduino Uno R3. I have done very very simple beginner codes, like using a button to light up a LED.

Hey, my experience is not great.

Then the first thing is to work on some other small projects to grow your experience. There is a wide range of example programs included with the Arduino IDE. They will probably cover most of the techniques you will need.


I see your point, the deadline is next friday. I’ll write the code for the money counter as best as i can, would you be able to help through the rest?

This is the task, its translated from norwegian: Coin toss should be simulated with pushbutton. It should count 1 dollar and 5 dollar separately. A 1-kroner push switch and a $ 5 push switch. • It should show the amount that is "put on" continuously at 7- segment. • When the correct amount is paid, this figure should be shown on the 7 segment. In addition, a green diode should be lit. • The number of the 7 segment should be displayed + the green diode should be lit until drink type is selected. 4 • The amount should not be changed even if the coin-throw button is pressed repeatedly. The machine does not receive more coins. • The price of the drink should be a variable that can be changed from 0 to 9. In this assignment the price should be USD 8. • The drink selector is a "button mechanism" that chooses between Cola, and Fanta. Use a push button (push switch) per day. choice. • The money must be paid before a soft drink type can be selected. • When the correct amount has been paid and a soft drink type is selected the should the trigger mechanism is indicated by a green diode which illuminate. The trigger should be active for about 10 seconds before the machine reset and again ready to receive coins. That is, it the green LED should light for 10 seconds and the amount displayed at 7- the segment must stand on for 10 seconds.

This is the counter i have for 1 button:
const int buttonPin = 2;

byte buttonPushCounter = 0;
byte buttonState = 1;
byte lastButtonState = 1;

void setup() {
pinMode(buttonPin, INPUT_PULLUP);

void loop() {

buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == LOW) {
Serial.print("Antall trykk: ");
lastButtonState = buttonState;



I dont know how to add a counter for the second button and how to set a value on it, it needs to count 5 and 5. Not 1 and 1.

I see your point, the deadline is next friday. I’ll write the code for the money counter as best as i can, would you be able to help through the rest?

That suggests to me that this is a school or college project and you are hoping we will write the program so you can get a good grade. If so, you will quickly find that the regulars here are not interested in helping people get grades by cheating.

If the deadline is on Friday 1st May, when was the project handed out?


Hey, i know how it looks like, and no, im not getting a grade for this. It was handed out last week, but online, today was my first day at school so i didnt get a chance to get all the buttons etc to wire this up. We in norway have a thing where 9 and 10th graders come over to see how the school is and what we work with, it some kind of "promotion" for our school. Everyone got their own task, and im stuck on this. So im not trying to "use" anyone to get a grade, i want the kids that come over to have a good experience. Im norwegian so my english may be bad. Either way, i re-did the counter, it looks like this now:

const int button1Pin = 2;
const int button2Pin = 3;

int button1PushCounter = 0;
int button1State = 1;
int lastButton1State = 1;
int button2PushCounter = 0;
int button2State = 1;
int lastButton2State = 1;
void setup () {

pinMode (button1Pin, INPUT_PULLUP);

pinMode (button2Pin, INPUT_PULLUP);


void loop () {

button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);

if (button1State != lastButton1State) {

if (button1State == LOW) {

Serial.print("Number of button1 pushes: ");

if (button2State != lastButton2State) {
if (button2State == LOW) {


Serial.print("Number of button2 pushes: ");


I cant figure out how to set a value on them. They keep adding 1 and 1, and they dont sum up. I need one of the buttons to have a value of 5 and the other one needs to have the value 1.


Please read the post at the start of any forum , entitled "How to use this Forum".

This will help to read your code on a forum post.

Thanks.. Tom.. :slight_smile:

So why not increment one of them by 5 instead of by one?


Change to
button2PushCounter = button2PushCounter + 5;

Your duplicate post in the Programming forum has been deleted. I can move the whole topic there if desired.

Yeah you can move it, thank you. And that was the answer i was looking for! Now i`ll need help with the 7 segment, i tried wiring it up 3 times now and it seems dead to me.

Common anode or common cathode display?
Do you have 7 resistors? 180 ohm, 220 ohm, 270 ohm, in that range.

Connect the common anode to +5. Connect a resistor to each segment and then to an Arduino pin. Writing a Low will turn on a segment.

Connect the common cathode to Gnd. Connect a resistor to each segment and then to an Arduino pin. Writing a High will turn on a segment.

I have both actually, and Yeah i got 220 Ohm resistors. I took a small break, i’ll try it out later.