Relay and Coin Hopper problem

'''
// 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

like this?

// Set relay pin as output
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH); // Ensure relay is initially off

// Set coin hopper pin as output
pinMode(coinHopperPin, OUTPUT);
digitalWrite(coinHopperPin, LOW); // Ensure coin hopper is initially off

Do you have a 10k pulldown resistor connected from pin 3 to GND to keep the pin from "floating" between 0 and 5V (LOW and HIGH)?
Also, I added code tags "</>" for you.

// 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
}
}

yes i have a 10k resistor

still the same problem when i run the code, the relay is active and the hopper is rotating

but when i add this code

// 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, HIGH); // 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, LOW); // Activate relay to start the coin hopper
activateRelay = false; // Reset flag
} else {
digitalWrite(relayPin, HIGH); // Ensure relay is off if not activated
}
}

// 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
}
}

the relay is not active but wont activate after there is a bill inserted

// Activate relay if the flag is set
if (activateRelay) {
digitalWrite(relayPin, LOW); // Activate relay to start the coin hopper
activateRelay = false; // Reset flag
} else {
digitalWrite(relayPin, HIGH); // Ensure relay is off if not activated
}

i write low to turn it on
and high to turn it off

but the thing is i cant determine the problem why its not working after i inserted a bill from bill acceptor

Post a link to the relay's datasheet or it's brand name and exact part number.

1 channel 5v dc solid state relay module board low level trigger G3MB-202P relay ssr 240v ac 2a arduino plc controller

// 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

}

}

ohh i see, i need to change it to low

thank you so much, its working now <3

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.