How to run several components automatically

Hey,
i am creating a box (1 arduino with I2C and Power; several with portextender (MCP23017)) to run some Motor. Therefore i must give the MCP different address and also they Need a begin.

mcp1.begin(0); //creates the mcp on 0x20
mcp2.begin(1); //creates the mcp on 0x21 and so on…

later these shall one by one control the motor. (first run m1, m2, m3, m4 of all mcp (which are connected) simultaneously (m1 of mcp1, m1 of mcp2 … shall run together).

is there a way i can write mcp.begin(x-1);
and later on in a for loop:
mcp.digitalWrite(a, HIGH);

You can create an array of MCP23017 objects, and then iterate over that array using a for loop.

Just create them in an array, and loop over the array during setup:

[color=#5e6d03]#include[/color] [color=#005c5f]"Adafruit_MCP23017.h"[/color]

[color=#000000]Adafruit_MCP23017[/color] [color=#000000]mcps[/color][color=#000000][[/color][color=#000000]3[/color][color=#000000]][/color][color=#000000];[/color]
  
[color=#00979c]void[/color] [color=#5e6d03]setup[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color]  
  [b][color=#d35400]size_t[/color][/b] [color=#000000]address[/color] [color=#434f54]=[/color] [color=#000000]0[/color][color=#000000];[/color]
  [color=#5e6d03]for[/color] [color=#000000]([/color][color=#000000]Adafruit_MCP23017[/color] [color=#434f54]&[/color][color=#000000]mcp[/color] [color=#434f54]:[/color] [color=#000000]mcps[/color][color=#000000])[/color]  
    [color=#000000]mcp[/color][color=#434f54].[/color][color=#d35400]begin[/color][color=#000000]([/color][color=#000000]address[/color][color=#434f54]++[/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]}[/color]

Please read the forum rules and use [code][/code] tags to post your code.

Pieter

Do you mean that there will be a master Arduino that must send commands to other Arduinos (the “several with portextender”) or do you mean that there will be a single Arduino to which several port extenders will be attached?

Your

mcp1.begin(0);
mcp2.begin(1);

suggests that there is only a single Arduino

Separate from that I don’t really understand your questions. If you are asking whether you can write to the port-extender pins using digitalWrite() then the answer is NO. You must send a message to the relevant port extender using I2C (I think they also work with SPI). The MCP23017 datasheet describes the messages that it responds to.

…R

I have 1 arduino and several portextenders. Data are sent by I2C. I also created the Array, but later i give a command (for example) mcp[3] but he doenst Reply.

PieterP:
Just create them in an array, and loop over the array during setup:

[color=#5e6d03]#include[/color] [color=#005c5f]"Adafruit_MCP23017.h"[/color]

Adafruit_MCP23017 mcps[3];
 
void setupcolor=#000000[/color] { 
  size_t address = 0;
  for (Adafruit_MCP23017 &mcp : mcps) 
    mcp.begincolor=#000000[/color];
}






Please read the [forum rules](https://forum.arduino.cc/index.php/topic,148850.0.html) and use `[code][/code]` tags to post your code.

Pieter

how do you adress the single mcp? normally they are mcp1, mcp2 etc…
my code is like:

Adafruit_MCP23017 mcp[Anzahl_Porte];      byte MCPs[] = {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26};
void setup() {
for (int p = 0; p < Anzahl_Ports; p++) {    
      Serial.println(p);
      mcp[p].begin(p); 
      for (int o = 0; o < 16; o++) {       
        mcp[p].pinMode(o, OUTPUT);        
        mcp[p].digitalWrite(o, HIGH);     
      }
    }
void MotorControl (int v, int i) {
  Serial.print("************ Motor Control    -->   ");
  Serial.println(v);
 for (int p = 1; p < Anzahl_Ports; p++){
  if (v == 9) {
        mcp[p].digitalWrite(ml[i], LOW);    
    delay(relaist);
  }
  else if (v == 2) {
       mcp[p].digitalWrite(ml[i], LOW);    
    delay(relaist);
  }
  else if (v == 4) {
    mcp[p].digitalWrite(mr[i], LOW);
    //mcp[p].digitalWrite(ml[i], HIGH);
  
  delay(relaist);
  }
  else if (v == 7) {
    mcp[p].digitalWrite(m[i], HIGH);
    delay(auslauft);
  }
  else if (v == 8) {
    mcp[p].digitalWrite(mr[i], HIGH);      
 mcp[p].digitalWrite(ml[i], HIGH);       delay(stopp);  
  }
 }
}

sorry

the if at the end is the plan of the Programm (2 says run, 7 says left, 8 right)... didnt want to post the complete Code because it is way too long

my code is like:

That code looks like crap. The Arduino team provides a menu item, Auto Format, on the Tools menu so you don't have to look amateurish posting misaligned code like that.

didnt want to post the complete Code because it is way too long

It should NOT be, when you are trying to learn/accomplish something new.

I copied the parts with the mcp, thats why it Looks like crap.

#include <Adafruit_MCP23017.h>  //lib einbinden
#include <Wire.h>             //lib einbinden


//PINS
//Arrays damit man i hochzählen kann und die unterschiedlichen Pins angesprochen werden
int m[4] = {12, 13, 3, 2};      //Motor An/Aus Pins (Motor1, Motor2,...)
int mr[4] = {9, 11, 7, 5};      //rechtes Motorrelais PINS
int ml[4] = {8, 10, 6, 4};      //linkes Motorrelais PINS
//PROGRAMMABLAUF
int v[8] = {2, 9, 7, 8, 4, 9, 7, 8};


long q = 0;
int Anzahl_Ports = 1;
//int Anzahl_P = 3;
const byte Anzahl_Porte = 7;


//Adafruit_MCP23017 mcp1;
Adafruit_MCP23017 mcp[Anzahl_Porte];      //mcp werden dynamisch gesetzt
byte MCPs[] = {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26};   //size_t gibt bit zurück --> adressen im Setup

int start = 1;
int select1 = 1;
int select2 = 1;
int select3 = 1;
int select4 = 1;
int select5 = 1;

int stopp = 2500;
int stopt = 2500;

int motor = 2500;
int motort = 2500;

int lauf = 2000;
int lauft = 2000;

long Anzahl_LW = 1000;
long Anzahl_LWf = 1000;

int relaist = 50;
int auslauft = 50;

void setup() {
  // put your setup code here, to run once:
  //mcp1.begin(0);
  Serial.begin(115200);
  Serial.println(" ---------------  Ansteuerung  ------------- ");

  for (int p = 0; p < Anzahl_Ports; p++) {    //erstellt MCP-Adressen (Anzahl wird zu beginn eingestellt)
    Serial.println(p);
    mcp[p].begin(p); //werden mit 0,1, 2 , ... adressiert
    for (int o = 0; o < 16; o++) {        //zählt bis 16 --> Pins definieren
      mcp[p].pinMode(o, OUTPUT);        //Alle Pins als Output
      mcp[p].digitalWrite(o, HIGH);     //Alle Pins auf HIGH --> Stabilisierung? macht überhaupt Sinn alle HIGH? ansonsten würden direkt alle Relais einmal Schalten weil die NO sind
    }
  }
void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(Anzahl_LWf);
  Serial.println(motor);
  Serial.println(lauf);
  Serial.print("Stop   :");
  Serial.println(stopp);
  Serial.println(Anzahl_Ports);
  motorprog(Anzahl_LWf, motor, lauf, stopp, Anzahl_Ports);
}
void motorprog(long Anzahl_LWf, int motor, int lauf, int stopp, int Anzahl_Ports) {
  Serial.println("------------------- Motor Prog -----------------------");
  // Array ist nach Ablauf aufgebaut (eine Zahl hochzählen = nächster Schritt). Dadurch kann man das nun in 2 Zeilen anstelle 20
  //a < 10 (man benötigt 10 Schritte (mit 2 mal Reset) für den Ablauf
  //delay zur Stabilisierung, Zeiten für jeden Schritt werden in der Definition der Variablen deklariert /eingestellt, Später im Menü
  for ( q = 0; q < Anzahl_LWf; q++) {        //solange bis Anzahl-lastwechsel erreicht ist
    //    Serial.println(q);
    for (int i = 0; i < 4; i++) {            //weil 4 Motoren
      //      Serial.println(i);
      for (int a = 0; a < 8; a++) {          //Ablauf steht im Array, braucht 8, damit durchgeht
        //        Serial.println(a);
        MotorControl(v[a], i);
        delay(motor);    //Zeit bevor nächster Motor angesprochen wird
      }
    }
  }
}
void MotorControl (int v, int i) {
  Serial.print("************ Motor Control    -->   ");
  Serial.println(v);
  for (int p = 1; p < Anzahl_Ports; p++) {
    if (v == 9) {
      //mcp[p].digitalWrite(mr[i], HIGH); // ist eigentlich unnötig, da alle HIGH sind
      mcp[p].digitalWrite(ml[i], LOW);    //linkes Relais auf Low --> S
      delay(relaist);
    }
    else if (v == 2) {
      //mcp[p].digitalWrite(mr[i], HIGH); // ist eigentlich unnötig, da alle HIGH sind
      mcp[p].digitalWrite(ml[i], LOW);    //linkes Relais auf Low --> S
      delay(relaist);
    }
    else if (v == 4) {
      mcp[p].digitalWrite(mr[i], LOW);
      //mcp[p].digitalWrite(ml[i], HIGH);

      delay(relaist);
    }
    else if (v == 7) {
      mcp[p].digitalWrite(m[i], HIGH);
      delay(auslauft);
    }
    else if (v == 8) {
      mcp[p].digitalWrite(mr[i], HIGH);   //eventuell auf LOW, damit die MOSFETS nicht durchgehen
      mcp[p].digitalWrite(ml[i], HIGH);   //sinnvoller auf HIGH oder LOW?
      delay(stopp); //Zeit nach Kurzschluss, bis Richtungswechsel ( ACHTUNG: MOTORWECHSELZEIT =STOPP + MOTOR)
    }
  }
}

Thats the Code. I Need to find out, how to make run 4 mcp the Funktion MotorControl simultaneously.

What's the difference between Anzahl_Ports and Anzahl_Porte?

There is no difference. All These variables shall later be set in the menue. I just gave them now a value to test this. Also the other Variables are like this (doubled) because of stability with the display/menueprogramm together.

Dimba:
There is no difference. All These variables shall later be set in the menue. I just gave them now a value to test this. Also the other Variables are like this (doubled) because of stability with the display/menueprogramm together.

That doesn't make any sense. You create an array of 7 MCPs, and then only initialize one of them?

The Code runs until the if conditions at the end. Also the values ( for example 7) of the if condition (if v ==7) is printed in Serial, only the mcp dos nothing.

Dimba:
There is no difference.

You may not want a difference to exist but as far as the compiler is concerned Anzahl_Ports and Anzahl_Porte are two entirely different things.

You have one of them defined as 7 and the other as 1 and it seems to be only the latter that is used in your program after line 21. That means that your FOR loops are only iterating once and not 7 times.

...R