//NAMA: Joy Disanto Nupa
//NIM: 2209106014
const int BTN_NAIK = 7;
const int BTN_TURUN = 8;
const int POT = 3;
const int MOTOR = 2;
const int PWM_FREQ = 5000;
const int PWM_RES = 8;
int spd = 0;
int spdMax = 255;
int tNaik = 0;
int tTurun = 0;
int tNow = 0;
const int debounce = 200;
void setup() {
Serial.begin(115200);
pinMode(BTN_NAIK, INPUT_PULLUP);
pinMode(BTN_TURUN, INPUT_PULLUP);
pinMode(POT, INPUT);
ledcAttach(MOTOR, PWM_FREQ, PWM_RES);
}
void loop() {
int potVal = analogRead(POT);
spdMax = map(potVal, 0, 4095, 0, 255);
tNow = millis();
if (digitalRead(BTN_NAIK) == LOW && (tNow - tNaik > debounce)) {
spd += 5;
if (spd > spdMax) spd = spdMax;
tNaik = tNow;
}
if (digitalRead(BTN_TURUN) == LOW && (tNow - tTurun > debounce)) {
spd -= 5;
if (spd < 0) spd = 0;
tTurun = tNow;
}
ledcWrite(MOTOR, spd);
int rpm = map(spd, 0, 255, 0, 1000);
Serial.print("MAX: ");
Serial.print(spdMax);
Serial.print(" | SPD: ");
Serial.print(spd);
Serial.print(" | RPM: ");
Serial.println(rpm);
delay(100);
}