False positives

I am reading pulses off of a 12v coin mechanism. I updated my code and I am now getting loads of false positives on the digital pin. I am wondering if I have shorted something on my board which is causing this. What do you guys think. Any questions please ask. I also tried my origional code and got the same issue. Below is my code

const int coinInt = 2; 
//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
volatile 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);
    Keyboard.press('#');
    delay(100);
   Keyboard.releaseAll();
   delay(300);
    Keyboard.press('+');
    delay(100);
   Keyboard.releaseAll();    
//Print the Value of coins inserted
  }
}

I presume you have tied the input pin to high or low.

Where is the full program?

Weedpharma

That is the full program, I dont understand what you mean could you give me an example

You have not let us know if the pin is tied to 5v or Gnd.

Weedpharma

A floating pin is neither connected to 5v or ground, which means that it just randomly goes HIGH and LOW

What are you using to detect the coin? Is it a simple microswitch? If so it'll likely need debouncing.

Hi, the pin that you are using to detect the coin, has to be either HIGH or LOW.

If the input is connected to nothing when there is no coin, the the input will what they call float and noise will make it go from HIGH to LOW randomly.

To solve this problem there is a solution with a 10K resistor.
If your input goes HIGH when the coin is present, then you need to connect the resistor between the input and GND, this will make sure your input is not floating when there is no coin present.

Tom..... :slight_smile: