#define POT A0
#define BTN1 11
#define BTN2 12
#define BTN3 13
#define BUZ1 9
#define BUZ2 10
#define LED A1
// Define pins for seven-segment display
const byte sevseg[] = {7, 8, 2, 3, 4, 6, 5};
bool btn1Pressed = false;
byte angka = 0;
bool bunyi = false;
void setup() {
Serial.begin(9600);
// Clear the serial monitor
Serial.flush();
// Move the cursor to the specified position
Serial.println("AFIS"); // AFIS on the first line
Serial.println("21650096"); // 21650096 on the second line
pinMode(BTN1, INPUT_PULLUP);
pinMode(BTN2, INPUT_PULLUP);
pinMode(BTN3, INPUT_PULLUP);
pinMode(LED, OUTPUT);
pinMode(BUZ1, OUTPUT);
pinMode(BUZ2, OUTPUT);
// Set up pins for seven-segment display
for (byte i : sevseg) {
pinMode(i, OUTPUT);
digitalWrite(i, HIGH);
}
attachInterrupt(digitalPinToInterrupt(BTN3), tombolInterrupt, RISING);
}
void loop() {
bool btn1 = digitalRead(BTN1);
bool btn2 = digitalRead(BTN2);
bool btn3 = digitalRead(BTN3);
// Check if BTN1 is pressed
if (btn1 == LOW) {
delay(250);
int notes[] = {262, 262, 294, 262, 349, 330, 262, 262, 294, 262, 392, 349, 262, 262, 523, 440, 349, 330, 294, 466, 466, 440, 349, 392, 349, 262, 262, 294, 262, 349, 330, 262, 262, 294, 262, 392, 349, 330};
int duration[] = {250, 250, 500, 500, 500, 500, 250, 250, 500, 500, 500, 500, 250, 250, 500, 500, 500, 500, 500, 500, 250, 250, 500, 500, 500, 500, 250, 250, 500, 500, 500, 500, 250, 250, 500, 500, 500, 500, 250, 250, 500};
for (int i = 0; i < sizeof(notes) / sizeof(notes[0]); i++) {
tone(BUZ2, notes[i], duration[i]);
delay(duration[i] + 50); // add a small delay between notes
tampilAngka(angka);
angka = (angka + 1) % 10;
}
noTone(BUZ2); // Stop the buzzer
matikan();
bunyi = false;
}
// Check if BTN2 is pressed
if (btn2 == LOW) {
delay(250);
// Blink the LED
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(500);
bunyi = false;
}
// Check if BTN3 is pressed
if (btn3 == LOW) {
delay(250);
bunyi = !bunyi;
digitalWrite(LED, HIGH);
delay(300);
digitalWrite(LED, LOW);
}
if (bunyi) {
int mq = analogRead(POT);
mq = map(mq, 0, 1023, 0, 100);
Serial.print("Intensitas Gas: ");
Serial.print(mq);
Serial.println("%");
if (mq < 50) return;
tone(BUZ1, 200);
delay(100);
noTone(BUZ1);
delay(300);
}
delay(100);
}
void tombolInterrupt() {
bunyi = false;
}
void tampilAngka(byte num) {
switch (num) {
case 1: nyalakan("bc"); break;
case 2: nyalakan("abdeg"); break;
case 3: nyalakan("abcdg"); break;
case 4: nyalakan("bcfg"); break;
case 5: nyalakan("acdfg"); break;
case 6: nyalakan("acdefg"); break;
case 7: nyalakan("abc"); break;
case 8: nyalakan("abcdefg"); break;
case 9: nyalakan("abcdfg"); break;
default: nyalakan("abcdef"); break;
}
}
void nyalakan(String l) {
matikan();
for (char c : l)
digitalWrite(sevseg[convertToNumber(c)], LOW);
}
void matikan() {
for (byte e : sevseg)
digitalWrite(e, HIGH);
}
int convertToNumber(char input) {
switch (input) {
case 'a': return 0;
case 'b': return 1;
case 'c': return 2;
case 'd': return 3;
case 'e': return 4;
case 'f': return 5;
case 'g': return 6;
}
}