Auch wenn das Thema Münzprüfer schon mehrfach hier im Forum nachgefragt wurde, liefert es mir bisher noch nicht die richtigen Antworten.
Ich habe eine 12V Münzprüfer mit meinen Arduino MEGA verbunden und es läuft soweit alles. D.h. der Münzprüfer bekommt unabhängig Strom und die Kommunikation zum Arduino mit den einzelnen Münzen funktioniert grundsätzlich auch. Als Code nutze ich:
const int coinInt = 0;
//Attach coinInt to Interrupt Pin 0 (Digital Pin 2). Pin 3 = Interrpt Pin 1.
volatile float coinsValue = 0.00;
//Set the coinsValue to a Volatile float
//Volatile as this variable changes any time the Interrupt is triggered
int coinsChange = 0;
//A Coin has been inserted flag
void setup()
{
Serial.begin(9600);
//Start Serial Communication
attachInterrupt(coinInt, coinInserted, RISING);
//If coinInt goes HIGH (a Pulse), call the coinInserted function
//An attachInterrupt will always trigger, even if your using delays
}
void coinInserted()
//The function that is called every time it recieves a pulse
{
coinsValue = coinsValue + 0.05;
//As we set the Pulse to represent 5p or 5c we add this to the coinsValue
coinsChange = 1;
//Flag that there has been a coin inserted
}
void loop()
{
if(coinsChange == 1)
//Check if a coin has been Inserted
{
coinsChange = 0;
//unflag that a coin has been inserted
Serial.print("Credit: £");
Serial.println(coinsValue);
//Print the Value of coins inserted
}
}
Ich habe die Geldmünzen mit Pulsen verknüpft am Münzprüfer: 10Cent: 1Puls, 20Cent: 2Pulse, 50Cent: 3Pulse, 1€: 4Pulse, 2€: 5Pulse. In der Anzeige beim Seriellen Monitor werden die Münzen entsprechend erkannt und nach dem Code hier in 0,05 P hochgezählt, als 50Cent Einwurf ergibt 0,15 P. Werfe ich eine weitere Münze ein z.B. 10Cent addiert er zu den 0,15 P die 0,05 p von den einen Impuls der 10 Cent Münze hinzu und zeigt im seriellen Monitor 20P an.
Genau das ist mein Problem: Anhand de Impulse weiß ich ja welche Münze eingeworfen wurde. Also war meine Idee eine Art "Counter" in die Mehtode "void coinInserted()" zu implementieren und wenn der Counter halt 3 ist (3 Impulse somit) ordne ich über eine if Bedingung den Wert der Münze zu.
if(Counter == 3){
muenze=0.50;
}
Die If-Bedingung ist nicht in mein Programm implementiert, da die Counter Variante nicht funktionieren würde.
Die Frage ist nun, wie ordne ich den Impulsen den Wert der Münze zu. Ich habe überlegt etwas mit millis() zu machen, da ja Impluse in einer bestimmten Zeit erfolgen, nur habe ich bedenken, dass das Programm durcheinander kommen könnte, wenn jemand 2 Münzen unmittelbar hintereinander einwirft.
Als Problematik in meinen Kopf ist quasi zu bestimmen, wie ich bestimme, wann die Impulse einer Münze Beendet sind und anhand dessen dann den Wert der Münze zuweise. Vielleicht ist millis auch der richtige Ansatz....