top of page
Rechercher

5.6 - Plus de couleurs avec la LED RGB

  • L C
  • 30 oct. 2024
  • 1 min de lecture

Dernière mise à jour : 11 déc. 2024

Matériel


  • 1 carte Arduino Uno

  • 1 breadboard

  • 1 LED RGB

  • 3 résistances 220 Ω

  • Fils de connexion


Montage



Sketch


// Couleurs au format RGB

const byte COLOR_BLACK = 0b000;

const byte COLOR_RED = 0b100;

const byte COLOR_GREEN = 0b010;

const byte COLOR_BLUE = 0b001;

const byte COLOR_MAGENTA = 0b101;

const byte COLOR_CYAN = 0b011;

const byte COLOR_YELLOW = 0b110;

const byte COLOR_WHITE = 0b111;


// Broches

const byte PIN_LED_R = 9;

const byte PIN_LED_G = 10;

const byte PIN_LED_B = 11;


void setup() {


// Initialise les broches

pinMode(PIN_LED_R, OUTPUT);

pinMode(PIN_LED_G, OUTPUT);

pinMode(PIN_LED_B, OUTPUT);

displayColor(COLOR_BLACK);

}


void loop() {

// la LED s'allume en rouge, vert, bleu, magentacyan, jaune, blanc et noir


displayColor(COLOR_RED);

delay(1000);

displayColor(COLOR_GREEN);

delay(1000);

displayColor(COLOR_BLUE);

delay(1000);

displayColor(COLOR_MAGENTA);

delay(1000);

displayColor(COLOR_CYAN);

delay(1000);

displayColor(COLOR_YELLOW);

delay(1000);

displayColor(COLOR_WHITE);

delay(1000);

displayColor(COLOR_BLACK);

delay(1000);

}


// displayColor

void displayColor(byte color) {


// Assigne l'état des broches

// Version cathode commune

//digitalWrite(PIN_LED_R, bitRead(color, 2));

//digitalWrite(PIN_LED_G, bitRead(color, 1));

//digitalWrite(PIN_LED_B, bitRead(color, 0));


// Version anode commune

digitalWrite(PIN_LED_R, !bitRead(color, 2));

digitalWrite(PIN_LED_G, !bitRead(color, 1));

digitalWrite(PIN_LED_B, !bitRead(color, 0));

}


Réalisation



bottom of page