CharliePlexing

Charlieplexing ist eine Technik zum Ansteuern eines Multiplex-Displays,
bei der relativ wenige I/O-Pins auf einem Mikrocontroller verwendet werden,
z. B. um ein Array von LEDs anzusteuern .

Quelle: Wikipedia

Arduino Seite: Charlie Chaser

Mein Video: Kai bei Youtube

Der Code:

int ledPins[] = {5, 6, 10, 11};
int pinCount = 4;

class charlie{
  int pin1,pin2;
  
  public:
  charlie(int firstPin, int secondPin){
    pin1 = firstPin;
    pin2 = secondPin;
  }
  
  void flash(int duration, bool forward){
    setupPins();
    if(forward){
        digitalWrite(pin1, LOW);
        digitalWrite(pin2, HIGH);
    } else {
        digitalWrite(pin1, HIGH);
        digitalWrite(pin2, LOW);
    }
      delay(duration);
  }
  
  void flash(int duration, bool forward, int intensity){
    setupPins();
    if(forward){
        analogWrite(pin1, intensity);
        digitalWrite(pin2, HIGH);
    } else {
        digitalWrite(pin1, HIGH);
        analogWrite(pin2, intensity);
    }
      delay(duration);
  }
  
  private:
  
  void setupPins(){
    for (int thisPin = 0; thisPin < pinCount; thisPin++) {
        if(ledPins[thisPin] != pin1 and ledPins[thisPin] != pin2){
          pinMode(ledPins[thisPin], INPUT);
        }
    };
    
    pinMode(pin1, OUTPUT);
      pinMode(pin2, OUTPUT);
  }
};

charlie C1(6,10);
charlie C2(11,10);
charlie C3(11,6);
charlie C4(11,5);
charlie C5(10,5);
charlie C6(6,5);

charlie Charlies[] = {C1,C2,C3,C4,C5,C6};

void setup()
{
  
}

void loop()
{
  int duration = 40;
  int durationStep = 3;
  int intensityStep = 3;
  
  for(int intensity = 240; intensity > 0; intensity += 0){
    for (int thisCharlie = 0; thisCharlie < 6; thisCharlie++) {
        Charlies[thisCharlie].flash(duration, true, intensity);
        intensity -= intensityStep;
    };
    for (int thisCharlie = 5; thisCharlie >= 0; thisCharlie--) {
        Charlies[thisCharlie].flash(duration, false, intensity);
        intensity -= intensityStep;
    };
    duration -= durationStep;
  }
  
  for (int thisCharlie = 0; thisCharlie < 6; thisCharlie++) {
        Charlies[thisCharlie].flash(duration, true);
    };
    for (int thisCharlie = 5; thisCharlie >= 0; thisCharlie--) {
        Charlies[thisCharlie].flash(duration, false);
    };
  
  for(int intensity = 0; intensity < 240; intensity += 0){
    for (int thisCharlie = 0; thisCharlie < 6; thisCharlie++) {
        Charlies[thisCharlie].flash(duration, true, intensity);
        intensity += intensityStep;
    };
    for (int thisCharlie = 5; thisCharlie >= 0; thisCharlie--) {
        Charlies[thisCharlie].flash(duration, false, intensity);
        intensity += intensityStep;
    };
    duration += durationStep;
  }
}