const int ledRed = 10;
const int ledGreen = 8;
const int ledYellow = 9;
const int buttonPin = 4;
const int buttonPin2 = 2;
const int buttonPin3 = 7;
int buttonState = 0 ;
int buttonState2 = 0;
int buttonState3 = 0;
// Command inter face
class Command {
public:
virtual void execute() = 0;
virtual void undo() = 0;
virtual const char* getName() const = 0;
};
// Receiver class (objek yang menerima perintah)
class Light {
public:
void Defaultled() {
digitalWrite(ledGreen, HIGH);
};
void Blinkon() {
for (int i = 0; i < 15; i++) {
digitalWrite(ledRed, HIGH);
delay(500);
digitalWrite(ledRed, LOW);
delay(500);
if(buttonState3 == HIGH ){
break;
}
}
};
void Fadeon() {
// if (buttonState2 == LOW) {
for (int ledVal = 0; ledVal <= 255; ledVal += 1) {
analogWrite(ledYellow, ledVal);
delay(15);
}
for (int ledVal = 255; ledVal >= 0; ledVal -= 1) {
analogWrite(ledYellow, ledVal);
delay(15);
}
// }
}
};
// Concrete Command untuk menyalakan LED
class Defaultcom : public Command {
private:
Light *defaultleds;
public:
Defaultcom(Light *defaultled) : defaultleds(defaultled) {}
void execute() override {
defaultleds->Defaultled();
}
void undo() override {
}
const char* getName() const override {
return "Defaultcom";
}
};
class Blinkcom : public Command {
private:
Light *blinkleds;
public:
Blinkcom(Light *blinkled) : blinkleds(blinkled) {}
void execute() override {
blinkleds->Blinkon();
}
void undo() override {
blinkleds->Defaultled();
}
const char* getName() const override {
return "Blinkcom";
}
};
class Fadecom : public Command {
private:
Light *fadeleds;
public:
Fadecom(Light *fadeled) : fadeleds(fadeled) {}
void execute() override {
fadeleds->Fadeon();
}
void undo() override {
fadeleds->Defaultled();
}
const char* getName() const override {
return "Fadecom";
}
};
// History class untuk menyimpan riwayat perintah
class History {
private:
static const int MAX_COMMANDS = 10;
Command *commands[MAX_COMMANDS];
int currentIndex;
public:
History() : currentIndex(-1) {}
void addCommand(Command *cmd) {
if (currentIndex < MAX_COMMANDS - 1) {
currentIndex++;
commands[currentIndex] = cmd;
cmd->execute();
printHistory();
} else {
Serial.println("Riwayat perintah penuh");
}
}
void undo() {
if (currentIndex >= 0) {
commands[currentIndex]->undo();
delete commands[currentIndex];
currentIndex--;
printHistory();
} else {
Serial.println("Tidak ada perintah untuk dibatalkan");
}
}
void printHistory() {
Serial.println("Riwayat Perintah:");
for (int i = 0; i <= currentIndex; i++) {
Serial.print(i + 1);
Serial.print(". ");
Serial.println(commands[i] == nullptr ? "Null Command" : commands[i]->getName());
}
Serial.println("----------");
}
};
// Setup
Light led;
Defaultcom *turnOnDefault = new Defaultcom(&led);
Blinkcom *turnOnBlinkled = new Blinkcom(&led);
Fadecom *turnOnFadeled = new Fadecom(&led);
History history;
void setup() {
Serial.begin(1152000);
pinMode(ledRed, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(ledYellow, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
// Set tombol untuk menyalakan LED
history.addCommand(turnOnDefault);
}
void loop() {
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
// Serial.println(buttonState);
if(buttonState == HIGH){
digitalWrite(ledGreen, LOW);
history.addCommand(turnOnBlinkled);
} else if(buttonState2 == HIGH){
digitalWrite(ledGreen, LOW);
history.addCommand(turnOnFadeled);
}
if(buttonState3 == HIGH){
history.undo();
}
delay(100);
}