hi to everyone who reads this!
I am from the UK and need some help with a project. I have a coin hopper/dispenser and want to connect it via an UNO to a microphone. the idea is that when a sound is heard (volume programmable on microphone) it will then turn the hopper motor on and allow it to dispense a set amount of coins. I had some code written for me but want to check if it would work before i go ahead and buy all of the components...!
Any help would be appreciated!
(Please see attached code - Was given this, but needs checking)..
Thanks 
2p_Change_Machine_Arduino_Programming.ino (1.55 KB)
You can use the magnifier symbol, up to the right, and search for "coin". There is a considerable community dealing woth coins thouse days.
For the benefit of everyone, here is the code after Auto formatting in the IDE
//********* Hopper initiated by Mic **************
#define micThreshold 500 //If the value read from the mic is above this value then a sound has been heared.
byte amountToDispenseIfSoundHeared = 5; //change this value to how many coins you want to dispense when a sound is heared
#define hopperSensorPin 3
#define motorDrivePin 2
#define micInputPin A0 //connect mic pin to analog input AO
byte amountToDispense = 0;
volatile byte amountDispensed = 0;
volatile boolean dispensing = false; //flag to track if currently dispensing
void setup()
{
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(hopperSensorPin), coinPulse, FALLING);
pinMode(hopperSensorPin, INPUT_PULLUP);
pinMode(motorDrivePin, OUTPUT);
digitalWrite(motorDrivePin, HIGH);
Serial.println("Hopper Program Started");
}
void loop()
{
int micReading = analogRead(micInputPin);
//Serial.println(micReading); //uncomment this to see what reading is coming from the mic when a sound is heared.
if ((micReading > 500) && !dispensing) //if a reading is over the threshold and not currently dispensing then start to dispense.
{
amountToDispense = amountToDispenseIfSoundHeared;
digitalWrite(motorDrivePin, LOW);
dispensing = true;
}
delay(5); //small delay to allow the analog to digital read to settle
}
void coinPulse()
{
++amountDispensed;
if (amountDispensed == amountToDispense)
{
Serial.println("Completed");
digitalWrite(motorDrivePin, HIGH);
amountDispensed = 0;
dispensing = false;
}
}
I note that the code prints in the ISR. Printing uses interrupts. Interrupts are automatically disabled in ISRs. Can you see a possible problem ?
Hi UKHeliBob, thanks for your reply. I am a total beginner and therefore have no idea what I am doing. Just thought this would be the easiest way to make it work. Would you be able to write a modified code that would work better?
Thanks 
Do you need to see the "Completed" message ?
You could consider using an LED or a number of LEDs to indicate that the cycle had ended or the program could easily be modified to print outside of the ISR
Still don't know what youre talking about... what is ISR?
and can you modify my code to make it right?
ISR - interrupt Service Routine. A short section of code that interrupts the normal flow of program operation and runs when certain conditions are true such as a coin being inserted.
Where did you get the code, as a competent Arduino programmer should have known that printing in an ISR would not work reliably, if at all.
ok i understand that. someone wrote it for me but thats why i'm asking for help on here as want to check if it will work. Can you re-write the code for me?
Whilst it is easy to re-write the code to do exactly what it does now I am wary to do so without knowing more about the hardware.
For instance, which Arduino are you using, how is it powered, what coin dispenser are you using (link please), how is it powered, what sort of environment will it be used in (electrically noisy ?), must the coins be dispensed as quickly as possible or would say one per second be acceptable, is the application for your personal use, educational or commercial ?
The first step is to ascertain whether the microphone can be used to trigger action. Can you/have you tested that ?
Have you got a full specification for your hardware and what you want to do with it ?
The reason that I am being cautious is that often a simple requirement blossoms into larger one when someone says "That works but can you just add ..."
Hi Thanks for your reply.
I will be using an arduino uno/possibly a clone uno board.
It will be powered via usb from a computer.
I have an Asahi Seiko hopper. Link: https://www.aseuro.co.uk/products/sh-400/
I am going to buy a 24v dc power supply to power it (as that is the rated voltage).
It will be relatively 'noisy' - but the microphone can be adjusted to different volumes, so can set it according to general background noise (so it doesn't pick it up, only the loud sound being played from the speaker next to it)..
Need them to be dispensed as quickly as possible.
Personal use, but want it to be reliable.
I have not yet bought the microphone so am unsure. But i have seen videos of microphones triggering led lights to illuminate/music to be played etc...
Here is the manual for the dispenser: https://www.aseuro.co.uk/wp-content/uploads/2016/01/technical_P13-SH400-Flexi-Disc-Iss-1.pdf
If you need any more info, please let me know.
The first thing that I suggest you do is to buy an Arduino. A Uno or a clone is a good choice because it is well understood and you can connect to it without soldering, at least when testing. You will also need some jumper wires to connect things up and, of course, the 24V power supply
In general, apart from printing in an ISR, the code looks like it could work but there may be a need to revise it to deal with contact bounce on the "coin dispensed" line. You don't really need to use an ISR as the repeat rate of the loop() function will allow you to capture the payout signals and the microphone can be disabled during the payout to speed things up and I would suggest revising the program to remove the need for the ISR
There is, however, a problem in that the Motor On input requires a voltage between 5V and 12V to dispense a coin. From the manual
Motor On+
should be pulled high to a reference voltage of between 5vDC and
12vDC for interfacing ‘directly’ with host machine control circuitry
The voltage of an Arduino pin can only be 5V at maximum and is, in practice, lower than that so something transistor or MOSFET will be required between the Arduino and the dispenser to provide a high enough control voltage
How is your knowledge of electronics ?
Hi thanks for your advice. Will get on and buy the bits for it.
I have a basic understanding of electronics but know nothing about transistors/MOSFET??!
Would you be able to send me a link to something i could buy to up the voltage?
Many thanks
Would you be able to send me a link to something i could buy to up the voltage?
I am reluctant to give such advice as it is not an area of my expertise
Ok, but what would you suggest?
Search the forum for the phrase logic level mosfet and you will get plenty of hits
This video may help MOSFETs
Hi - watched the video. I now understand what a mosfet will do!
Will get on and buy the components and then get back to you when I come to wire it all up.
Many thanks for your help!!