Card Despenser intrupting a for loop count

hello everyone, i have an interested project in my hands that I am making from a card despensor i bought in a garage sale. Anyways, the card despenser already has a controller on it and there is no data or information about it anywhere especially on google. anyways that card despenser is functioning pretty good. but i guess after all that i plan to do with i think i will end up replacing the old microcontroller with the an arduino.

anyways, i have made a sketch that works to my needs, and i have it ask for number of cards desired. now, before a variable is input to the despenser, the arduino will check if there are cards on the card deck (presences sw) is okay. if not it will not continue until there's card in the card deck if everything is okay then the arduino will start despensing the number of cards input. here is where i have a question.

if i want to stop or if there are problems and i want to stop the count in which its in a for loop.
How do i stop that for loop (either stop it to coninue it or stop it and reset to resart the count?

i tried various ways and i searched forums after forums for ideas before i came here.

Now there is a presensce sensor at the end, detecting presence of each card that is despense . that will stop the loop, if a card was not despense. the presesence sensor is included in the sketch but not enabled at the moment as i want to figure the "for loop" first...

Any Ideas guys...

/* 
 *  Tested with Arduino 1.6.0 on Arduino Uno
 *  This sketch is operate the pushbutton on a card despenser to despense desired cards
 *  the purpose is to give it an input variable 1 to 100+ to despense the amount 
 *  of cards spefiied in the input. a relay is operating the human interaction with the pushbutton
 *  this can be used for other despenses, like tickets, rfid cards, business cards, etc etc
 */


// Arduino libraries



//Pin Definitions
#define carddeckled      2    // led for cutoff switch for carddeck
#define despenseled      3    // led to Despense cards
#define presenceled      4    // led for exit presence sensor
#define manualsw         5    // button or switch to manual despense
#define carddecksw       6    // microswitch or sensor for cardstack sensor
#define presencesw       7    // microswitch or sensor to sense card presence 
#define relay1           8    //  output to despense switch 

//Times Variables with millis
const long interval          =   600;

int count = 0;


//Variable
int manualVal         =  HIGH;   // manual button value
int carddeckVal       =  HIGH;   // cardstack sensor value
int presenceVal       =  HIGH;   // cardstack sensor value

//boolean State
boolean manualState   =  false;   // manual button state
boolean carddeckState =  false;   // card deck  sensor State
boolean presenceState =  false;   // card deck  sensor State



void setup()
{
  Serial.begin(9600);                 // Communication buad rate
  pinMode(carddeckled,      OUTPUT);  // set carddeckled to output
  pinMode(despenseled,      OUTPUT);  // set despenseled to output
  pinMode(presenceled,      OUTPUT);  // set presenceled to output
  pinMode(relay1,           OUTPUT);  // 
  pinMode(manualsw,   INPUT_PULLUP); //set as input using internal pull-up
  pinMode(carddecksw, INPUT_PULLUP); //set as input using internal pull-up
  pinMode(presencesw, INPUT_PULLUP); //set as input using internal pull-up

  digitalWrite(relay1,      HIGH);   // in relay polarity is oppsite HIGH=off / LOW=ON

  Serial.println(" Number of Cards? "); // display input message
  Serial.setTimeout(200);            // timeout for parseInt



}

void loop()
{

  Carddeck();                       // go to cardDeck 

}

// Variable INPUT allow users or software mnagement system to give a variable
void serialInput() {
  if ( Serial.available() > 0)       // something available ? waits of user input
  {
    if ( !isdigit(Serial.peek()))    // is it a digit ? make sure its numerical digits over 10+ count
    {
      Serial.read();                 // throw away CR LF and other things, stores input data in buffer
    }
    else                             // 
    {
      Despense();                    // goto despense 
    }
  }
}

// despence cycle the normal operation after varialbe input is given
void Despense() {
   count = Serial.parseInt();                // read integer with timeout
//  Serial.print(" count = ");               // display count before starting count
//  Serial.println(count);                   // display amount of cards to despense
  for (int i = 0; i < count; i++)            // loop for the input count from Serial.parseInt()
  {
    digitalWrite(despenseled, HIGH);         // enabledespenseled on (enable)
    digitalWrite(relay1, LOW);               // open relay1 (enable)
    delay(interval);                         // on for 600ms 
    digitalWrite(despenseled, LOW);          // despenseled off (disable)
    digitalWrite(relay1, HIGH);              // close relay1 (disable)
    delay(interval);                         // off after 600ms 
  }
  Serial.print(" count = ");                 // displays count after the last card count.
  Serial.println(count);                     // display amount of cards despensed
}

// manual Despence Cycle is a switch to allow a manual test at anytime unless card deck switch is off
void manualDespence() {
  manualVal = digitalRead(manualsw);
  if (manualState && manualVal == 1){         // manualState true and manualval is HIGH
    manualVal = 1;                            // manual is high for sure
    digitalWrite(relay1, LOW);                // open relay (enable)
    digitalWrite(despenseled, HIGH);          // enable despenseled is 
    Serial.println(" Manual Despense... ");   // display message
    delay(600);                               // enable relay1 and despenseled for 600ms
    manualState = false;                      // set manualState to False closing the true logic state 
  }
  if (!manualState && manualVal == 1){        // set manualstate to false and manualVal to HIGH
    manualVal = 0;                            // manualVal is to LOW
    digitalWrite(relay1, HIGH);               // disable relay1 
    digitalWrite(despenseled, LOW);           // disable despenseled
    //delay(600);                             // disable relay1 and despenseled after 600ms
    manualState = true;                       // manualState is set to true closing the false logic state
  }
}

// Card Deck Switch checks if the deck of cards is present or empty
void Carddeck() {
  carddeckVal = digitalRead(carddecksw);      // set carddecksw variable as carddeckVal
  if (carddeckState && carddeckVal == 1) {    // check if carddeckState is true and cardeckval is HIGH
    carddeckVal = 1;                          // carddeckVal is set to HIGH
    
    digitalWrite(carddeckled, LOW);           // disable carddeckLED
    
    serialInput();                            // go to SerialInput()
    manualDespence() ;                        // go to ManaulDespense()
    
    delay(25);                                // delay is set at 25 to allow switch for anolog to logic
    carddeckState = false;                    // carddeckState is set to false closing the true logic state
  }
  if  (!carddeckState && carddeckVal == 1){   // check carddeckState is false and carddeckVal to HIGH
    carddeckVal = 1;                          // set carddeckVal to HIGH
    digitalWrite(carddeckled, HIGH);          // enable cardeckled 
    //delay(100);                             // delay is set to 100 at the - not needed
    carddeckState = true;                     // cardeckState is set to true closing the false logic state 
  }
}

// card sensor. detects if a card has gone through
void Presence() {
  presenceVal = digitalRead(presencesw);   // set presencesw variable as presenceVal
  if (presenceState && presenceVal == 1) {  // check if presenceState is true and pesenceVal is set to high
    presenceVal = 1;                        // set presenceVal to HIGH
    digitalWrite(presenceled, LOW);         // disable presenceled
    serialInput();                          // go to serialInput();
    manualDespence() ;                      // got to manualDespence(); 
    delay(25);                              // delay is at 25 to allow switch for anolog to logic
    presenceState = false;                  // presenceState is set to false closing the true logic state
  } 
  if (!presenceState && presenceVal == 1) { // chack if presenceState is false and presenceVal is set to HIGH 
    presenceVal = 1;                        // set presenceVal  to HIGH
    digitalWrite(presenceled, HIGH);        // disable presenceled 
    //delay(100);                        
    presenceState = true;                   // presenceState is set to true closing the false logic state
  }
}

You can use the break statement:

    for (int i = 0; i < count; i++)            // loop for the input count from Serial.parseInt()
    {
        if( exit condition is true )
            break;

        digitalWrite(despenseled, HIGH);         // enabledespenseled on (enable)
        digitalWrite(relay1, LOW);               // open relay1 (enable)
        delay(interval);                         // on for 600ms
        digitalWrite(despenseled, LOW);          // despenseled off (disable)
        digitalWrite(relay1, HIGH);              // close relay1 (disable)
        delay(interval);                         // off after 600ms
    }

If you need to break out of a FOR loop you should probably not be using FOR in the first place. Use IF and allow loop() to do the repetition and a variable to keep count.

Something like this

if (cardCount < requestedCards) {
   cardCount ++

   // your other code
}

...R

Sorry for the late and Thank you Robin2 for your reply. I will go ahead an try that as soon as i get to home and post the update.

Okay I got rid of the For Loop and did the IF loop... Still cant stop the count loop from stopping if carddeck switch is disable. Any Ideas.

// despence cycle the normal operation after varialbe input is given
void Despense() {
   count1 = Serial.parseInt();                // read integer with timeout

while (count < count1) {
  if( carddeckState){
    if(carddeckVal = HIGH){
    if (despenseState = true){
    digitalWrite(despenseled, HIGH);
    digitalWrite(relay1, LOW);
    delay(700);
    }
    if(despenseState = true){
    digitalWrite(despenseled, LOW);
    digitalWrite(relay1, HIGH);
    delay(700);
    }
  Serial.print(" count = ");                 // displays count after the last card count.
  Serial.println(count);                     // display amount of cards despensed
    count++;
}
  }
 if (!carddeckState){
  if (carddeckVal = LOW){

}
  }
}
count = 0;
}

You should not have a WHILE for the same reason you abandoned the FOR. There is no WHILE in the program in your Original Post.

Leave it to loop() to do the repetition.

...R