Paul Philippov

home is where ~/ is

Shorter is better and smaller, too

Below are two variants of the same program for Arduino micro-controller.

First program, the longer one, was found among examples somewhere on the Net.

#define RELAY_ON  0
#define RELAY_OFF 1

#define Relay_1  3
#define Relay_2  2
#define Relay_3  20
#define Relay_4  19
#define Relay_5  18
#define Relay_6  11
#define Relay_7  6
#define Relay_8  5

int waittime;

void setup()
{
  waittime = 1000;
  digitalWrite(Relay_1, RELAY_OFF);
  digitalWrite(Relay_2, RELAY_OFF);
  digitalWrite(Relay_3, RELAY_OFF);
  digitalWrite(Relay_4, RELAY_OFF);
  digitalWrite(Relay_5, RELAY_OFF);
  digitalWrite(Relay_6, RELAY_OFF);
  digitalWrite(Relay_7, RELAY_OFF);
  digitalWrite(Relay_8, RELAY_OFF);

  pinMode(Relay_1, OUTPUT);
  pinMode(Relay_2, OUTPUT);
  pinMode(Relay_3, OUTPUT);
  pinMode(Relay_4, OUTPUT);
  pinMode(Relay_5, OUTPUT);
  pinMode(Relay_6, OUTPUT);
  pinMode(Relay_7, OUTPUT);
  pinMode(Relay_8, OUTPUT);

  delay(4000);
}

void loop()
{
  digitalWrite(Relay_1, RELAY_ON);
  delay(waittime);
  digitalWrite(Relay_2, RELAY_ON);
  delay(waittime);
  digitalWrite(Relay_3, RELAY_ON);
  delay(waittime);
  digitalWrite(Relay_4, RELAY_ON);
  delay(waittime);
  digitalWrite(Relay_5, RELAY_ON);
  delay(waittime);
  digitalWrite(Relay_6, RELAY_ON);
  delay(waittime);
  digitalWrite(Relay_7, RELAY_ON);
  delay(waittime);
  digitalWrite(Relay_8, RELAY_ON);
  delay(waittime);

  digitalWrite(Relay_1, RELAY_OFF);
  delay(waittime);
  digitalWrite(Relay_2, RELAY_OFF);
  delay(waittime);
  digitalWrite(Relay_3, RELAY_OFF);
  delay(waittime);
  digitalWrite(Relay_4, RELAY_OFF);
  delay(waittime);
  digitalWrite(Relay_5, RELAY_OFF);
  delay(waittime);
  digitalWrite(Relay_6, RELAY_OFF);
  delay(waittime);
  digitalWrite(Relay_7, RELAY_OFF);
  delay(waittime);
  digitalWrite(Relay_8, RELAY_OFF);
  delay(waittime);
}

Sketch uses 5,336 bytes (18%) of program storage space. Maximum is 28,672 bytes.
Global variables use 153 bytes (5%) of dynamic memory, leaving 2,407 bytes for local variables. Maximum is 2,560 bytes.

The other, shorter one, is my reworked variant of the first program. In my opinion it is easier to read and is more flexible, happens that I need to make any changes.

#define RELAY_ON  LOW
#define RELAY_OFF HIGH
#define WAIT_TIME 1000

uint8_t pins[] = {3, 2, 20, 19, 18, 11, 6, 5};

void setup() {
  for (uint8_t i = 0; i < sizeof(pins); i++) {
    uint8_t pin = pins[i];
    digitalWrite(pin, RELAY_OFF);
    pinMode(pin, OUTPUT);
  }
  delay(4000);
}

void loop() {
  for (uint8_t i = 0; i < sizeof(pins); i++) {
    uint8_t pin = pins[i];
    digitalWrite(pin, !digitalRead(pin));
    delay(WAIT_TIME);
  }
}

Sketch uses 4,706 bytes (16%) of program storage space. Maximum is 28,672 bytes.
Global variables use 156 bytes (6%) of dynamic memory, leaving 2,404 bytes for local variables. Maximum is 2,560 bytes.

Surprisingly, the shorter variant requires significantly less memory which is very beneficial when writing for micro-controllers.

Published on March 23, 2016 (over 1 year ago)

Article tags: arduino, code, cpp