Trying to take two value from two arrays and add them in a function in my sketch

Greetings,
Trying to take values from two different arrays and add their values into my sketch. So basically with one variable I could call the same indexed value from two arrays and have them set as values into the sketch. My sketch looks like this:

#include <Servo.h>  // Add Servo library to the sketch

Servo myservo;      // Declare myservo into the sketch




int main(void)      // Initialize stepes I want the loop to follow

{
  init();           
  setup();
  ReadSerial();
  ForwardX();        // The main loop of the program will begin by running the Stepper Motors by the commands inside the FowardX first and move to the next step and so on until it reaches ReverseY then will completely stop at original starting position 
  ForwardY();          
  Sweepservo();
  ReverseX();
  ReverseY();
  return 0;
}


void setup(){
  Serial.begin(9600);
  pinMode(6, OUTPUT);  // Set the mode to pin 6 as an output, pin 6 is the Y Stepper Direction Pin from the Easy Driver which tells the Stepper Motor which direction to turn
  pinMode(7, OUTPUT);  // set the mode to pin 7 as an output, pin 7 is the Y Stepper Step Pin which makes the Stepper Motor turn depending on the values given
  pinMode(8, OUTPUT);  // Set the mode to pin 8 as an output, pin 8 is the X Stepper Direction Pin from the Easy Driver which tells the Stepper Motor which direction to turn
  pinMode(9, OUTPUT);  // set the mode to pin 9 as an output, pin 9 is the X Stepper Step Pin which makes the Stepper Motor turn depending on the values given

}
void ReadSerial(){
long  XArray[3] = {
    165000, 30000    };  // position 0 is value  drawer 1, position 1 is value for drawer 2; 
long  YArray[2] = {
    180000, 30000    };  // position 0 is value for drawer 1, and postion 1 is value for drawer 2; 
long n;

  for (n = 0; n <= 1; n++)  
  {
    Serial.println("Please choose the drawer you would like me to open: ");
    Serial.read = XArray[n] && YArray[n];
  }
}

void ForwardX(){                       // X Stepper Motor begins to turn Counterclockwise  
  long int X = 0;                      // Set variable X as a long intiger
  for(X = 0; X < XArray[n]; X += 1)    // Make X equal to zero; If X is less than 165000; Incrament by 1
  {
    digitalWrite(8, HIGH);          // Sets pin 8 as a digital pin, and sets it as a High
    digitalWrite(9, HIGH);          // Sets pin 9 as a digital pin, and sets it as a High
    delayMicroseconds(150);         // Waits 150 Microseconds 
    digitalWrite(9, LOW);           // Sets pin 9 as a digital pin, and sets it as a Low
    delayMicroseconds(150);         // Waits 150 Microseconds
  }
}
void ForwardY() {                   // Y Stepper Motor begins to turn Counterclockwise
  long int Y = 0;                   // Set variable Y as a long intiger
  for(Y = 0; Y < YArray[n]; Y += 1)    // Make Y equal to zero; If X is less than 180000; Incrament by 1
  {
    digitalWrite(6, HIGH);          // Sets pin 6 as a digital pin, and sets it as a High
    digitalWrite(7, HIGH);          // Sets pin 7 as a digital pin, and sets it as a High
    delayMicroseconds(150);         // Waits 150 Microseconds 
    digitalWrite(7, LOW);           // Sets pin 7 as a digital pin, and sets it as a Low
    delayMicroseconds(150);         // Waits 150 Microseconds
  }
}
void Sweepservo()                      // Servo begins turning
{                                        
  int pos = 180;                       // Make pos an intiger which equals 180
  myservo.attach(13);                  // Tell Arduino that Servo is connected to pin 13
  for(pos = 180; pos >= 0; pos -= 1)   // pos equals 180; if pos is less than or equal to zero; decrement by one and move to that position until it reaches zero
  {
    myservo.write(pos);                // Sets back to original position
    delay(15);                         
  }
  myservo.detach();                    // Detache the Servo from pin 13 to make it stop
}
void ReverseX(){                       // X Stepper Motor begins to turn Clockwise
  long int X = 0;                      // Set variable X as a long intiger
  for(X = 0; X <= XArray[n]; X += 1)      // Set variable X equal to zero; if X is less than or equal to 164000; Incrament by 1
  { 
    digitalWrite(8, LOW);              // Sets pin 8 as a digital pin, and sets it as a Low
    digitalWrite(9, LOW);              // Sets pin 9 as a digital pin, and sets it as a Low
    delayMicroseconds(150);            // Waits 150 Microseconds 
    digitalWrite(9, HIGH);             // Sets pin 9 as a digital pin, and sets it as a High
    delayMicroseconds(150);            // Waits 150 Microseconds 
  }
}
void ReverseY(){                       // Y Stepper Motor command to begin rotation Clockwise
  long int Y = 0;                      // Set variable Y as a long intiger
  for(Y = 0; Y <= YArray[n]; Y += 1)      // Make Y equal to zero; if Y is less than or equal to 180000; Incrament by 1
  {
    digitalWrite(6, LOW);              // Sets pin 6 as a digital pin, and sets it as a Low
    digitalWrite(7, LOW);              // Sets pin 7 as a digital pin, and sets it as a Low
    delayMicroseconds(150);            // Waits 150 Microseconds
    digitalWrite(7, HIGH);             // Sets pin 7 as a digital pin, and sets it as a High
    delayMicroseconds(150);            // Waits 150 Microseconds
  }
}

Added the for function to read the index value from the serial monitor and just want to know if this is the correct way to do this. Thank you in advance for any tips on making this work.

Sam3408

try to improve on your commenting skills

pinMode(6, OUTPUT);  // Set the mode to pin 6 as an output, pin 6 is the Y Stepper Direction Pin from the Easy Driver which tells the Stepper Motor which direction to turn

could be

pinMode(6, OUTPUT);  // Y Stepper Direction Pin from Easy Driver: LOW = left HIGH = right

It does not repeat the code, and is more informative. (LOW & HIGH may be the other way around)

even better

#define Y_STEPPER_DIRECTION_PIN 6
...
pinMode(Y_STEPPER_DIRECTION_PIN, OUTPUT);  // LOW = left HIGH = right

make your as code self documenting as possible.

void ReadSerial(){
long  XArray[3] = {
    165000, 30000    };  // position 0 is value  drawer 1, position 1 is value for drawer 2; 
long  YArray[2] = {
    180000, 30000    };  // position 0 is value for drawer 1, and postion 1 is value for drawer 2; 
long n;

  for (n = 0; n <= 1; n++)  
  {
    Serial.println("Please choose the drawer you would like me to open: ");
    Serial.read = XArray[n] && YArray[n];
  }
}

will not work, you need to check the Serial.available() before you can read from it.
the array’s only exist within the function - google scope of variables in C - make them global

A try to rewrite the code, not tested but it still might help

long  DrawerPositionX[2] = { 165000, 30000    };  
long  DrawerPositionY[2] = { 180000, 30000    };  

// -1 => none selected     
// 0 => drawer 1     
// 1 => drawer 2;
// other values => invalid
int currentDrawer = -1;    // to be used as index for the array's

void ReadSerial()
{
  bool done = false;

  Serial.println("Please choose the drawer you would like me to open:  (X for exit) ");
  while (!done)
  {
    while (Serial.avaliable() == 0); // wait for input
    int c = Serial.read(); // read incoming char
    switch(c)
    {
      case '1': 
        currentDrawer = 0;
        done = true;
        break;
      case '2': 
        currentDrawer = 1;
        done = true;
        break;
      case 'X': 
        done = true;
        break;
    }
  }
}

Thank you for the response robtillaart, Yes I need to improve my commenting skills, working on that. Will do some research on scope of variables. Going to rewrite the code with the idea you have given me. Thank you will post results up soon.

Yes I need to improve my commenting skills, working on that.

  • do not repeat the code
  • explain the WHY or semantics, not the HOW
  • use names that cover the purpose -> both for functions and for variables and for everything

readSerial() is a bad name in that sense as it is a HOW name

selectDrawerFromUser() is a descriptive name although a bit long selectDrawer() on the other hand might be ambiguous

While symbol naming conventions are a contentious subject no one will argue that good and consistent naming of symbols is not of value.

‘Good’, whatever that may be, naming conventions can be useful in making easily readable code lessening the need for comments of the form seen in your sketch which can drift out of sync with the code leading to unneccessarry confusion.

Bad or wrong comments are WORSE than no comments at all.

What follows is your code redone slightly to give you an idea of what some naming conventions can do for readability.

There are also a few corrections to integer literals having the suffix ‘L’ appended to correct what would otherwise introduce ranges errors.

And the ‘A_SIZEOF’ macro shows how to keep from hardcoding magic numbers associated with array sizes in your code.

// <http://forum.arduino.cc/index.php?topic=167321.0>

#include <Servo.h>

#define A_SIZEOF(ARRAY)     (sizeof(ARRAY) / sizeof(ARRAY[0]))

const uint8_t   pinSTEPPER                      = 13;

const uint8_t   pinSTEPPER_Y_DIR                = 6;
const uint8_t   pinSTEPPER_X_DIR                = 8;

const uint8_t   pinSTEPPER_Y_ADVANCE            = 7;
const uint8_t   pinSTEPPER_X_ADVANCE            = 9;

const uint8_t   STEPPER_DIR_COUNTER_CLOCKWISE   = HIGH;
const uint8_t   STEPPER_DIR_CLOCKWISE           = LOW;

Servo servo;

long xDrawer[] = { 165000L, 30000L };       // position 0 is value  drawer 1, position 1 is value for drawer 2;
long yDrawer[] = { 180000L, 30000L };       // position 0 is value for drawer 1, and postion 1 is value for drawer 2;


void ReverseY()
{
    for ( size_t y = 0; y <= yDrawer[n]; y++ )
    {
        digitalWrite(pinSTEPPER_Y_DIR, STEPPER_DIR_CLOCKWISE);

        digitalWrite(pinSTEPPER_Y_ADVANCE, LOW);
        delayMicroseconds(150);

        digitalWrite(pinSTEPPER_Y_ADVANCE, HIGH);
        delayMicroseconds(150);
    }
}

// X Stepper Motor begins to turn Clockwise
void ReverseX()
{
    for ( size_t x = 0; x <= xDrawer[n]; x++ )
    {
        digitalWrite(pinSTEPPER_X_DIR, STEPPER_DIR_CLOCKWISE);

        digitalWrite(pinSTEPPER_X_ADVANCE, LOW);
        delayMicroseconds(150);

        digitalWrite(pinSTEPPER_X_ADVANCE, HIGH);
        delayMicroseconds(150);
    }
}

void Sweepservo()
{
    for ( size_t pos = 180; pos--; )
    {
        servo.write(pos);   // Sets back to original position
        delay(15UL);
    }
}


// Y Stepper Motor begins to turn Counterclockwise
void ForwardY()
{
    for ( size_t y = 0; y < yDrawer[n]; y++ )
    {
        digitalWrite(pinSTEPPER_Y_DIR, STEPPER_DIR_COUNTER_CLOCKWISE);

        digitalWrite(pinSTEPPER_Y_ADVANCE, HIGH);
        delayMicroseconds(150);

        digitalWrite(pinSTEPPER_Y_ADVANCE, LOW);
        delayMicroseconds(150);
    }
}


// X Stepper Motor begins to turn Counterclockwise
void ForwardX()
{
    for ( size_t x = 0; x < xDrawer[n]; x++ )
    {
        digitalWrite(pinSTEPPER_X_DIR, STEPPER_DIR_COUNTER_CLOCKWISE);

        digitalWrite(pinSTEPPER_X_ADVANCE, HIGH);
        delayMicroseconds(150);

        digitalWrite(pinSTEPPER_X_ADVANCE, LOW);
        delayMicroseconds(150);
    }
}

void ReadSerial()
{
    for ( size_t n = 0; n < A_SIZEOF(xDrawer); n++ )
    {
        Serial.println("Please choose the drawer you would like me to open: ");
        Serial.read = xDrawer[n] && yDrawer[n];
    }
}

int loop()
{   }

void setup()
{
    Serial.begin(9600);

    servo.attach(pinSTEPPER);

    pinMode(pinSTEPPER_Y_DIR, OUTPUT);
    pinMode(pinSTEPPER_Y_ADVANCE, OUTPUT);
    pinMode(pinSTEPPER_X_DIR, OUTPUT);
    pinMode(pinSTEPPER_X_ADVANCE, OUTPUT);


    // Initialize steps I want the loop to follow

    ReadSerial();


    // The main loop of the program will begin by running the Stepper Motors by
    // the commands inside the FowardX first and move to the next step and so on
    // until it reaches ReverseY then will completely stop at original starting
    // position

    ForwardX();
    ForwardY();
    Sweepservo();
    ReverseX();
    ReverseY();
}

Thanks for the input guys, I have worked on the code today and I got it to work fine using the switch statement. Still needs some labeling improvement but I am still having trouble with this part of the sketch:

int main(void)      // Initialize stepes I want the loop to follow

{
  bool done = false;
  init();           
  setup();
  do{
  selectdrawerfromuser();
  ForwardX(currentDrawer);
  ForwardY(currentDrawer);          
  Sweepservo();
  ReverseX(currentDrawer);
  ReverseY(currentDrawer);
  Serial.begin(9600);
  Serial.println("would you like to open another drawer:  (Y for yes, N for exit)");
  while(!done){
  while(Serial.available() ==0);}
  char c = Serial.read();
  }
  while (c == 'Y' && c != 'N');
  return 0;
}


void setup(){
  Serial.begin(9600);
  pinMode(6, OUTPUT);  // Set the mode to pin 6 as an output
  pinMode(7, OUTPUT);  // set the mode to pin 7 as an output 
  pinMode(8, OUTPUT);  // Set the mode to pin 8 as an output
  pinMode(9, OUTPUT);  // set the mode to pin 9 as an output

}
long selectdrawerfromuser()
{
  bool done = false;
  Serial.println("Please choose the drawer you would like me to open:  (X for exit) ");
  while (!done)
  {
    while (Serial.available() == 0); // wait for input
    char ch = Serial.read(); // read incoming char
    switch(ch)
      {
      case '1': 
        currentDrawer = 0;
        done = true;
        return currentDrawer;
        break;
      case '2': 
        currentDrawer = 1;
        done = true;
        return currentDrawer;
        break;
      case 'X': 
        done = true;
        break;
           
      }
    }
  }

After uploading the sketch to the Arduino, I run the serial monitor it prompts me with a question ("Please choose the drawer you would like me to open: (X for exit) "), entering 1 and 2 works fine and the steppers run to their perspective positions and the servo functions the way I want it to. A problem still arises when the steppers return to the reset position another comment comes on the serial monitor which is,(“would you like to open another drawer: (Y for yes, N for exit)”), when I enter Y it should ask me the first question again but it does not. The only way it works again is if I press the reset button on the Arduino. So my question is, how can I get it to bring up the first question which is, ("Please choose the drawer you would like me to open: (X for exit) ") and open a drawer without having to press the reset button on the Arduino. Thanks for any input. Here is the entire code just in case:

#include <Servo.h>  // Add Servo library to the sketch
#define Y_STEPPER_DIRECTION_PIN 6
#define Y_STEPPER_STEP_PIN 7
#define X_STEPPER_DIRECTION_PIN 8
#define X_STEPPER_STEP_PIN 9

Servo myservo;      // Declare myservo into the sketch

long  DrawerPositionX[] = {165000L, 30000L};  
long  DrawerPositionY[] = {180000L, 30000L};  

// -1 => none selected     
// 0 => drawer 1     
// 1 => drawer 2;
// other values => invalid
int currentDrawer = -1;    // to be used as index for the array's
char c =0;

int main(void)      // Initialize stepes I want the loop to follow

{
  bool done = false;
  init();           
  setup();
  do{
  selectdrawerfromuser();
  ForwardX(currentDrawer);
  ForwardY(currentDrawer);          
  Sweepservo();
  ReverseX(currentDrawer);
  ReverseY(currentDrawer);
  Serial.begin(9600);
  Serial.println("would you like to open another drawer:  (Y for yes, N for exit)");
  while(!done){
  while(Serial.available() ==0);}
  char c = Serial.read();
  }
  while (c == 'Y' && c != 'N');
  return 0;
}


void setup(){
  Serial.begin(9600);
  pinMode(6, OUTPUT);  // Set the mode to pin 6 as an output
  pinMode(7, OUTPUT);  // set the mode to pin 7 as an output 
  pinMode(8, OUTPUT);  // Set the mode to pin 8 as an output
  pinMode(9, OUTPUT);  // set the mode to pin 9 as an output

}
long selectdrawerfromuser()
{
  bool done = false;
  Serial.println("Please choose the drawer you would like me to open:  (X for exit) ");
  while (!done)
  {
    while (Serial.available() == 0); // wait for input
    char ch = Serial.read(); // read incoming char
    switch(ch)
      {
      case '1': 
        currentDrawer = 0;
        done = true;
        return currentDrawer;
        break;
      case '2': 
        currentDrawer = 1;
        done = true;
        return currentDrawer;
        break;
      case 'X': 
        done = true;
        break;
           
      }
    }
  }
long ForwardX( int currentDrawer){                      // X Stepper Motor begins to turn Counterclockwise  
  long int X = 0;                      // Set variable X as a long intiger
  for(X = 0; X < DrawerPositionX[currentDrawer]; X += 1)    // Make X equal to zero; If X is less than 165000; Incrament by 1
  {
    digitalWrite(8, HIGH);          // Sets pin 8 as a digital pin, and sets it as a High
    digitalWrite(9, HIGH);          // Sets pin 9 as a digital pin, and sets it as a High
    delayMicroseconds(150);         // Waits 150 Microseconds 
    digitalWrite(9, LOW);           // Sets pin 9 as a digital pin, and sets it as a Low
    delayMicroseconds(150);         // Waits 150 Microseconds
  }
}
long ForwardY(int currentDrawer) {                   // Y Stepper Motor begins to turn Counterclockwise
  long int Y = 0;                   // Set variable Y as a long intiger
  for(Y = 0; Y < DrawerPositionY[currentDrawer]; Y += 1)    // Make Y equal to zero; If X is less than 180000; Incrament by 1
  {
    digitalWrite(6, HIGH);          // Sets pin 6 as a digital pin, and sets it as a High
    digitalWrite(7, HIGH);          // Sets pin 7 as a digital pin, and sets it as a High
    delayMicroseconds(150);         // Waits 150 Microseconds 
    digitalWrite(7, LOW);           // Sets pin 7 as a digital pin, and sets it as a Low
    delayMicroseconds(150);         // Waits 150 Microseconds
  }
}
void Sweepservo()                      // Servo begins turning
{                                        
  int pos = 180;                       // Make pos an intiger which equals 180
  myservo.attach(13);                  // Tell Arduino that Servo is connected to pin 13
  for(pos = 180; pos >= 0; pos -= 1)   // pos equals 180; if pos is less than or equal to zero; decrement by one and move to that position until it reaches zero
  {
    myservo.write(pos);                // Sets back to original position
    delay(15);                         
  }
  myservo.detach();                    // Detache the Servo from pin 13 to make it stop
}
long ReverseX(int currentDarwer){                       // X Stepper Motor begins to turn Clockwise
  long int X = 0;                      // Set variable X as a long intiger
  for(X = 0; X <= DrawerPositionX[currentDrawer]; X += 1)      // Set variable X equal to zero; if X is less than or equal to 164000; Incrament by 1
  { 
    digitalWrite(8, LOW);              // Sets pin 8 as a digital pin, and sets it as a Low
    digitalWrite(9, LOW);              // Sets pin 9 as a digital pin, and sets it as a Low
    delayMicroseconds(150);            // Waits 150 Microseconds 
    digitalWrite(9, HIGH);             // Sets pin 9 as a digital pin, and sets it as a High
    delayMicroseconds(150);            // Waits 150 Microseconds 
  }
}
long ReverseY(int currentDrawer){                       // Y Stepper Motor command to begin rotation Clockwise
  long int Y = 0;                      // Set variable Y as a long intiger
  for(Y = 0; Y <= DrawerPositionY[currentDrawer]; Y += 1)      // Make Y equal to zero; if Y is less than or equal to 180000; Incrament by 1
  {
    digitalWrite(6, LOW);              // Sets pin 6 as a digital pin, and sets it as a Low
    digitalWrite(7, LOW);              // Sets pin 7 as a digital pin, and sets it as a Low
    delayMicroseconds(150);            // Waits 150 Microseconds
    digitalWrite(7, HIGH);             // Sets pin 7 as a digital pin, and sets it as a High
    delayMicroseconds(150);            // Waits 150 Microseconds
  }

}

Sam3408