// MADE BY CAKRAYP :)
//
// Dikutip dari Sumber Dokumentasi Project Tombol.
// Dokumentasi (https://docs.wokwi.com/parts/wokwi-pushbutton)
// Project 1 (https://wokwi.com/projects/397990618860958721)
// Project 2 (https://wokwi.com/projects/397990611031240705)
#include <Servo.h>
#define BtnPin 8 // Pin Button
// Pin LED
#define Led_1 13
#define Led_2 12
int oldValue = LOW;
long PressTime = 0;
// Set UP Variable
int default_Servo = 0; // Servo Default mulai dari 0 Derajat.
int time_degress = 0; // Putaran derajat yang dapat dikalikan.
int degress_Servo = 0; // Servo akan diputar setelah dikalikan saat tombol di tekan (*)
int degress_Servo_main = 0; // Menyimpan Variable Putaran derajat Servo.
// Servo Variable
Servo GoServo;
// Main
void setup() {
Serial.begin(115200);
Serial.println("<Arduino UNO is ready>");
Serial.println("Press the red Button...");
// Initialize Button
pinMode(BtnPin, INPUT);
// LED
pinMode(Led_1, OUTPUT); // RED
pinMode(Led_2, OUTPUT); // Green
digitalWrite(Led_1, HIGH); // LED Merah Utama.
// Servo
GoServo.attach(11); // Memanggil Servo dari Pin 11
GoServo.write(0); // Servo Derajat Awal Putaran (mulai dari 0).
}
// Loop for repeated the programs.
void loop() {
int newValue = digitalRead(BtnPin); // Membaca tombol dari Pin 8
// HIGH = 1
// LOW = 0
if (newValue != oldValue) {
// Watch time to press the button.
PressTime = PressTime + newValue; // Menyimpan data tombol Per-Klik.
// Servo
if (newValue == HIGH) {
// Putaran ketika tombol ditekan (Maksimal 6x Putaran)
time_degress = time_degress + newValue; // Menyimpan data tombol Per-Klik untuk dikalikan derajat.
degress_Servo_main = degress_Servo * time_degress;
if (degress_Servo_main == 0) { // Mulai dari 0 derajat
degress_Servo_main = default_Servo + 30; // (30 Derajat)
degress_Servo = degress_Servo_main;
} else if (degress_Servo_main > 180) { // Maksimal 180 derajat
degress_Servo_main = default_Servo;
degress_Servo = default_Servo;
time_degress = default_Servo;
}
// Servo yang akan diputar dengan 6x Putaran.
GoServo.write(degress_Servo_main);
}
// LED Button Controls
if (newValue == HIGH) {
Serial.print("[BTN]: ");
Serial.println("The button is pressed. (" + String(PressTime) + "x)");
digitalWrite(Led_2, HIGH);
} else {
Serial.print("[BTN]: ");
Serial.println("The button is released.");
digitalWrite(Led_2, LOW);
}
// Remember the value for the next time.
oldValue = newValue;
}
// Slow down the sketch.
// Also for debouncing the button.
delay(100);
}