/*
  Wokwi | questions
  not sure what the correct way to use the void

  cold frog — 11/15/24 at 9:34 PM
  how do i change elements in a table
  honestly that is just an easy google i dont know why i am asking
  ok nevermind im getting no asnwers
*/

const int NUM_BTNS = 4;   // how many buttons
const int NUM_BASES = 5;  // how many bases in "strand" sequence
const char BASES[4] = {'A', 'C', 'G', 'T'};
// initialize pins
const int BTN_PINS[NUM_BTNS] = {9, 8, 7, 6};
const int LED_PINS[NUM_BTNS] = {5, 4, 3, 2};

// initialize global variables
int btnState[NUM_BTNS];
int oldBtnState[NUM_BTNS];
char strand[NUM_BASES];

// function returns which button was pressed, or 0 if none
int checkButtons()  {
  int btnPressed = 0;

  for (int i = 0; i < NUM_BTNS; i++) {
    // check each button
    btnState[i] = digitalRead(BTN_PINS[i]);
    if (btnState[i] != oldBtnState[i])  { // if it changed
      oldBtnState[i] = btnState[i];       // remember state for next time
      if (btnState[i] == LOW)  {          // was just pressed
        btnPressed = i + 1;
        Serial.print("Button ");
        Serial.print(btnPressed);
        Serial.print(" pressed, ");  // (or add linefeed)
        digitalWrite(LED_PINS[i], HIGH);
      } else {                            // was just released
        digitalWrite(LED_PINS[i], LOW);
      }
      delay(20);  // debounce
    }
  }
  return btnPressed;
}

// prints the current value of strand
void printStrand(int digits) {
  Serial.print("base ");
  Serial.print(digits + 1);
  Serial.print(" of ");
  Serial.print(NUM_BASES);
  Serial.print("\tStrand = ");
  for (int i = 0; i <= digits; i++) {
    Serial.print(strand[i]);
  }
  Serial.println();
}

// runs once, on boot
void setup() {
  Serial.begin(115200);
  for (int i = 0; i < NUM_BTNS; i++) {
    pinMode(LED_PINS[i], OUTPUT);
    pinMode(BTN_PINS[i], INPUT_PULLUP);
  }
  Serial.println("DNA Sequencer V1.00\n");
}

// runs forever after setup
void loop() {
  static int index = 0;

  int current_button = checkButtons();
  if (current_button != 0)  {       // if a button was pressed
    strand[index] = BASES[current_button - 1]; // add base to next element
    printStrand(index);             // show result
    index++;                        // move to next element
    if (index >= NUM_BASES) {       // clear once max digits reached
      index = 0;
      for (int i = 0; i < NUM_BASES; i++) {
        strand[i] = 0;
      }
      Serial.println("\nReady for new sequence");
    }
    delay(10);
  }
}
$abcdeabcde151015202530354045505560fghijfghij
A
C
G
T