If/else, while, do_while? I'm very stuck.

I am running an Lynxmotion Botboarduino (Arduino Duemilanove equivalent), connected to a Lynxmotion SSC-32 servo controller via serial cable, controlling my Lynxmotion AL5D arm. I am trying to program the arm to sense an object placed at a starting location via IR sensor, have the arm move to pick it up and drop it in a cup, a specified number of times (having the arm wait for the object is sensed by the sensor before it goes to pick it up) then stop, ignoring anything else placed in front of the IR sensor.

My progress: I have the arm sense the object, move to pick it up, drop it in the cup.

My problem: Anything I have tried as far as to limit how many times the arm reacts to the IR sensor has not worked. It senses the object, then "picks up and drops" whatever my specified number of times was, regardless of there having been only one object (it doesn't wait for the IR sensor). I will include the upper portion of the code, but not the last half, as its just movements:

// set pin numbers:
const int irsensorPin = 2;     // the number of the irsensor pin
const int go_ledPin =  8;      // the number of the Green LED pin
const int stop_ledPin = 7;     // the number of the Red LED pin

// variables will change:
int irsensorState = 0;         // variable for reading the irsensor status

void setup() 
 
{
   Serial.begin(9600); //sets the baud rate at 9600 to match the one set on the SSC-32 board
   pinMode(irsensorPin, INPUT);  // initialize the irsensor pin as an input
   pinMode(go_ledPin, OUTPUT);  // initialize the LED pins as an outputs
   pinMode(stop_ledPin, OUTPUT);  // initialize the LED pins as an outputs
   digitalWrite(go_ledPin, HIGH);
   digitalWrite(stop_ledPin, HIGH);   
   park();  // park ARM to starting position

}

void loop(){
    
  irsensorState = digitalRead(irsensorPin);  // read the state of the ir sensor value
  checksensorLED();
  getround();
}


void checksensorLED()
{
  if (irsensorState == LOW) {     // check if the ir sensor is triggered, if it is, the ir sensor is LOW
    // turn Green LED on, turn Red LED off:    
    digitalWrite(go_ledPin, LOW);  // the LED's on this board light during low state...
    digitalWrite(stop_ledPin, HIGH);
  } 
    
  else {
    // turn Green LED off, turn Red LED on:
    digitalWrite(go_ledPin, HIGH);
    digitalWrite(stop_ledPin, LOW); 
  }
}


void getround()
{
  
if (irsensorState == LOW) {     // check if the ir sensor is triggered, if it is, the ir sensor is LOW  
//Move arm to pick up round:
    delay(2000);
    Serial.println("#5 P1500 T1000 <cr>");
    Serial.println("#2 P1790 T1000 <cr>");
    Serial.println("#3 P840 T1000 <cr>");
    Serial.println("#0 P690 T1000 <cr>");
    delay(1000);
    Serial.println("#1 P1620 T1000 <cr>");
    delay(1000);
    Serial.println("#4 P1435 T1000 <cr>");
    delay(1000);

(the remaining code is more "Serial.println("#5 P1500 T1000 ");" commands for the arms movement).
Any suggestions as to what mechanism would work in this instance would be greatly appreciated. I have tried if/else, then tried while with exactly the same results. I am having no luck adapting anything from the reference examples I can find.
Also: I have to base the count on how many times the arm is initiated, as opposed to counting the IR trips, as the sensor would count a number of times every cycle due to the gripper setting it off during the cycle.

I thank you for your assistance in advance.

getround();

What's this thing doing? Loading a gun?

hehe no. I am trying to teach myself robotics in process automation, and decided to program my robotic arm to count out rounds to a predefined number, as they pose challenges in the process due to the unique conical in shape and wouldn't stack in a "hopper" or such, forcing me to get more creative. No guns involved.

Your problem is a little confusing. Please correct me if I'm wrong, but once the IR sensor is triggered, your arm keeps going through its cycle regardless of future reading from the IR sensor. Correct?

I'm trying to get a better picture of your problem

Thanks for your question G3t, yes, when I attempt to implement any kind of count into the sketch to limit the number of times the arm reacts to the IR sensor, yes, it cycles the same number of times as the number I wanted the arm to stop at, regardless of the sensor not being tripped more than once.

when what I want it to do is "see" the object, pick it up, drop in cup, repeat, but only X number of times..

Basically I want to feed objects to the arm, and have it retrieve X number of those objects into a cup then stop.

Sounds easy enough! :slight_smile:

This was the second failed attempt (Modification of the original code I posted) that acted the way I described:

const int irsensorPin = 2;     // the number of the irsensor pin
const int go_ledPin =  8;      // the number of the Green LED pin
const int stop_ledPin = 7;     // the number of the Red LED pin

// variables will change:
int irsensorState = 0;       // variable for reading the irsensor status
int sensorroundcount = 0;   // counter for the number of rounds sensed by the ir sensor
int lastirsensorState = 0;   // previous state of the ir sensor


void setup() 
 
{
   Serial.begin(9600); //sets the baud rate at 9600 to match the one set on the SSC-32 board
   pinMode(irsensorPin, INPUT);  // initialize the irsensor pin as an input
   pinMode(go_ledPin, OUTPUT);  // initialize the LED pins as an outputs
   pinMode(stop_ledPin, OUTPUT);  // initialize the LED pins as an outputs
   park();  // park ARM to starting position

}

void loop(){
    
  irsensorState = digitalRead(irsensorPin);  // read the state of the ir sensor

// compare the buttonState to its previous state
  if (irsensorState != lastirsensorState) {
    // if the state has changed, increment the counter
    if (irsensorState == LOW) {
      // if the current state is LOW then the button
      // wend from off to on:
      sensorroundcount++;
    }
    
    else {}
    lastirsensorState = irsensorState;
  sensorroundcount = 0;
while(sensorroundcount < 2){
  // do something repetitive 2 times
  checksensorLED();
  getround();
  sensorroundcount++;
}
}
}

I used a button counting code, heavily modified to try a "while" statement, but no dice.

Pseudo code:

roundCount = 1; //before setup

void loop(){ //to do 5 times
  if (roundCount <=5 && IRdetected){ //Logical AND, both have to be true.
     roundCount ++; //increment roundCount
     Do your moves here;
  }
}

Henry, you are my hero! I don't know why I didn't think of that, outside of only having started with arduino a month ago recreationally..lol Many thanks for all the support from everyone! I gave you a tip-o'-the-hat in the sketch description :slight_smile: This is the now fully functional sketch (pertinent portion anyway):

// set pin numbers:
const int irsensorPin = 2;     // the number of the irsensor pin
const int go_ledPin =  8;      // the number of the Green LED pin
const int stop_ledPin = 7;     // the number of the Red LED pin

// variables will change:
int irsensorState = 0;       // variable for reading the irsensor status
int sensorroundcount = 1;   // counter for the number of rounds sensed by the ir sensor


void setup() 
 {
   Serial.begin(9600); //sets the baud rate at 9600 to match the one set on the SSC-32 board
   pinMode(irsensorPin, INPUT);  // initialize the irsensor pin as an input
   pinMode(go_ledPin, OUTPUT);  // initialize the LED pins as an outputs
   pinMode(stop_ledPin, OUTPUT);  // initialize the LED pins as an outputs
   digitalWrite(go_ledPin, HIGH);
   digitalWrite(stop_ledPin, LOW);
   park();  // park ARM to starting position
}

void loop()
{ 
   irsensorState = digitalRead(irsensorPin);  // read the state of the ir sensor

if (sensorroundcount <=5 && irsensorState == LOW){ //Logical AND, both have to be true.
   sensorroundcount++; //increment roundCount
   getround(); 
}
else {}
}


void getround()
{  
//Move arm to pick up round:
    digitalWrite(go_ledPin, LOW);
    digitalWrite(stop_ledPin, HIGH);