Coin acceptor help!

Hi, I'm trying to put together a coin acceptor that turns on a solenoid once the correct number of coins have been put in. I had it working correctly the other day while plugged into the computer, but now trying to put it where it's meant to be, it's not working. Even plugging it back into the computer it won't work.

As soon as electricity goes to the arduino, the coin count starts going up (in random increments) and the puzzle is solved. I don't really know much about coding, so this could be an easy fix. Thanks in advance!

// Constants
const int coinpin = 2;
const int ledpin = 3;
const int targetcents = 100;
const byte lockPin = A0;

// Variables
volatile int cents = 0;
int credits = 0;

// Setup
void setup() {

Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(coinpin), coinInterrupt, RISING);
// Configure the lock pin as an output
pinMode(lockPin, OUTPUT);
digitalWrite(lockPin, LOW);

}

// Main loop
void loop() {

// If we've hit our target amount of coins, increment our credits and reset the cents counter
if (cents >= targetcents) {
credits = credits + 1;
cents = cents - targetcents;
}

// If we haven't reached our target, keep waiting...
else {
}

// Debugging zone
Serial.print(cents);
Serial.print(" cents toward current credit and ");
Serial.print(credits);
Serial.println(" credit(s) earned so far.");
delay(1000);

// Turn off LED
digitalWrite(ledpin, LOW);

// Now, write your own cool code here that triggers an event when the player has credits!
if (credits >= 1) {
Serial.println("The puzzle has been solved");
digitalWrite(lockPin, HIGH);
}
else{
digitalWrite(lockPin, LOW);

}

}

// Interrupt
void coinInterrupt(){

// Each time a pulse is sent from the coin acceptor, interrupt main loop to add 1 cent and flip on the LED
cents = cents + 20;
digitalWrite(ledpin, HIGH);

}

Loose connection - coinpin is likely floating, and hence picking up noise from the environment and transitioning randomly.

That's what we thought, but even with nothing plugged into the arduino, other than the usb connection to my computer, the count goes up.

I just unplugged the A0 pin which connects the relay, and the count doesn't go up. Is there something wrong with my code there? Again I'm really new at this haha

You don't have a pinMode() for the input pin. That might be OK (pins get set to INPUT on reset) but is bad practice.

What value of pull-up or pull-down resistor are you using on that pin?

You don’t have </> code tags around your post, that is definitely a problem.