Code controls 2 stepper motors without using libraries. Need help! Cannot solve problem: sketch.ino:31:19: error: variable or field '<>' declared void

Dear Forum members, this is the first time I use the Forum, however, I have been using Arduino for a while.

Apologies if I make any mistakes using the Forum, such as choosing wrong category or adressing the wrong participants. My goal was to send a question to the whole Forum in the appropriate category.

I am trying to further develop my coding skills by using functions and arrays, making codes cleaner and more efficient. The code posted below returns several erros of the same kind (example below):

Blockquote

sketch.ino:31:19: error: variable or field 'stepM1Run' declared void
void stepM1Run(stepBM1,stepEM1,amplitudeM1);
^~~~~~~
sketch.ino:32:19: error: variable or field 'stepM1Run' declared void
void stepM1Run(stepBM1,stepEM1,amplitudeM1) {
^~~~~~~

Blockquote

I have tried finding solutions for a while now. I have tried moving the functions to the top, bottom, but nothing seems to work. The code controls 2 stepper motors without using libraries. It works perfectly in version one without the functions and arrays.

could you please try to help?

Thank you.

Full code posted below.

// stepper2

// defines pins

const int openSensorPin = 2;
const int closeSensorPin = 3;

const int stepPinM1 = 4;
const int dirPinM1 = 5;

const int stepPinM2 = 9;
const int dirPinM2 = 10;

// defines variables

int stepBM1[13] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
int stepEM1[13] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
int amplitudeM1[13] = {1,2,3,4,5,6,7,8,9,10,11,12,13};

int stepBM2[13] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
int stepEM2[13] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
int amplitudeM2[13] = {1,2,3,4,5,6,7,8,9,10,11,12,13};

int i = 0;
int stepMotorM1 = 0;
int stepMotorM2 = 0;  

// Define Functions

   void stepM1Run(stepBM1,stepEM1,amplitudeM1);
   void stepM1Run(stepBM1,stepEM1,amplitudeM1) {
    
     for(stepMotorM1 = int stepBM1; stepMotorM1 < int stepEM1; stepMotorM1++) {        
     digitalWrite(stepPinM1,HIGH); 
     delayMicroseconds(int amplitudeM1);                                          
     digitalWrite(stepPinM1,LOW); 
     delayMicroseconds(int amplitudeM1); 
     } 
   }   
   
   void stepM2Run(stepBM2,stepEM2,amplitudeM2);
   void stepM2Run(stepBM2,stepEM2,amplitudeM2){
    
     for(stepMotorM2 = int stepBM2; stepMotorM2 < int stepEM2; stepMotorM2++) {        
     digitalWrite(stepPinM2,HIGH); 
     delayMicroseconds(int amplitudeM2);                                          
     digitalWrite(stepPinM2,LOW); 
     delayMicroseconds(int amplitudeM2); 
    
     }
   } 

void setup() {

  //sets serial

  Serial.begin(9600);   
  
  // sets pins

  pinMode (openSensorPin,INPUT_PULLUP);
  pinMode (closeSensorPin,INPUT_PULLUP);

  pinMode(stepPinM1,OUTPUT); 
  pinMode(dirPinM1,OUTPUT);

  pinMode (stepPinM2,OUTPUT);
  pinMode (dirPinM2,OUTPUT);
 
}

void loop() {

  // Open Routine

  if (digitalRead (openSensorPin) == LOW) {                           

    digitalWrite(dirPinM1,LOW);                                     
    digitalWrite(dirPinM2,LOW);

    for(i=0;i<=12;i++) {
     
      stepM1Run(stepBM1[i], stepEM1[i], amplitudeM1[i]);
      stepM2Run(stepBM2[i], stepEM2[i], amplitudeM2[i]);
    }
  }  
    
   // Close Routine

   if(digitalRead(closeSensorPin) == LOW); {                           
   
    digitalWrite(dirPinM1,HIGH);
    digitalWrite(dirPinM2,HIGH);                                    

     for(i=0;i<=12;i++) {
     
      stepM1Run(stepBM1[i], stepEM1[i], amplitudeM1[i]);
      stepM2Run(stepBM2[i], stepEM2[i], amplitudeM2[i]);
     } 
  }  
}

if it helps, the original code that works perfectly but is very long and not at all optimized (no functions or arrays) is fully posted below.

Thank you again.

// stepper1

// defines pins

const int openSensorPin = 2;
const int closeSensorPin = 3;

const int stepPinMov = 4;
const int dirPinMov = 5;

const int stepPinExp = 9;
const int dirPinExp = 10;


void setup() {

  // sets pins

  pinMode (openSensorPin,INPUT_PULLUP);
  pinMode (closeSensorPin,INPUT_PULLUP);

  pinMode(stepPinMov,OUTPUT); 
  pinMode(dirPinMov,OUTPUT);

  pinMode (stepPinExp,OUTPUT);
  pinMode (dirPinExp,OUTPUT);
 
}


void loop() {

 Serial.begin(9600);                                                  // use the serial port

  if (digitalRead (openSensorPin) == LOW) {                           // open door

    digitalWrite(dirPinExp,LOW);                                      // enables the motor to move counterclockwise
 
    int stepMotorExp = 0;                                             //creates a variable integer called 'stepMotorExp'

    for(stepMotorExp = 0; stepMotorExp < 50; stepMotorExp++) {        // rotates motor (200 pulses for 1 revolution and 2mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(4000);                                          // defines speed
    digitalWrite(stepPinExp,LOW); 
    delayMicroseconds(4000); 
    }
  
    for(stepMotorExp = 50; stepMotorExp < 125; stepMotorExp++) {      // rotates motor (200 pulses for 1 revolution and 2mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(2667);                                          // defines speed
    digitalWrite(stepPinExp,LOW); 
    delayMicroseconds(2667); 
    }

    for(stepMotorExp = 125; stepMotorExp < 250; stepMotorExp++) {     // rotates motor (200 pulses for 1 revolution and 2mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(1600);                                          // defines speed
    digitalWrite(stepPinExp,LOW);   
    delayMicroseconds(1600); 
    }

    for(stepMotorExp = 250; stepMotorExp < 450; stepMotorExp++) {     // rotates motor (200 pulses for 1 revolution and 2mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(1000);                                          // defines speed
    digitalWrite(stepPinExp,LOW); 
    delayMicroseconds(1000); 
    }

    for(stepMotorExp = 450; stepMotorExp < 775; stepMotorExp++) {     // rotates motor (200 pulses for 1 revolution and 2mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(615);                                           // defines speed
    digitalWrite(stepPinExp,LOW); 
    delayMicroseconds(615); 
    }

    for(stepMotorExp = 775; stepMotorExp < 1250; stepMotorExp++) {    // rotates motor (200 pulses for 1 revolution and 2mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(421);                                           // defines speed
    digitalWrite(stepPinExp,LOW); 
    delayMicroseconds(421); 
    }

    for(stepMotorExp = 1250; stepMotorExp < 3750; stepMotorExp++) {   // rotates motor (200 pulses for 1 revolution and 2mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(400);                                           // defines speed
    digitalWrite(stepPinExp,LOW); 
    delayMicroseconds(400); 
    }

    for(stepMotorExp = 3750; stepMotorExp < 4225 ; stepMotorExp++) {  // rotates motor (200 pulses for 1 revolution and 2mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(421);                                           // defines speed
    digitalWrite(stepPinExp,LOW); 
    delayMicroseconds(421); 
    }

    for(stepMotorExp = 4225; stepMotorExp < 4550; stepMotorExp++) {   // rotates motor (200 pulses for 1 revolution and 2mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(615);                                           // defines speed
    digitalWrite(stepPinExp,LOW); 
    delayMicroseconds(615); 
    }

    for(stepMotorExp = 4550; stepMotorExp < 4750; stepMotorExp++) {   // rotates motor (200 pulses for 1 revolution and 2mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(1000);                                          // defines speed
    digitalWrite(stepPinExp,LOW); 
    delayMicroseconds(1000); 
    }

    for(stepMotorExp = 4750; stepMotorExp < 4875; stepMotorExp++) {   // rotates motor (200 pulses for 1 revolution and 2mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(1600);                                          // defines speed
    digitalWrite(stepPinExp,LOW); 
    delayMicroseconds(1600); 
    }

    for(stepMotorExp = 4875; stepMotorExp < 4950; stepMotorExp++) {   // rotates motor (200 pulses for 1 revolution and 2mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(2667);                                          // defines speed
    digitalWrite(stepPinExp,LOW); 
    delayMicroseconds(2667); 
    }

    for(stepMotorExp = 4950; stepMotorExp < 5000; stepMotorExp++) {   // rotates motor (200 pulses for 1 revolution and 2mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(4000);                                          // defines speed
    digitalWrite(stepPinExp,LOW); 
    delayMicroseconds(4000); 
    }

    delay (500);
  
    digitalWrite(dirPinMov,LOW);                                      // enables the motor to move counterclockwise
 
    int stepMotorMov = 0;                                             //creates a variable integer called 'stepMotor'

    for(stepMotorMov = 0; stepMotorMov < 50; stepMotorMov++) {        // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(4000);                                          // defines speed
    digitalWrite(stepPinMov,LOW); 
    delayMicroseconds(4000); 
    }
  
    for(stepMotorMov = 50; stepMotorMov < 125; stepMotorMov++) {      // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(2667);                                          // defines speed
    digitalWrite(stepPinMov,LOW); 
    delayMicroseconds(2667); 
    }

    for(stepMotorMov = 125; stepMotorMov < 250; stepMotorMov++) {     // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(1600);                                          // defines speed
    digitalWrite(stepPinMov,LOW);   
    delayMicroseconds(1600); 
    }

    for(stepMotorMov = 250; stepMotorMov < 450; stepMotorMov++) {     // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(1000);                                          // defines speed
    digitalWrite(stepPinMov,LOW); 
    delayMicroseconds(1000); 
    }

    for(stepMotorMov = 450; stepMotorMov < 775; stepMotorMov++) {     // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(615);                                           // defines speed
    digitalWrite(stepPinMov,LOW); 
    delayMicroseconds(615); 
    }

    for(stepMotorMov = 775; stepMotorMov < 1250; stepMotorMov++) {    // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(421);                                           // defines speed
    digitalWrite(stepPinMov,LOW); 
    delayMicroseconds(421); 
    }

    for(stepMotorMov = 1250; stepMotorMov < 3750; stepMotorMov++) {   // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(400);                                           // defines speed
    digitalWrite(stepPinMov,LOW); 
    delayMicroseconds(400); 
    }

    for(stepMotorMov = 3750; stepMotorMov < 4225 ; stepMotorMov++) {  // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(421);                                           // defines speed
    digitalWrite(stepPinMov,LOW); 
    delayMicroseconds(421); 
    }

    for(stepMotorMov = 4225; stepMotorMov < 4550; stepMotorMov++) {   // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(615);                                  // defines speed
    digitalWrite(stepPinMov,LOW); 
    delayMicroseconds(615); 
    }

    for(stepMotorMov = 4550; stepMotorMov < 4750; stepMotorMov++) {   // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(1000);                                          // defines speed
    digitalWrite(stepPinMov,LOW); 
    delayMicroseconds(1000); 
    }

    for(stepMotorMov = 4750; stepMotorMov < 4875; stepMotorMov++) {   // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(1600);                                          // defines speed
    digitalWrite(stepPinMov,LOW); 
    delayMicroseconds(1600); 
    }

    for(stepMotorMov = 4875; stepMotorMov < 4950; stepMotorMov++) {     // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(2667);                                         // defines speed
    digitalWrite(stepPinMov,LOW); 
    delayMicroseconds(2667); 
    }

    for(stepMotorMov = 4950; stepMotorMov < 5000; stepMotorMov++) {     // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(4000);                                         // defines speed
    digitalWrite(stepPinMov,LOW); 
    delayMicroseconds(4000); 
    }

  }

  if(digitalRead(closeSensorPin) == LOW) {                          // close door
   
    digitalWrite(dirPinMov,HIGH);                                    // enables the motor to move counterclockwise
 
    int stepMotorMov = 0;                                            //creates a variable integer called 'stepMotor'

    for(stepMotorMov = 0; stepMotorMov < 50; stepMotorMov++) {       // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(4000);                                         // defines speed
    digitalWrite(stepPinMov,LOW); 
    delayMicroseconds(4000); 
    }
  
    for(stepMotorMov = 50; stepMotorMov < 125; stepMotorMov++) {      // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(2667);                                          // defines speed
    digitalWrite(stepPinMov,LOW); 
    delayMicroseconds(2667); 
    }

    for(stepMotorMov = 125; stepMotorMov < 250; stepMotorMov++) {     // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(1600);                                          // defines speed
    digitalWrite(stepPinMov,LOW);   
    delayMicroseconds(1600); 
    }

    for(stepMotorMov = 250; stepMotorMov < 450; stepMotorMov++) {     // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(1000);                                          // defines speed
    digitalWrite(stepPinMov,LOW); 
    delayMicroseconds(1000); 
    }

    for(stepMotorMov = 450; stepMotorMov < 775; stepMotorMov++) {     // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(615);                                           // defines speed
    digitalWrite(stepPinMov,LOW); 
    delayMicroseconds(615); 
    }

    for(stepMotorMov = 775; stepMotorMov < 1250; stepMotorMov++) {    // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(421);                                           // defines speed
    digitalWrite(stepPinMov,LOW); 
    delayMicroseconds(421); 
    }

    for(stepMotorMov = 1250; stepMotorMov < 3750; stepMotorMov++) {   // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(400);                                           // defines speed
    digitalWrite(stepPinMov,LOW); 
    delayMicroseconds(400); 
    }

    for(stepMotorMov = 3750; stepMotorMov < 4225 ; stepMotorMov++) {  // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(421);                                           // defines speed
    digitalWrite(stepPinMov,LOW); 
    delayMicroseconds(421); 
    }

    for(stepMotorMov = 4225; stepMotorMov < 4550; stepMotorMov++) {   // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(615);                                           // defines speed
    digitalWrite(stepPinMov,LOW); 
    delayMicroseconds(615); 
    }

    for(stepMotorMov = 4550; stepMotorMov < 4750; stepMotorMov++) {   // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(1000);                                          // defines speed
    digitalWrite(stepPinMov,LOW); 
    delayMicroseconds(1000); 
    }

    for(stepMotorMov = 4750; stepMotorMov < 4875; stepMotorMov++) {   // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(1600);                                          // defines speed
    digitalWrite(stepPinMov,LOW); 
    delayMicroseconds(1600); 
    }

    for(stepMotorMov = 4875; stepMotorMov < 4950; stepMotorMov++) {   // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(2667);                                          // defines speed
    digitalWrite(stepPinMov,LOW); 
    delayMicroseconds(2667); 
    }

    for(stepMotorMov = 4950; stepMotorMov < 5000; stepMotorMov++) {   // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinMov,HIGH); 
    delayMicroseconds(4000);                                          // defines speed
    digitalWrite(stepPinMov,LOW); 
    delayMicroseconds(4000); 
    }

    delay (500);                                                      // half a second delay
   
    digitalWrite(dirPinExp,HIGH);                                      // enables the motor to move counterclockwise
 
    int stepMotorExp = 0;                                             //creates a variable integer called 'stepMotor'

    for(stepMotorExp = 0; stepMotorExp < 50; stepMotorExp++) {        // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(4000);                                          // defines speed
    digitalWrite(stepPinExp,LOW); 
    delayMicroseconds(4000); 
    }
  
    for(stepMotorExp = 50; stepMotorExp < 125; stepMotorExp++) {      // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(2667);                                          // defines speed
    digitalWrite(stepPinExp,LOW); 
    delayMicroseconds(2667); 
    }

    for(stepMotorExp = 125; stepMotorExp < 250; stepMotorExp++) {     // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(1600);                                          // defines speed
    digitalWrite(stepPinExp,LOW);   
    delayMicroseconds(1600); 
    }

    for(stepMotorExp = 250; stepMotorExp < 450; stepMotorExp++) {     // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(1000);                                          // defines speed
    digitalWrite(stepPinExp,LOW); 
    delayMicroseconds(1000); 
    }

    for(stepMotorExp = 450; stepMotorExp < 775; stepMotorExp++) {     // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(615);                                           // defines speed
    digitalWrite(stepPinExp,LOW); 
    delayMicroseconds(615); 
    }

    for(stepMotorExp = 775; stepMotorExp < 1250; stepMotorExp++) {    // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(421);                                           // defines speed
    digitalWrite(stepPinExp,LOW); 
    delayMicroseconds(421); 
    }

    for(stepMotorExp = 1250; stepMotorExp < 3750; stepMotorExp++) {   // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(400);                                           // defines speed
    digitalWrite(stepPinExp,LOW); 
    delayMicroseconds(400); 
    }

    for(stepMotorExp = 3750; stepMotorExp < 4225 ; stepMotorExp++) {  // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(421);                                           // defines speed
    digitalWrite(stepPinExp,LOW); 
    delayMicroseconds(421); 
    }

    for(stepMotorExp = 4225; stepMotorExp < 4550; stepMotorExp++) {   // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(615);                                           // defines speed
    digitalWrite(stepPinExp,LOW); 
    delayMicroseconds(615); 
    }

    for(stepMotorExp = 4550; stepMotorExp < 4750; stepMotorExp++) {   // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(1000);                                          // defines speed
    digitalWrite(stepPinExp,LOW); 
    delayMicroseconds(1000); 
    }

    for(stepMotorExp = 4750; stepMotorExp < 4875; stepMotorExp++) {   // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(1600);                                          // defines speed
    digitalWrite(stepPinExp,LOW); 
    delayMicroseconds(1600); 
    }

    for(stepMotorExp = 4875; stepMotorExp < 4950; stepMotorExp++) {   // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(2667);                                          // defines speed
    digitalWrite(stepPinExp,LOW); 
    delayMicroseconds(2667); 
    }

    for(stepMotorExp = 4950; stepMotorExp < 5000; stepMotorExp++) {   // rotates motor (200 pulses for 1 revolution and 8mm linear motion)
    digitalWrite(stepPinExp,HIGH); 
    delayMicroseconds(4000);                                          // defines speed
    digitalWrite(stepPinExp,LOW); 
    delayMicroseconds(4000); 
    }                   

  }
 
}  

Thank you for your reply. Yes I have. I wrote the code posted below that works perfectly. However, trying to optimize it, it is the first time I am using arrays and functions. The "int" as you mentioned is a mistake I made using array for the first time. I will remove it and try again.

Thank you again. Would the types replace the "void" or would they go inside the function?

int stepM1Run(stepBM1,stepEM1,amplitudeM1);
   int stepM1Run(stepBM1,stepEM1,amplitudeM1) {

or

void stepM1Run(int stepBM1,int stepEM1,int amplitudeM1);
   void stepM1Run(int stepBM1,int stepEM1,int amplitudeM1) {

?

I got the sketch to compile correctly after your comments. Full code below. I will try using actual parameters to see if the code actually does what it is supposed to do correctly. If it works well I will post full code here again.

Thank you again.

// stepper2

// defines pins

const int openSensorPin = 2;
const int closeSensorPin = 3;

const int stepPinM1 = 4;
const int dirPinM1 = 5;

const int stepPinM2 = 9;
const int dirPinM2 = 10;

// defines variables

int stepBM1[13] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
int stepEM1[13] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
int amplitudeM1[13] = {1,2,3,4,5,6,7,8,9,10,11,12,13};

int stepBM2[13] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
int stepEM2[13] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
int amplitudeM2[13] = {1,2,3,4,5,6,7,8,9,10,11,12,13};

int i = 0;
int stepMotorM1 = 0;
int stepMotorM2 = 0;  

// Define Functions

   
   void stepM1Run(int stepBM1,int stepEM1,int amplitudeM1) {
    
     for(stepMotorM1 = stepBM1; stepMotorM1 < stepEM1; stepMotorM1++) {        
     digitalWrite(stepPinM1,HIGH); 
     delayMicroseconds(amplitudeM1);                                          
     digitalWrite(stepPinM1,LOW); 
     delayMicroseconds(amplitudeM1); 
     } 
   }   
   
   
   void stepM2Run(int stepBM2,int stepEM2,int amplitudeM2){
    
     for(stepMotorM2 = stepBM2; stepMotorM2 < stepEM2; stepMotorM2++) {        
     digitalWrite(stepPinM2,HIGH); 
     delayMicroseconds(amplitudeM2);                                          
     digitalWrite(stepPinM2,LOW); 
     delayMicroseconds(amplitudeM2); 
    
     }
   } 

void setup() {

  //sets serial

  Serial.begin(9600);   
  
  // sets pins

  pinMode (openSensorPin,INPUT_PULLUP);
  pinMode (closeSensorPin,INPUT_PULLUP);

  pinMode(stepPinM1,OUTPUT); 
  pinMode(dirPinM1,OUTPUT);

  pinMode (stepPinM2,OUTPUT);
  pinMode (dirPinM2,OUTPUT);
 
}

void loop() {

  // Open Routine

  if (digitalRead (openSensorPin) == LOW) {                           

    digitalWrite(dirPinM1,LOW);                                     
    digitalWrite(dirPinM2,LOW);

    for(i=0;i<=12;i++) {
     
      stepM1Run(stepBM1[i], stepEM1[i], amplitudeM1[i]);
      stepM2Run(stepBM2[i], stepEM2[i], amplitudeM2[i]);
    }
  }  
    
   // Close Routine

   if(digitalRead(closeSensorPin) == LOW); {                           
   
    digitalWrite(dirPinM1,HIGH);
    digitalWrite(dirPinM2,HIGH);                                    

     for(i=0;i<=12;i++) {
     
      stepM1Run(stepBM1[i], stepEM1[i], amplitudeM1[i]);
      stepM2Run(stepBM2[i], stepEM2[i], amplitudeM2[i]);
     } 
  }  
}


Thank you. I did. That is how I got as far as I did before reaching out. I guess I still made mistakes at this first attempt with functions. Thank you again for your attention. I hope this discussion helps others too.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.