How to Apply Arrays to Digital

Hi Everyone,

I have been working on getting some motors to work through H-bridges and managed to with this code.

// initialise motors
int enA = 3; // Motor 1
int in1 = 4;
int in2 = 2;

int enB = 5; // Motor 2
int in3 = 8;
int in4 = 7;

int enC = 11; // Motor 3
int in5 = 12;
int in6 = 13;

int enD = 6; // Motor 4
int in7 = 9;
int in8 = 10;

void setup()
{
 
  pinMode(enA, OUTPUT);   // set the outputs for motors
  pinMode(enB, OUTPUT);
  pinMode(enC, OUTPUT);
  pinMode(enD, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(in5, OUTPUT);
  pinMode(in6, OUTPUT);
  pinMode(in7, OUTPUT);
  pinMode(in8, OUTPUT);
  
}


void motorLoop(){
// setting the direction to turn and speed
  digitalWrite(in1, HIGH); // Motor 1
  digitalWrite(in2, LOW);
  
  
  digitalWrite(in3, HIGH); // Motor 2
  digitalWrite(in4, LOW);
  
  
  digitalWrite(in5, HIGH); // Motor 3
  digitalWrite(in6, LOW);
  
  
  digitalWrite(in7, HIGH); // Motor 4
  digitalWrite(in8, LOW);
  
// Set the speed for the Motors
  analogWrite(enA, 1);
  analogWrite(enB, 20);
  analogWrite(enC, 100);
  analogWrite(enD, 200);
};

void loop()
{
  motorLoop();
  delay(500);

}

However I am trying to turn the data into an array and have hit some issues.
I have never tried creating arrays with Digital inputs but have with Analog ones.
Here is a link to my project on (TinkerCAD) https://www.tinkercad.com/things/fFQKRTjhDrb-smashing-allis-kieran/editel?tenant=circuits?sharecode=6rKnUZsFtcOAetd_TufIuN8TfUgi8EupA1TMjlxiacM=

As you can see by this code I have tried to setup the enable inputs which show no errors but I am struggling setting up the OUTPUT and Speed that the motors rotate at.

// initialise motors
// Motor 1
int in1 = 4;
int in2 = 2;

// Motor 2
int in3 = 8;
int in4 = 7;

// Motor 3
int in5 = 12;
int in6 = 13;

// Motor 4
int in7 = 9;
int in8 = 10;

// Array of PWM's
byte pwms[] = {3,5,6,11};
byte numberPwms = 4;

void setup()
{
 
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(in5, OUTPUT);
  pinMode(in6, OUTPUT);
  pinMode(in7, OUTPUT);
  pinMode(in8, OUTPUT);
  
  for(byte i = 0; i <= numberPwms; i++){
  	pinMode(pwms(OUTPUT));
  };
  
}


void motorLoop(){
// setting the direction to turn and speed
  digitalWrite(in1, HIGH); // Motor 1
  digitalWrite(in2, LOW);
  
  
  digitalWrite(in3, HIGH); // Motor 2
  digitalWrite(in4, LOW);
  
  
  digitalWrite(in5, HIGH); // Motor 3
  digitalWrite(in6, LOW);
  
  
  digitalWrite(in7, HIGH); // Motor 4
  digitalWrite(in8, LOW);
  
// Set the speed for the Motors

  for(byte i = 0; i < numberPwms; i++){
  	analogWrite(pwms(200));
  };
  
};

void loop()
{
  motorLoop();
  delay(500);

}

Any help with this would be greatly appreciated :smiley:

I'm not following your description. Do you want to store various motor parameters in an array and to be able to select them at random?

  	pinMode(pwms(OUTPUT));

That's not the syntax for pinMode and it's certainly not the way you read data from an array.

Since you're using i to iterate the for loop you also need to use i to read the array e.g. pwms[ i ].

As you've already used the correct syntax for pinMode 8 times in your program I'll leave you to fix it for this one!

Steve