/*
   4 Seven segment display with Pi Pico
   by Anderson Costa
   Wokwi Community
*/

// Digit 1-4
uint8_t digitPins[] = { 11, 2, 3, 5 };

// A B C D E F G DP
uint8_t segmentPins[] = { 12, 4, 7, 9, 10, 13, 6, 8 };

/*
       A
      ---
  F |  G  | B
      ---
  E |     | C
      ---
       D
*/
uint8_t alpha[4][8] = {
  { 1, 1, 0, 0, 1, 1, 1, 0 }, // P
  { 0, 0, 0, 0, 1, 1, 0, 0 }, // I
  { 1, 0, 0, 1, 1, 1, 0, 0 }, // C
  { 1, 1, 1, 1, 1, 1, 0, 0 }  // O
};

int main() {
  // Define the digit pins as output
  for (uint8_t i = 0; i < 4; i++) {
    _gpio_init(digitPins[i]);
    gpio_set_dir(digitPins[i], GPIO_OUT);
  }

  // Define the segment pins as output
  for (uint8_t i = 0; i < 8; i++) {
    _gpio_init(segmentPins[i]);
    gpio_set_dir(segmentPins[i], GPIO_OUT);
  }

  while (true) {
    // Scroll the digits
    for (uint8_t dig = 0; dig < 4; dig++)
    {
      // Enable digit
      gpio_put(digitPins[dig], 1);

      // Scroll the segments
      for (uint8_t seg = 0; seg < 8; seg++) {
        // Invert (common anode)
        if (alpha[dig][seg] == 0) {
          gpio_put(segmentPins[seg], 1);
        } else {
          gpio_put(segmentPins[seg], 0);
        }
      }

      sleep_ms(200);
      // Disable digit
      gpio_put(digitPins[dig], 0);
      sleep_ms(100);
    }

    // Set the digits to LOW (OFF)
    for (uint8_t i = 0; i < 4; i++) {
      gpio_put(digitPins[i], 0);
    }

    //Set the segments to LOW (OFF)
    for (uint8_t i = 0; i < 8; i++) {
      gpio_put(segmentPins[i], 0);
    }
  }

  return 0;
}
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT