hello can anyone help me with my code it detecting pulse but not that accurate example when i upload the sketch automatically displaying that i inserted 5 peso but i didn't. thanks
I would add a case 0 (which does nothing) and a default which prints out the number directly, so that you can check you're not getting silly numbers like 3.
Are you sure there isn't any way that coinSelector pin can go HIGH? Remember interrupts are very sensitive and even a nanosecond of HIGH will trigger it.
I had to think for a minute which pin is interrupt 1. It's better to write this as:
francisagrava:
hello can anyone help me with my code it detecting pulse
When I have code like that I add a line newPulse = true; into my ISR and then I can easily test for that in loop() and when I have read the ISR values I can set it back to false
Note the use of noInterrupts() to prevent the ISR changing the values while you are reading them. Also I have copied the ISR values into other variables for use elsewhere in loop()
haha sorry i forgot to switch the coin acceptor into a NO mode. btw this codes is working but not that accurate sometimes when i inserted a 5peso coin it displayng 10 peso coin or vice versa
volatile byte CoinPulseCount = 0;
volatile byte hopperPulseCount = 0;
volatile unsigned long PulseTime;
byte NewCoinInserted;
byte Command = 0;
byte pulseThreshold = 200; //EDIT THIS VALUE TO CHANGE DELAY BETWEEN DETECTING BANK OF PULSES
int hopperPin = 2; // pin2 as optical count input
int coinSelectorPin = 3; // pin3 as optical count input
String OnePulse = "5 peso";
String TwoPulses = "10 peso";
void setup()
{
Serial.begin(9600);
//**** pinModes *************
pinMode(hopperPin, INPUT_PULLUP); //hopper optical count is an input
pinMode(coinSelectorPin, INPUT_PULLUP); //coin selector optical input
digitalWrite(hopperPin, HIGH); //use the internal pullup resistor on the hopper optical input
attachInterrupt(digitalPinToInterrupt(coinSelectorPin), coinacceptor, RISING);
}
void loop()
{
//CHECK NOW TO SEE WHICH COIN IS INSERTED
if(CoinPulseCount > 0 && millis() - PulseTime > 200){
NewCoinInserted = CoinPulseCount;
CoinPulseCount = 0;
}
//Proccess the coin inserted
switch (NewCoinInserted)
{
case 1:
Serial.print(OnePulse);
Serial.println(" inserted");
NewCoinInserted = 0;
break;
case 2:
Serial.print(TwoPulses);
Serial.println(" inserted");
NewCoinInserted = 0;
break;
}
}
//*****INTERUPT detecting pulses from the coin acceptor
void coinacceptor() //Function called when coin enters coin acceptor
{
CoinPulseCount++;
PulseTime = millis(); //store current time in pulseTime
}