Arduino > Classe

/*
Circuit:
Attacher une DEL rouge à la broche 2 (avec une résistance appropriée) et
une DEL verte à la broche 3 (avec une résistance appropriée) .
*/


class Led {
        private:
        byte pin;
        byte state;
        int interval;
        unsigned long lastMillis;

        public:
        Led(byte _pin, int _interval) {
                pin = _pin;
                interval = _interval;
                pinMode(pin, OUTPUT);
        }

        void update() {
                if(millis() - lastMillis > interval) {
                        lastMillis = millis();
                        if (state == LOW)
                        state = HIGH;
                        else
                        state = LOW;
                        digitalWrite(pin, state);
                }
        }

};

Led red(2,1000);
Led green(3,500);
/*
// La ligne suivant démontre comment initialiser des classes dans un tableau:
Led leds[2] = {Led(2,1000), Led(3,500)};
*/


void setup() {

}

void loop()
{

        red.update();
        green.update();

}