-1

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?

asked Aug 15 at 16:17
1
  • 2
    Not reproducible on a real Arduino, maybe this is the TinkerCad specific error. Commented Aug 16 at 2:56

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.