'''
// Define pin for bill acceptor
const int billAcceptorPin = 3;
const int relayPin = 7; // Pin connected to the relay for activating the coin hopper
const int coinHopperPin = 2; // Pin connected to the coin hopper
// Variables to store the pulse count and calculated amount
volatile int pulseCount = 0;
int amount = 0;
bool activateRelay = false;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Set bill acceptor pin as input
pinMode(billAcceptorPin, INPUT);
// Set relay pin as output
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Ensure relay is initially off
// Set coin hopper pin as output
pinMode(coinHopperPin, OUTPUT);
digitalWrite(coinHopperPin, LOW); // Ensure coin hopper is initially off
// Attach an interrupt to the bill acceptor pin
attachInterrupt(digitalPinToInterrupt(billAcceptorPin), pulseCounter, RISING);
}
void loop() {
static unsigned long timer = 0; // Timer to keep track of time
const unsigned long interval = 15000; // Interval to wait before next bill can be inserted
static bool readyPrinted = false; // Flag to track if "Ready for next bill" has been printed
// If a pulse is detected
if (pulseCount != 0) {
// Now calculate and print the amount
amount = calculateAmount(pulseCount);
Serial.print("Amount Inserted: ");
Serial.println(amount);
// Reset pulse count after printing amount
pulseCount = 0;
// Start the timer
timer = millis();
// Reset the readyPrinted flag
readyPrinted = false;
// Set flag to activate relay
activateRelay = true;
}
// Check if the interval has passed and "Ready for next bill" has not been printed
if (!readyPrinted && millis() - timer >= interval) {
Serial.println("Ready for next bill");
readyPrinted = true; // Set the flag to true after printing the message
}
// Activate relay if the flag is set
if (activateRelay) {
digitalWrite(relayPin, HIGH); // Activate relay to start the coin hopper
activateRelay = false; // Reset flag
}
}
// Interrupt service routine to count pulses
void pulseCounter() {
pulseCount++;
}
// Function to calculate amount based on pulse count
int calculateAmount(int pulses) {
Serial.print("Pulses: ");
Serial.println(pulses);
// Define the mapping between pulse count and amount for each denomination
int pulsesPer1000Pesos = 100;
int amountPer1000Pesos = 1000;
int pulsesPer500Pesos = 50;
int amountPer500Pesos = 500;
int pulsesPer200Pesos = 20;
int amountPer200Pesos = 200;
int pulsesPer100Pesos = 10;
int amountPer100Pesos = 100;
int pulsesPer50Pesos = 5;
int amountPer50Pesos = 50;
int pulsesPer20Pesos = 2;
int amountPer20Pesos = 20;
// Calculate amount based on pulse count
if (pulses == pulsesPer1000Pesos) {
return amountPer1000Pesos;
} else if (pulses == pulsesPer500Pesos) {
return amountPer500Pesos;
} else if (pulses == pulsesPer200Pesos) {
return amountPer200Pesos;
} else if (pulses == pulsesPer100Pesos) {
return amountPer100Pesos;
} else if (pulses == pulsesPer50Pesos) {
return amountPer50Pesos;
} else if (pulses == pulsesPer20Pesos) {
return amountPer20Pesos;
} else {
return 0; // No match found
}
}
'''
this is the code, the relay is turn on (turning on the coin hopper) before i inserted a bill, then relay will turn off (stops the coin hopper after i inserted the bill. please help.
the relay is a low level trigger solid state relay ch1