코드

// Softpot의 아날로그 입력 핀
const int softpotPin = A0;
// 부저의 디지털 출력 핀
const int buzzerPin = 8;

// 음계 주파수 (C4, D4, E4, F4, G4, A4, B4, C5)
const int notes[] = {262, 294, 330, 349, 392, 440, 494, 523};

void setup() {
  // 부저 핀을 출력으로 설정
  pinMode(buzzerPin, OUTPUT);
  // 시리얼 통신 시작 (디버깅용)
  Serial.begin(9600);
}

void loop() {
  // Softpot의 아날로그 값을 읽음 (0 ~ 1023)
  int sensorValue = analogRead(softpotPin);
  // 시리얼 모니터에 값 출력 (디버깅용)
  Serial.println(sensorValue);

  // Softpot의 값을 0 ~ 7 범위로 매핑
  int noteIndex = map(sensorValue, 0, 1023, 0, 7);

  if(sensorValue>10) {
  // 출력이 10초과면 해당 음을 부저로 재생
  tone(buzzerPin, notes[noteIndex]);
  
  // 잠시 대기
  delay(100);
  
  // 부저를 끔
  noTone(buzzerPin);
  }