Asking for some general advice!

Hi!

When setting up libraries with different settings, is it possible to put the name of the instance in a for loop and loop through it?

I’m probably not explaining myself correct, but let me show with code:

stepper1.setMaxSpeed(maxSpeed);
stepper1.setAcceleration(acc);

“stepper1” is what I want to put in a for loop, so I don’t have to repeat so much code when changing settings for the steppers.

I also want you to take a look at a function I wrote for controlling 8 relays, what you would have done different etc.

boolean relays[8];

boolean relay(int relay, boolean highLow){
  //////////////////////////////////////////////////////////////////////////////
  if(highLow){                            // if highLow == HIGH
    mcp.digitalWrite(relay, HIGH);        // Write HIGH to relayPin
    delay(1);                             // Wait just a little tad
    byte DR = mcp.digitalRead(15-relay);  // Read output of pin
    if(DR){                               // If HIGH
      relays[relay] = true;               // Set relay as ON in array
      return 1;                           // Return 1, relay is ON
    }
    else if(!DR){                         // else if pin is still LOW // This is IF something goes wrong with the first write
      mcp.digitalWrite(relay, HIGH);      // Do a new write with HIGH
      delay(1);                           // Wait a little before reading
      if(mcp.digitalRead(15-relay)){      // If actually HIGH this time
        relays[relay] = true;             // Then set relay as ON in array
        return 1;                         // Return 1, relay is ON
      }
    }
  }//////////////////////////////////////////////////////////////////////////////
  else if(!highLow){                      // if highLow == LOW
    mcp.digitalWrite(relay, LOW);         // Write relayPin, LOW
    delay(1);                             // Wait
    byte DR = mcp.digitalRead(15-relay);  // Read output pin
    if(!DR){                              // If pin is LOW
      relays[relay] = false;              // Set relay as OFF
      return 0;                           // Return 0, relay is OFF
    }
    else if(DR){                          // else if output is still HIGH // This is IF something goes wrong with the first write
      mcp.digitalWrite(relay, LOW);       // Do a new write to relay pin      
      delay(1);                           // Wait
      if(!mcp.digitalRead(15-relay)){     // Do a new read, and if LOW
        relays[relay] = false;            // Set relay as OFF
        return 0;                         // Return 0, relay is OFF
      }
    } 
  }
}

Thanks!

I think you need to look at arrays, but I'm not sure.

When setting up libraries with different settings, is it possible to put the name of the instance in a for loop and loop through it?

No. Names exist at compile time. Once compilation is complete, names are history.

You could, of course, create an array of Stepper objects, and iterate over the array.

what you would have done different etc.

I would have used some names that made sense. I can't begin to guess whether highLow == true means that the pin is HIGH or LOW. Now, if the name were isHIGH, then when the value was true, no one would assume that the pin was LOW. When the value was false, no one would assume that the pin state was HIGH. With the highLow name, true must mean that the pin is HIGH or LOW, and false must mean that it isn't.

   if(DR){                               // If HIGH
if(DR == HIGH) // No useless comment needed
{

By convention all capital letter names are reserved for constants. Constants NEVER appear to the left of an equal sign (except for the initial assignment).

   if(!DR){                              // If pin is LOW
    else if(DR){                          // else if output is still HIGH // This is IF something goes wrong with the first write

Always good to be prepared for the introduction of tri-state pins. Not!

If the pin isn't HIGH, there is NO OTHER STATE besides LOW.

Instead of all the obvious comments on each line, a couple of lines before the function that explained why it even exists would be far better.