Help me with my coding detecting pulse

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

volatile byte CoinPulseCount = 0; 
volatile byte hopperPulseCount = 0; 
volatile unsigned long PulseTime;  
byte NewCoinInserted;
byte Command = 0;
byte pulseThreshold = 200;  
int hopperPin = 2;  
int coinSelectorPin = 3;
String OnePulse = "5 peso";
String TwoPulses = "10 peso";



void setup() 
{
  Serial.begin(9600);
  pinMode(hopperPin, INPUT_PULLUP);     
  pinMode(coinSelectorPin, INPUT_PULLUP);
  digitalWrite(hopperPin, HIGH); 
  attachInterrupt(1, coinacceptor, RISING);
 
}

void loop()
{  
  
        if(CoinPulseCount > 0 && millis() - PulseTime > 200)
        {
            NewCoinInserted = CoinPulseCount;
            CoinPulseCount = 0;
        }

        switch (NewCoinInserted) 
        {

            case 1:
                  Serial.println(OnePulse + " inserted");
                  NewCoinInserted = 0;
                  break;
            case 2:
                  Serial.println(TwoPulses + " inserted");
                  NewCoinInserted = 0;
                  break;

        } 
}

void coinacceptor()      
{
  CoinPulseCount++;
  PulseTime = millis();   
}
                  Serial.println(OnePulse + " inserted");

Don't add Strings. If you can avoid using big-S strings entirely, that's going to work much better for you.

The above code could be written as:

                  Serial.print(OnePulse);
                  Serial.println(" inserted");

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:

  attachInterrupt(digitalPinToInterrupt(coinSelectorPin), coinacceptor, RISING);

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

Something like this

void coinacceptor()     
{
  CoinPulseCount++;
  PulseTime = millis();
  newPulse = true;
}

and then in loop() I would have

if (newPulse == true) {
   noInterrupts();
     latestPulseCount = PulseCount;
     latestPulseMillis = PulseTime;
     newPulse = false;
   interrupts();
}

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()

...R

volatile byte CoinPulseCount = 0;
volatile byte hopperPulseCount = 0;

Why is CoinPulseCount capitalized, but hopperPulseCount is not? Consistency is a good thing.

  pinMode(hopperPin, INPUT_PULLUP);     
  digitalWrite(hopperPin, HIGH);

You've already turned on the internal pullup resistor, using INPUT_PULLUP as the mode. Why are you doing it again using digitalWrite()?

Why do you have global variables that are never used?

hello guys haha im new here and i already confused how this things work perfectly sorry for my wrong codes

francisagrava:
i already confused how this things work perfectly

If it works perfectly, then what is the problem?

...R

i already did all of your suggestions but still not working

Please post the updated code and an update on what it is or is not doing correctly.

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
}

francisagrava:
haha sorry i forgot to switch the coin acceptor into a NO mode. btw this codes is working but not that accurate

Did you read Reply #2 ?

...R

1 Like