I'm writing a program to display leds that sequentially light up in one direction until the last led at which point the process reverses. I'm using an OOP approch for practice. Below is my code in its entirety:
// Bouncing LEDs
enum class LED_STATE {OFF, ON};
constexpr unsigned long HALF_SEC {500}; // ms
constexpr int NUM_LED {8};
constexpr int LED_PINS[NUM_LED] = {6, 7, 8, 9, 10, 11, 12, 13};
constexpr int INVALID {-1}; // flag invalid pin number
class LED {
private:
int pin;
public:
LED() : pin(INVALID) {} // default constructor
void begin(int p) {
pin = p;
pinMode(p, OUTPUT);
}
void setState(LED_STATE state) {
digitalWrite(pin, state == LED_STATE::ON ? HIGH : LOW);
}
};
LED leds[NUM_LED];
void setup() {
for (int i = 0; i < NUM_LED; ++i) {
leds[i].begin(LED_PINS[i]);
}
}
void loop() {
// put your main code here, to run repeatedly:
for (int i = 0; i < NUM_LED; ++i) {
leds[i].setState(LED_STATE::ON);
delay(HALF_SEC);
leds[i].setState(LED_STATE::OFF);
}
for (int i = NUM_LED-2; i > 0; --i) {
leds[i].setState(LED_STATE::ON);
delay(HALF_SEC);
leds[i].setState(LED_STATE::OFF);
}
}
In compiling this with Tinkercad simulator, the compiler complains:
error: variable or field 'setState' declared void
error: 'LED_STATE' was not declared in this scope
However, declaring auto state instead of LED_STATE state in the argument for setState makes everything work perfectly. What happens?
mha hart. mah sole
asked Aug 15 at 16:17
-
2Not reproducible on a real Arduino, maybe this is the TinkerCad specific error.hcheung– hcheung08/16/2025 02:56:56Commented Aug 16 at 2:56
lang-cpp