user50605: The problem of my code is in the serial monitor it print the not enough credits even i did not pressed the button. The output that i want is it must be print not enough credits when i pressed the button.
#include <Servo.h>
const int coinpin = 2;
const int buttonPin = 7;

// Variables
volatile int credits = 0;
int buttonState = 0;
const int servoPin = 8;
 Servo servo;

// Setup
void setup() {
  servo.attach (servoPin);
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(coinpin), coinInterrupt, RISING);
  pinMode(buttonPin, INPUT);
  servo.write(0);

}

// Main loop
void loop() {

Serial.println(credits);
delay(2000);
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && credits == 5) {

  Serial.println(credits);
  servo.write(180);
  delay(1000);
  servo.write(0);
}

if(credits !=5) {
  Serial.println("NOT ENOUGH CREDITS");
  delay(2000);
}




}

// 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
  credits = credits + 1; 
}


user521: The second if statement is standalone and not inside the button press check if statement so will always print 'not enough credit' whenever credit is not exactly 5.
You might also want to look at debouncing the button as you're liable to get multiple triggers on one press.

user34529: Look at lines of code I marked:

Each loop you check for credits being equal to 5, and if it's not equal to 5 you write "NOT ENOUGH CREDITS" You might want to modify your "if" structure to only check for credits when the button is pressed.
Immediately I see two bugs:

What happens when number of credits is 6? Is it enough or not?
What happens when user presses the button for 2 or more "loop"s?

The physical button press looks to the hardware as several consecutive button presses because the button "bouncess" under your finger. Fixing that issue is called "debouncing" and there are several ways to do that. Read more about that.

50605: <p>The problem of my code is in the serial monitor it print the not enough credits even i did not pressed the button. The output that i want is it must be print not enough credits when i pressed the button.</p>

<pre><code>#include &lt;Servo.h&gt;
const int coinpin = 2;
const int buttonPin = 7;

// Variables
volatile int credits = 0;
int buttonState = 0;
const int servoPin = 8;
 Servo servo;

// Setup
void setup() {
  servo.attach (servoPin);
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(coinpin), coinInterrupt, RISING);
  pinMode(buttonPin, INPUT);
  servo.write(0);

}

// Main loop
void loop() {

Serial.println(credits);
delay(2000);
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH &amp;&amp; credits == 5) {

  Serial.println(credits);
  servo.write(180);
  delay(1000);
  servo.write(0);
}

if(credits !=5) {
  Serial.println("NOT ENOUGH CREDITS");
  delay(2000);
}




}

// 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
  credits = credits + 1; 
}
</code></pre>
 521: <p>The second if statement is standalone and not inside the button press check if statement so will always print 'not enough credit' whenever credit is not exactly 5.
You might also want to look at debouncing the button as you're liable to get multiple triggers on one press.</p>
 34529: <p>Look at lines of code I marked:</p>

<p><a href="https://i.stack.imgur.com/LpJVH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LpJVH.png" alt="Formatted code from the question"></a></p>

<p>Each loop you check for credits being equal to 5, and if it's not equal to 5 you write "NOT ENOUGH CREDITS" You might want to modify your "if" structure to only check for credits when the button is pressed.</p>

<p>Immediately I see two bugs:</p>

<ol>
<li>What happens when number of credits is 6? Is it enough or not?</li>
<li>What happens when user presses the button for 2 or more "loop"s?</li>
</ol>

<p>The physical button press looks to the hardware as several consecutive button presses because the button "bouncess" under your finger. Fixing that issue is called "debouncing" and there are several ways to do that. Read more about that.</p>
