// กำหนดขาของ LED 6 ดวง
int ledPins[] = {2, 3, 4, 5, 6, 7};
class Led {
private:
int pin;
public:
Led(int p) : pin(p) {
pinMode(pin, OUTPUT);
}
void stateOn() {
digitalWrite(pin, HIGH);
}
void stateOff() {
digitalWrite(pin, LOW);
}
};
class Button {
private:
int pin;
int lastState;
int currentState;
public:
Button(int p) : pin(p), lastState(LOW), currentState(LOW) {
pinMode(pin, INPUT_PULLUP);
}
void update() {
lastState = currentState;
currentState = digitalRead(pin);
}
bool isPressed() {
return currentState == LOW;
}
int getState() {
return currentState;
}
};
class Controlled {
private:
Led* leds[6];
int currentLed;
public:
Controlled() : currentLed(0) {
for (int i = 0; i < 6; i++) {
leds[i] = new Led(ledPins[i]);
}
}
void stateUp() {
if (currentLed < 3) {
leds[currentLed]->stateOn();
currentLed++;
}
}
void stateDown() {
for (int i = 0; i < 6; i++) {
leds[i]->stateOff();
}
currentLed = 0;
}
};
Controlled controller;
void setup() {
// ไม่ต้องทำอะไรใน setup
}
void loop() {
controller.stateUp();
delay(500);
controller.stateDown();
delay(500);
}