Help with code for coin operated dispenser for school project please.

I am building a coin operated salt and pepper dispenser as a school project for our cafeteria. I am hoping to make a little cash and provide seasoning to our flavorless cafeteria food. Essentially how it works is that when a coin is put in the homemade coin-slot, a momentary pushbutton is activated. After the switch has been activated, there is two more momentary pushbuttons to choose from either salt or pepper.
If the salt button is activated, a dispensing wheel controlled by a servo will turn in a certain pattern, while if the pepper button is activated, the same dispensing wheel will turn in a different pattern, hence dispensing the appropriate seasoning.
I have written some code, but it is not working how I think it should. I want the code to say "if the coinPin(or coin detector switch) is pressed and if the pepperPin(pepper switch) is pressed, then turn the servo the allotted degrees" or "if the coinPin is pressed and if the saltPin is pressed, then turn the servo the allotted degrees." However, whenever the coinPin is pressed, the servo automatically turns, without waiting for either of the seasoning switches. Please help me with this code, as I feel it is a simple answer. Here is the code:

#include <Servo.h>

Servo myservo; // creating myservo object
int coinPin = 2; // one side of coinPin attached to pin 2 and 10K resistor to ground, while other is on +5V
int pepperPin = 3; // one side of pepperPin attached to pin 3 and 10K resistor to ground, while other is on +5V
int saltPin = 4;  // one side of saltPin attached to pin 4 and 10K resistor to ground, while other is on +5V
int servoPin = 11;  //servo attached to pin 11
int coinState = 0; // set coinState 
int pepperState = 0; // set pepperState
int saltState = 0; // set saltState

void setup()
{
myservo.attach(servoPin); 
pinMode(coinPin, INPUT); 
pinMode(pepperPin, INPUT);
pinMode(saltPin, INPUT);
}


void loop()
{coinState = digitalRead(coinPin); // read and save to the variable "coinState" the actual state of button
{pepperState = digitalRead(pepperPin); // read and save to the variable "pepperState" the actual state of button
{saltState = digitalRead(pepperPin); // read and save to the variable "saltState" the actual state of button
if(coinState == HIGH){
   if (pepperState == HIGH){
    delay(1000);
    myservo.write(50);
    delay(1000);
    myservo.write(111);
    delay(1000);
    myservo.write(90);
    }
}
if(coinState == HIGH){
    if (saltState == HIGH){
     delay (1000);
     myservo.write(111);
     delay(1000);
     myservo.write(50);
     delay(1000);
     myservo.write(90);
    }
 }
    }
}
}

Thankyou. icecats

Why are there { in front of your digitalReads?
Furthermore, I would think, you want to give the user a chance to push the button after inserting the coin and not to restrict him to do it simultaneously

As for why the servo turns without you pressing the button, since you're not using internal pull-ups, I can only guess that you don't have a pull-down resistor on your switches.

EDIT:
Also, you're assigning pepperPin value to both pepperState and saltState.

I am unsure about the reasoning for the { before the digital reads. For this code, I have copy and pasted snippets from all over the web, as well as writing a fair amount myself. What will the { do to the code? In addition, I was hoping "to give the user a chance to push the button after inserting the coin and not to restrict him to do it simultaneously." That was another coding problem that I was having. How might this be solved?
I believe that I am using 10k ohm pull down resistors from the switches to ground.
Thank you so much for your help.
icecats

Unnecessary {} can make your code not work the way you expect it if you put them at the wrong spot, not to mention they are a clutter.

You can go about many ways to give the user some time to push the button, but the simplest one I believe would be for the code, once a coin is detected, to enter another loop in which it continuously looks at the salt and pepper switches. You could later on add a time out period after which the code exits the loop and again waits for the coin. For that you'll need to take a look at the blinkWithoutDelay example.

It would help if you could post a schematic or a photo of your connections (or start using internal pull-ups to simplify the circuit). There is probably a mistake there.

I am a little confused about the whole concept of the blinkWithoutDelay and how to incorporate it into the code.
Here is a Fritzing image of my circuit. Thanks a bunch. icecats

The concept of blinkWithoutDelay is the same as when you cook an egg. You make a mental note of when the water started to boil and repeatedly check the time. If not enough time has passed you do nothing (or something completely unrelated) and check the time again. When enough time has passed you stop the cooking.
Nick Gammon has a good article about it on his page:

The schematic looks ok, apart from both red and black wires of the motor going to the 5V rail on the breadboard (I suppose that's an error in the schematic, and not the wiring?).
Note that it is generally not a good idea to power motors (or any other high current device) from the Arduino 5V pin.

Did you get rid of the unnecessary {}? Does the program now work correctly?

Hi, firstly are you using protoboard as in the fritz diagram?
If so, check that the pos and neg tracks that run down the length of the board are continuous the full length, some have a break in the middle and you end up with only one half length of track connected.
Also it is not advisable to power the servo off the 5V pin of the arduino, the servo can pull more current than the 5V pin is designed for, use another supply for it, remembering to connect the gnd of the servo supply to the gnd of the arduino.

Tom.... :slight_smile: