good day. im currently working on my 1st project, it is a vendo machine. and i am having a hard time on it because my && operator is being skipped and also when inserting coins to the coinslot it gives different value it is much less from the value it should be for example i inserted 10pesos the only value that it gives is 5 or 6
#include <Servo.h>
#define coinSlot 11 // coin slot pin
#define button1 3 // button 1 pin
#define button2 4 // button 2 pin
#define button3 5 // button 3 pin
#define button4 7 // button 6 pin
#define servoPin1 8 // servo 1 pin
#define servoPin2 9 // servo 2 pin
Servo servo1; // create servo 1 object
Servo servo2; // create servo 2 object
int coinCount = 0; // variable to store coin count
void setup() {
Serial.begin(9600);
servo1.attach(servoPin1); // attaches the servo 1 on pin 6
servo2.attach(servoPin2); // attaches the servo 2 on pin 7
pinMode(coinSlot, INPUT_PULLUP); // coin slot as input
pinMode(button1, INPUT_PULLUP); // button 1 as input
pinMode(button2, INPUT_PULLUP); // button 2 as input
pinMode(button3, INPUT_PULLUP); // button 3 as input
pinMode(button4, INPUT_PULLUP); // button 4 as input
}
void loop() {
if (digitalRead(coinSlot) < 1) { // if a coin is inserted
delay(30);
coinCount++; // increment coinCount
Serial.println(coinCount);
}
if ((digitalRead(button1) == HIGH) && (coinCount == 10)) { // if 10 coins are inserted and button 1 is pressed
delay(500);
servo1.write(180); // turn servo 1 to 90 degrees
coinCount = 0; // reset coinCount
}
if (coinCount == 20 && digitalRead(button2) < HIGH) { // if 20 coins are inserted and button 2 is pressed
servo2.write(180); // turn servo 2 to 90 degrees
coinCount = 0; // reset coinCount
}
if (coinCount == 50 && digitalRead(button3) < HIGH) { // if 50 coins are inserted and button 3 is pressed
servo1.write(90); // turn servo 1 to 90 degrees
servo2.write(90); // turn servo 2 to 90 degrees
delay(500); // wait for a second
servo2.write(90); // turn servo 2 to 90 degrees again
coinCount = 0; // reset coinCount
}
if (coinCount == 100 && digitalRead(button4) < HIGH) { // if 100 coins are inserted and button 2 is pressed
for (int i = 0; i < 5; i++) { // loop 5 times
servo2.write(90); // turn servo 2 to 90 degrees
delay(500); // wait for a second
}
coinCount = 0; // reset coinCount
}
}
are you sure your buttons are working correctly? how did you test them?
put some Serial.println() statement in your code to print variable and button values and show the flow of the program execution?
You have used INPUT_PULLUP in pinMode() which is a good idea but how are the button pins wired ?
To make your code more standard please change all of the tests for the result of digitalRead() to HIGH or LOW.
Print the value of all variables and digitalRead()s that are used in your tests immediately before you do the tests. Are the values what you expect ? For instance, does the value of coinCount increase when you insert coins ?