Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
6.9 kB
5
Indexable
#include <Arduino.h>
#include <TM1637Display.h>

#define R1 2 // DO
#define R2 3 // RE
#define R3 4 // MI
#define R4 5 // FA
#define R5 6 // SO
#define R6 7 // LA
#define CLK A4
#define DIO A5

TM1637Display display(CLK, DIO);

int buzz = 13;
int BSTART = 8;
int switchUpPin = 11;
int switchDownPin = 12;
int delayUpPin = 9; // Tombol Jeda Naik
int delayDownPin = 10; // Tombol Jeda Turun
int counter = 0;
volatile int delayTime = 0; // Waktu jeda awal dalam milidetik (volatile karena digunakan dalam ISR)
int buttonUpState = 0;
int lastButtonUpState = 0;
int buttonDownState = 0;
int lastButtonDownState = 0;
int delayUpState = 0;
int lastDelayUpState = 0;
int delayDownState = 0;
int lastDelayDownState = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(R1, OUTPUT);
  pinMode(R2, OUTPUT);
  pinMode(R3, OUTPUT);
  pinMode(R4, OUTPUT);
  pinMode(R5, OUTPUT);
  pinMode(R6, OUTPUT);
  pinMode(BSTART, INPUT_PULLUP);
  pinMode(switchUpPin, INPUT_PULLUP);
  pinMode(switchDownPin, INPUT_PULLUP);
  pinMode(delayUpPin, INPUT_PULLUP);
  pinMode(delayDownPin, INPUT_PULLUP);
  pinMode(buzz, OUTPUT);

  display.setBrightness(0x0f);

  // Inisialisasi Timer1 untuk interrupt setiap 100ms
  cli(); // Nonaktifkan interrupt saat konfigurasi
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1 = 0;
  OCR1A = 1000; // Interrupt setiap 100ms (16 MHz clock dengan prescaler 1024)
  TCCR1B |= (1 << WGM12); // CTC mode
  TCCR1B |= (1 << CS12) | (1 << CS10); // Prescaler 1024
  TIMSK1 |= (1 << OCIE1A); // Enable timer compare interrupt
  sei(); // Aktifkan kembali interrupt
}

void loop() 
{
  if (digitalRead(BSTART) == LOW) {
    // Putar lagu sesuai dengan nilai counter
    int val1 = counter;
    switch (val1) 
    {
      case 1: // LAGU 1
T1(); delay(100+delayTime); S0(); delay(129+delayTime);
T1(); delay(100+delayTime); S0(); delay(129+delayTime);
T1(); delay(200+delayTime); S0(); delay(54+delayTime);
T2(); delay(100+delayTime); S0(); delay(129+delayTime);

T1();T3(); delay(60+delayTime); S0(); delay(54+delayTime);
T3(); delay(60+delayTime); S0(); delay(54+delayTime);
T5();T3(); delay(60+delayTime); S0(); delay(54+delayTime);
T5(); delay(60+delayTime); S0(); delay(54+delayTime);
T1();T3(); delay(60+delayTime); S1(); delay(54+delayTime);
T3(); delay(100+delayTime); S0(); delay(54+delayTime);
T2(); delay(200+delayTime); S0(); delay(54+delayTime);
T1();T3(); delay(60+delayTime); S1(); delay(54+delayTime);
T3(); delay(100+delayTime); S0(); delay(54+delayTime);
T4();T5(); delay(60+delayTime); S5(); delay(54+delayTime);
T5(); delay(100+delayTime); S0(); delay(54+delayTime);
T1();T3(); delay(60+delayTime); S1(); delay(54+delayTime);
T3(); delay(100+delayTime); S0(); delay(54+delayTime);
T1(); delay(200+delayTime); S0(); delay(54+delayTime);



        break;
        
      case 2: // LAGU 2
        T1(); delay(150 + delayTime); S0(); delay(150 + delayTime);
        T2(); delay(150 + delayTime); S0(); delay(150 + delayTime);
        T3(); delay(150 + delayTime); S0(); delay(150 + delayTime);
        T4(); delay(150 + delayTime); S0(); delay(150 + delayTime);
        break;
        
      // Tambahkan lagu lain di sini
    }
  } 
  else 
  {
    // Jika tombol BSTART tidak ditekan, tampilkan nilai counter
    changeNumber(counter);
  }

  // Tangani tombol naik
  buttonUpState = digitalRead(switchUpPin);
  if (buttonUpState != lastButtonUpState) 
  {
    if (buttonUpState == HIGH) 
    {
      if(counter == 21)
      {
        counter = -1;
      }
      counter++;
      Serial.println(counter);
      delay(300);
    }
    else
    {
      Serial.println("OFF");
    }
    delay(50);
  }
  lastButtonUpState = buttonUpState;

  // Tangani tombol turun
  buttonDownState = digitalRead(switchDownPin);
  if (buttonDownState != lastButtonDownState) 
  {
    if (buttonDownState == HIGH) 
    {
      if(counter == 0)
      {
        counter = 22;
      }
      counter--;
      Serial.println(counter);
      delay(300);
    }
    else
    {
      Serial.println("OFF");
    }
    delay(50);
  }
  lastButtonDownState = buttonDownState;
}

void changeNumber(int buttonPress)
{
  switch (buttonPress)
  {
    case 0:
      display.showNumberDec(0, true);
      delay(100);
      break;
    case 1:
      display.showNumberDec(1, false);
      delay(100);
      break;
    case 2:
      display.showNumberDec(2, false);
      delay(100);
      break;
    // Tambahkan case lain jika diperlukan
  }
}

void showLetter(uint8_t letter)
{
  // Tampilkan huruf pada tampilan 7-segment
  display.setSegments(&letter, 1);
}

void T1()
{
  if(!digitalRead(R1)){
    tone(buzz, 523);
  }
  else{
    noTone(buzz);
  }
  digitalWrite(R1, HIGH);
}

void T2()
{
  if(!digitalRead(R2)){
    tone(buzz, 587);
  }
  else{
    noTone(buzz);
  }
  digitalWrite(R2, HIGH);
}

void T3()
{
  if(!digitalRead(R3)){
    tone(buzz, 659);
  }
  else{
    noTone(buzz);
  }
  digitalWrite(R3, HIGH);
}

void T4()
{
  if(!digitalRead(R4)){
    tone(buzz, 698);
  }
  else{
    noTone(buzz);
  }
  digitalWrite(R4, HIGH);
}

void T5()
{
  if(!digitalRead(R5)){
    tone(buzz, 784);
  }
  else{
    noTone(buzz);
  }
  digitalWrite(R5, HIGH);
}

void T6()
{
  if(!digitalRead(R6)){
    tone(buzz, 880);
  }
  else{
    noTone(buzz);
  }
  digitalWrite(R6, HIGH);
}

void S0()
{
  digitalWrite(R1, LOW);
  digitalWrite(R2, LOW);
  digitalWrite(R3, LOW);
  digitalWrite(R4, LOW);
  digitalWrite(R5, LOW);
  digitalWrite(R6, LOW);
  noTone(buzz);
}
void S1()
{
  digitalWrite(R1, LOW);
  noTone(buzz);
}
void S2()
{
  digitalWrite(R2, LOW);
  noTone(buzz);
}
void S3()
{
  digitalWrite(R3, LOW);
  noTone(buzz);
}
void S4()
{
  digitalWrite(R4, LOW);
  noTone(buzz);
}
void S5()
{
  digitalWrite(R5, LOW);
  noTone(buzz);
}
void S6()
{
  digitalWrite(R6, LOW);
  noTone(buzz);
}

// Tambahkan fungsi S1, S2, dll. jika diperlukan

ISR(TIMER1_COMPA_vect) {
  // Tangani tombol jeda naik
  delayUpState = digitalRead(delayUpPin);
  if (delayUpState != lastDelayUpState) 
  {
    if (delayUpState == HIGH) 
    {
      delayTime += 5; // Tambah 5 ms pada jeda
      Serial.print("Jeda Naik: ");
      Serial.println(delayTime);
    }
  }
  lastDelayUpState = delayUpState;

  // Tangani tombol jeda turun
  delayDownState = digitalRead(delayDownPin);
  if (delayDownState != lastDelayDownState) 
  {
    if (delayDownState == HIGH) 
    {
      if (delayTime > -20) 
      {
        delayTime -= 5; // Kurangi 5 ms pada jeda
        Serial.print("Jeda Turun: ");
        Serial.println(delayTime);
      }
    }
  }
  lastDelayDownState = delayDownState;
}
Leave a Comment