Easy Relay 4 - C example

#include <gpiod.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
  // open GPIO chip
  const char *chipname = "gpiochip0";
  struct chip = gpiod_chip_open_by_name(chipname);

  // the relays have io numbers 16,17,22,23 when connected via the 2x20 header
  // if you wire manually via the 8-pin header then these numbers can be different
  struct gpiod_line *rel1 = gpiod_chip_get_line(chip, 16);
  struct gpiod_line *rel2 = gpiod_chip_get_line(chip, 17);
  struct gpiod_line *rel3 = gpiod_chip_get_line(chip, 22);
  struct gpiod_line *rel4 = gpiod_chip_get_line(chip, 23);

  // open for output
  gpiod_line_request_output(rel1, "relay-demo", 0);
  gpiod_line_request_output(rel2, "relay-demo", 0);
  gpiod_line_request_output(rel3, "relay-demo", 0);
  gpiod_line_request_output(rel4, "relay-demo", 0);

  // turn each relay on/off for a second, repeating 10 times
  for (i=0; i<10; i++) {
    gpiod_line_set_value(rel1, 1);
    usleep(100000);
    gpiod_line_set_value(rel1, 0);
    gpiod_line_set_value(rel2, 1);
    usleep(100000);
    gpiod_line_set_value(rel2, 0);
    gpiod_line_set_value(rel3, 1);
    usleep(100000);
    gpiod_line_set_value(rel3, 0);
    gpiod_line_set_value(rel4, 1);
    usleep(100000);
    gpiod_line_set_value(rel4, 0);
  }

  // release io's and chip
  gpiod_line_release(rel1);
  gpiod_line_release(rel2);
  gpiod_line_release(rel3);
  gpiod_line_release(rel4);
  gpiod_chip_close(chip);

  return 0;
}