One function that affects mulitple I/Os?

Hello,

I'm fairly new to Arduino, and I'm trying to create a function that does something like the following:

ScenarioA() {
    digitalWrite(Pin2, HIGH);
    digitalWrite(Pin3, LOW);
    digitalWrite(Pin4, LOW);
    digitalWrite(Pin5, HIGH);
    digitalWrite(Pin6, LOW);
    digitalWrite(Pin7, HIGH);
    digitalWrite(Pin8, HIGH);
    digitalWrite(Pin9, HIGH);
    digitalWrite(Pin10, HIGH);
}

That first function would be followed by dozens of other functions that would desciribe a different set of HIGH/LOW states for those pins.

For example:

ScenarioB() {
    digitalWrite(Pin2, LOW);
    digitalWrite(Pin3, LOW);
    digitalWrite(Pin4, HIGH);
    digitalWrite(Pin5, HIGH);
    digitalWrite(Pin6, LOW);
    digitalWrite(Pin7, HIGH);
    digitalWrite(Pin8, HIGH);
    digitalWrite(Pin9, HIGH);
    digitalWrite(Pin10, LOW);
}

Secondly, if I was able to create such a function, how would I call it later on?

Thanks for any help!

ScenarioA(); // calls the ScenarioA function

Functions have a have a type, example: void, int, byte . . .

Looks like you need to study how to use arrays. http://www.thebox.myzen.co.uk/Tutorial/Arrays.html .

Foundations: https://www.arduino.cc/en/Tutorial/Foundations

Thanks for your response. I had a look at the references you gave me and was able to create the following:

void loop() {

  boolean scenarioA [7] = { HIGH, HIGH, LOW, HIGH, HIGH, LOW, HIGH };
  boolean scenarioB [7] = { LOW, LOW, LOW, LOW, LOW, LOW, LOW, };
  
  int pins [7] = { 2, 3, 4, 5, 7, 11, 12 };

  for (int i = 0; i < 7; i++) {
    digitalWrite(pins[i], scenarioA[i]);
    Serial.print("scenarioA's pin number ");
    Serial.print(pins[i]);
    Serial.print(" outputs a ");
    Serial.println(scenarioA[i]);

  }
  Serial.println("END A");
  delay(1000);

  for (int i = 0; i < 7; i++) {
    digitalWrite(pins[i], scenarioA[i]);
    Serial.print("scenarioB's pin number ");
    Serial.print(pins[i]);
    Serial.print(" outputs a ");
    Serial.println(scenarioB[i]);

  }

  Serial.println("END B");
  delay(2000);
}

Looking better.

boolean scenarioA [7] = { HIGH, HIGH, LOW, HIGH, HIGH, LOW, HIGH }; boolean scenarioB [7] = { LOW, LOW, LOW, LOW, LOW, LOW, LOW, };

int pins [7] = { 2, 3, 4, 5, 7, 11, 12 };

You can move these above setup()

You can still put the two sections of code into functions if you need.

Great, that helps clean things up. This is what I’ve got now:

// The below is a quicker way to say:   const int pin13 = 13;   const int pin12 = 12;   ...   const int pinA3 = A3
const int pin [15] = { 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, A0, A1, A2, A3 };

// Assigns a series of HIGH or LOW to an array from 0 to 14
boolean v_ALPHA [15] = { LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW };
boolean v_BRAVO [15] = { HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH };
boolean v_CHARLIE [15] = { HIGH, HIGH, HIGH, LOW, LOW, LOW, LOW, HIGH, HIGH, HIGH, LOW, LOW, LOW, LOW, LOW };
boolean v_DELTA [15] = { LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW };
boolean v_ECHO [15] = { HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH };

// Assigns a series of pin names to an array from 0 to 14
int pins [15] = { 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, A0, A1, A2, A3 };


void setup() {

// Begin serial output incase you want to write to the Serial Monitor
  Serial.begin(9600);
 
// The below is a quicker way to say:   pinMode(pin13, OUTPUT);   pinMode(pin12, OUTPUT);   etc.
for(int i=0; i<15; i++) pinMode(pin[i], OUTPUT);


}

void loop() {

  for (int i = 0; i < 15; i++)
  {
    digitalWrite(pins[i], v_ALPHA[i]);
    /*
        Serial.print("v_ALPHA's pin number ");
        Serial.print(pins[i]);
        Serial.print(" outputs a ");
        Serial.println(v_ALPHA[i]);
    */
  }
  delay(500);

  for (int i = 0; i < 15; i++)  {
    digitalWrite(pins[i], v_BRAVO[i]);
  }
  delay(500);

  for (int i = 0; i < 15; i++)  {
    digitalWrite(pins[i], v_CHARLIE[i]);
  }
  delay(500);

  for (int i = 0; i < 15; i++)  {
    digitalWrite(pins[i], v_DELTA[i]);
  }
  delay(1000);

  for (int i = 0; i < 15; i++)  {
    digitalWrite(pins[i], v_ECHO[i]);
  }
  delay(1000);

}

Thanks so much for your help!!

give this variation a try

  • changed some datatypes
  • created a function with all repetition in it.
const byte pin[15] = { 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, A0, A1, A2, A3 };

byte v_ALPHA [15] = { LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW };
byte v_BRAVO [15] = { HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH };
byte v_CHARLIE [15] = { HIGH, HIGH, HIGH, LOW, LOW, LOW, LOW, HIGH, HIGH, HIGH, LOW, LOW, LOW, LOW, LOW };
byte v_DELTA [15] = { LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW };
byte v_ECHO [15] = { HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH };

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

  for (byte i = 0; i < 15; i++) pinMode(pin[i], OUTPUT);
}

void loop()
{
  setpins(v_ALPHA, 500);
  setpins(v_BRAVO, 500);
  setpins(v_CHARLIE, 500);
  setpins(v_DELTA, 1000);
  setpins(v_ECHO, 1000);
}


void setpins(byte * ar, int del)
{
  for (byte i = 0; i < 15; i++)
  {
    digitalWrite(pin[i], ar[i]);
  }
  delay(del);
}

A memory optimization would be coding the HIGH LOW in a binary number

byte v_ALPHA [15] = { LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, }

would become

int ALPHA = 0b0000000000000000; // 16 bits all zero == LOW 2 bytes instead of 14 per character.

yes the function to set the pins would need to be changed with bit math