program bug

Im trying to control a solenoid and a servo motor. the servo motor will activate when a total of 20 pulses is received and the solenoid will activate every time 4 pulses are detected. it works fine if I start with the coin that equal 4 pulses but it I start with a coin that equals 10 pulses or 20 the servo will activate but then she solenoid won’t work again.

#include <Servo.h>
int solenoidPin = 7;    //This is the output pin on the Arduino we
// Constants
const int coinpin = 2;
const int targetcents =20  ;
int servoPin = 9;
Servo servo; 
int angle = 0;   // servo position in degrees  

float metal, metall;
int reading;
int metalPin = A0;
int metalPinn = A1;


// Variables
volatile int pulse = 0;
volatile int pulsee = 0;
int pesos = 0;

// Setup
void setup() {
  
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(coinpin), coinInterrupt, RISING);
  // Servo motor///////
   servo.attach(servoPin); 

   // put your setup code here, to run once:
  pinMode(solenoidPin, OUTPUT);           //Sets the pin as an output

}


// Main loop
void loop() {

 reading = analogRead(metalPin);
metal = (float)reading*100/1024.0;
Serial.print("Metal in Proximity = ");
Serial.println(metal);
reading = analogRead(metalPinn);
metall = (float)reading;
Serial.print("Metall in Proximity = ");
Serial.println(metall);





   if (pulse >= targetcents) {
    pesos = pesos+1;
    pulse = pulse - targetcents;
  }
   

  delay(10);
  switch(pulsee){
    case 1: 
    pulsee=0;
    break;
   
    case 4:
  
    digitalWrite(solenoidPin, HIGH);    //Switch Solenoid ON
  delay(100);                      //Wait 1 Second
  digitalWrite(solenoidPin, LOW);     //Switch Solenoid OFF
  delay(100);                      //Wait 1 Second
  
      pulsee=0;
      Serial.print(pulsee);
      Serial.println("4 pulsossssssssssssssssssssssssssssssssssssssssssss");
      delay(10);
      break;
      
  }
  
 

 
if (pulse==2) {
    pesos=1;
   
  }
Serial.print(pesos);
  Serial.println(" Pesos insertados");
  delay(10);


  




  
if (pulse==4) {
    pesos=2;
    
  }
Serial.print(pesos);
  Serial.println(" Pesos insertados");
  delay(10);

if (pulse==10) {
    pesos=5;
  }
Serial.print(pesos);
  Serial.println(" Pesos insertados");
  delay(10);




  delay(1000);
  if (pulse==20) {
    
    // scan from 0 to 180 degrees
  for(angle = 0; angle < 90; angle++)  
  {                                  
    servo.write(angle);               
    delay(3);                   
  } 
  // now scan back from 180 to 0 degrees
  for(angle = 90; angle >5; angle--)    
  {                                
    servo.write(angle);           
    delay(3);
          
  } 
       
     
  }

} 


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

Moderator-added code tags. Use the </> for your next code load please.

Here is your code with the logic working as you explained. You did not explain the purpose of the two analog inputs so I have removed that section.

#include <Servo.h>
Servo servo;
int angle = 0;   // servo position in degrees

// Constants
const int solenoidPin = 7;    //This is the output pin on the Arduino we
const int coinpin = 2;
const int targetcents = 20;
const int servoPin = 9;

// Variables
int lastPulseCount = 0;
volatile int pulse = 0;
volatile int pesos = 0;

void setup() {
  Serial.begin(9600);
  pinMode(coinpin, INPUT);
  pinMode(solenoidPin, OUTPUT);           //Sets the pin as an output
  servo.attach(servoPin);
  attachInterrupt(digitalPinToInterrupt(coinpin), coinInterrupt, RISING);
}

void loop()
{
  if (lastPulseCount != pulse) {
    lastPulseCount = pulse;
    Serial.print(pulse);
    Serial.print(" Pulses ");
    Serial.print(pesos);
    Serial.print(" Pesos");
    if ((pulse > 0) && (pulse % 4 == 0 )) {
      Serial.print(" + Cycle Solenoid");
      digitalWrite(solenoidPin, HIGH);  //Switch Solenoid ON
      delay(100);                      //Wait 0.1 Second
      digitalWrite(solenoidPin, LOW);  //Switch Solenoid OFF
    }
    if ( pulse >= targetcents ) {
      Serial.print(" + Cycle Servo");
      // scan from 0 to 180 degrees
      for (angle = 0; angle < 90; angle++)    {
        servo.write(angle);
        delay(3);
      }
      // now scan back from 180 to 0 degrees
      for (angle = 90; angle > 5; angle--)    {
        servo.write(angle);
        delay(3);
      }
      pesos = 0;
      pulse = 0;
    }
    Serial.println("");
  }
}

// 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
  pulse++;
  if ((pulse % 2) == 0)
    pesos = pulse / 2;
}

Please note that the only delay()s I have left in the code are for the solenoid and servo movement. The program does nothing until the pulse count changes and then if either pulses equal a multiple of 4 or they reach 20.

The code uses the Modulo operator to perform several tests, like this:

 if ((pulse > 0) && (pulse % 4 == 0 )) {

Which says if the pulse count is greater than zero AND “pulse” is evenly divisible by 4, then cycle the solenoid. Here is the Serial output when fed a series of pulses:

1 Pulses 0 Pesos
2 Pulses 1 Pesos
3 Pulses 1 Pesos
4 Pulses 2 Pesos + Cycle Solenoid
5 Pulses 2 Pesos
6 Pulses 3 Pesos
7 Pulses 3 Pesos
8 Pulses 4 Pesos + Cycle Solenoid
9 Pulses 4 Pesos
10 Pulses 5 Pesos
11 Pulses 5 Pesos
12 Pulses 6 Pesos + Cycle Solenoid
13 Pulses 6 Pesos
14 Pulses 7 Pesos
15 Pulses 7 Pesos
16 Pulses 8 Pesos + Cycle Solenoid
17 Pulses 8 Pesos
18 Pulses 9 Pesos
19 Pulses 9 Pesos
20 Pulses 10 Pesos + Cycle Solenoid + Cycle Servo
0 Pulses 0 Pesos
1 Pulses 0 Pesos
2 Pulses 1 Pesos
3 Pulses 1 Pesos