// Basic oscilloscope functionalities

uint16_t samples[512];

uint32_t prevMillis;
uint32_t sincValue = 512;

uint16_t pot = 0;
uint16_t pwm = 0;
uint16_t audio = 0;

void setup() {
  // Define input and output pins
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(3, OUTPUT);

  delay(10);

  // Set PWM with 50% duty cycle
  analogWrite(3, 127);

  // Serial initialization with baud rate of 115200
  Serial.begin(115200);

  delay(10);
}

void loop() {
  // 1-second timing
  if (((millis() - prevMillis) > 1000)) {
    // Reset
    prevMillis = millis();

    int8_t prevState = -1;

    for (uint16_t k = 0; k < 512; k++) {
      int8_t state = (analogRead(3) > sincValue);
      if ((prevState == 0) && (state == 1)) break;
      prevState = state;
    }

    for (uint16_t i = 0; i < 512; i++) {
      samples[i] = analogRead(3) + 512;
    }

    for (uint16_t j = 0; j < 512; j++) {
      // PWM samples
      Serial.println(String(samples[j]));
    }
  }

  // Microphone audio samples
  audio = analogRead(A1) + 1200;
  Serial.print("0 " + String(audio) + ",");

  // Potentiometer samples
  pot = analogRead(A0) + 2400;
  Serial.println(String(pot) + " 4200");
}