Hello, I am having a hard time trying to figure out how to Serial.println only once and only the latest increment or decrement.
decrement
buttonState = digitalRead(button);
if (buttonState == 1 && pulse > 0 && userBalance) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dispensing Coins");
for (int i = pulse * 2; i > 0; i--) {
digitalWrite(relayPin, LOW); // turn on relay - active LOW.
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(i);
Serial.println(i); // i need to print only 1 since im going to send this to nodemcu and nodemcu to database
// Check coin hopper sensor status
if (digitalRead(coinHopper) == LOW) {
// Coin detected, decrease pulse count and delay before next dispensing
pulse--;
delay(255); // Adjust delay as needed
} else {
i++; // Skip this iteration // No coin detected, wait for coin
delay(30); // Adjust delay as needed
}
}
increment
coinSlotStatus = digitalRead(coinSlot);
delay(30);
if (coinSlotStatus == 0) {
userBalance = true;
lcd.setCursor(0, 0);
lcd.print("Press a Button ");
pulse += 1;
sprintf(lcdBuffer, "Bal Php: %d0.00", pulse);
lcd.setCursor(0, 1);
lcd.print(lcdBuffer);
delay(30);
Serial.println(pulse); // i need to print only 1 since im going to send this to nodemcu and nodemcu to database
}
i already solve the decrement code, but the increment, i cant get the exact serial.println. always ended up with 1 as result of serial.println which is it only reads the first print and not the latest print.
I inserted a 20 bill = 2 pulse
but the code prints only 1 and its the first serial.println and not the latest serial.println.
boolean pulsePrinted = false; // Flag for pulse print status
coinSlotStatus = digitalRead(coinSlot);
delay(30);
if (coinSlotStatus == 0) {
userBalance = true;
lcd.setCursor(0, 0);
lcd.print("Press a Button ");
pulse += 1;
sprintf(lcdBuffer, "Bal Php: %d0.00", pulse);
lcd.setCursor(0, 1);
lcd.print(lcdBuffer);
delay(30);
if (!pulsePrinted) { // Print pulse only once when a coin is inserted
Serial.println(pulse);
pulsePrinted = true;
}
}
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define button 4
#define relayPin 7
#define coinHopper 2
#define coinSlot 3
int coinSlotStatus;
int pulse;
int buttonState;
boolean userBalance = false;
boolean noCoin = false;
boolean pulsePrinted = false; // Flag for pulse print status
boolean iPrinted = false; // Flag for i print status
char lcdBuffer[16];
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.clear();
pinMode(coinSlot, INPUT_PULLUP);
pinMode(coinHopper, INPUT_PULLUP);
pinMode(button, OUTPUT);
pinMode(relayPin, OUTPUT); // Set the relay pin as output
digitalWrite(relayPin, HIGH); // Initially turn off the relay
}
void loop() {
int first_i = -1; // Variable to store the first value of i
if (!noCoin) {
noCoin = true;
lcd.setCursor(0, 0);
lcd.print("INSERT BILL/COIN");
Serial.println("Inserted Bill/Coin");
}
buttonState = digitalRead(button);
if (buttonState == 1 && pulse > 0 && userBalance) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dispensing Coins");
for (int i = pulse * 2; i > 0; i--) {
if (first_i == -1) { // Store the first value of i
first_i = i;
Serial.println(first_i); // Print the first value of i
}
digitalWrite(relayPin, LOW); // turn on relay - active LOW.
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(i);
// Check coin hopper sensor status
if (digitalRead(coinHopper) == LOW) {
// Coin detected, decrease pulse count and delay before next dispensing
pulse--;
delay(255); // Adjust delay as needed
} else {
i++; // Skip this iteration // No coin detected, wait for coin
delay(30); // Adjust delay as needed
}
}
digitalWrite(relayPin, HIGH); // turn off relay - active HIGH.
pulse = 0;
noCoin = false;
lcd.setCursor(0, 0);
lcd.print(" Thank You, Bye ");
lcd.setCursor(0, 1);
lcd.print("Pls get ur Coins");
delay(3000);
lcd.clear();
}
coinSlotStatus = digitalRead(coinSlot);
delay(30);
if (coinSlotStatus == 0) {
userBalance = true;
lcd.setCursor(0, 0);
lcd.print("Press a Button ");
pulse += 1;
sprintf(lcdBuffer, "Bal Php: %d0.00", pulse);
lcd.setCursor(0, 1);
lcd.print(lcdBuffer);
delay(30);
if (!pulsePrinted) { // Print pulse only once when a coin is inserted
Serial.println(pulse);
pulsePrinted = true;
}
}
}