Arduino project no. 2: RGB LEDs and struct
Written by RHIBA Zakaria, Zakpy Academy
In this project we change the colour of two RGB LEDs at the same time: red, then green, then blue. To keep the code clean even with several LEDs, we use a struct.
An RGB LED is really three LEDs in one package: a red one, a green one and a blue one. To get red, we turn on the red pin and turn off the green and blue pins.
What you will learn
How an RGB LED works.
Group several variables in a struct.
Write functions that take a parameter, so we do not repeat the same code.
What you need
An Arduino Uno or Mega board
2 common cathode RGB LEDs
6 220 Ω resistors (one per colour)
A breadboard and wires
Wiring
LED
Red
Green
Blue
Common
LED 1
pin 9
pin 10
pin 11
GND
LED 2
pin 5
pin 6
pin 7
GND
Each colour goes through its own 220 Ω resistor. The longest leg of the LED is the common pin.
The struct
A struct lets us create our own type of variable that groups several values. An RGB LED always has 3 pins, so it is the right tool:
structRGB{intred;intgreen;intblue;};
Then we create our two LEDs by giving the pin of each colour:
RGBled1={9,10,11};RGBled2={5,6,7};
The functions
We write one function per colour. It receives the LED as a parameter, so the same function works for both LEDs:
The comments in the code are in French, like the .ino file you download.
/* * Projet : changer la couleur de deux LED RGB avec une struct * Zakpy Academy - RHIBA Zakaria * Carte : Arduino Uno ou Mega * * LED RGB à cathode commune : la broche commune va au GND, * et chaque couleur passe par une résistance de 220 ohms. */// Une LED RGB, c'est 3 broches : rouge, vert et bleustructRGB{intred;intgreen;intblue;};RGBled1={9,10,11};// broches de la première LEDRGBled2={5,6,7};// broches de la deuxième LED// Met les 3 broches d'une LED en sortievoidSetupLed(RGBled){pinMode(led.red,OUTPUT);pinMode(led.green,OUTPUT);pinMode(led.blue,OUTPUT);}// Allume la couleur rouge seulementvoidred(RGBled){digitalWrite(led.red,HIGH);digitalWrite(led.green,LOW);digitalWrite(led.blue,LOW);}// Allume la couleur verte seulementvoidgreen(RGBled){digitalWrite(led.red,LOW);digitalWrite(led.green,HIGH);digitalWrite(led.blue,LOW);}// Allume la couleur bleue seulementvoidblue(RGBled){digitalWrite(led.red,LOW);digitalWrite(led.green,LOW);digitalWrite(led.blue,HIGH);}voidsetup(){SetupLed(led1);SetupLed(led2);}voidloop(){red(led1);red(led2);delay(1000);// rouge pendant 1 sgreen(led1);green(led2);delay(1000);// vert pendant 1 sblue(led1);blue(led2);delay(1000);// bleu pendant 1 s}
Going further
Mix the colours: red + green gives yellow, red + blue gives purple.
Use analogWrite() instead of digitalWrite() to get shades of colour. Careful, you need PWM pins (on the Uno, pin 7 is not PWM).
If your LED is common anode, the common pin goes to 5 V and you must swap HIGH and LOW.
Projet Arduino n°2 : LED RGB et struct
Écrit par RHIBA Zakaria, Zakpy Academy
Dans ce projet on fait changer la couleur de deux LED RGB en même temps : rouge, puis vert, puis bleu. Pour que le code reste propre même avec plusieurs LED, on utilise une struct.
Une LED RGB, c'est en fait trois LED dans un seul boîtier : une rouge, une verte et une bleue. Pour avoir du rouge, on allume la broche rouge et on éteint les broches verte et bleue.
Ce qu'on va apprendre
Comment marche une LED RGB.
Regrouper plusieurs variables dans une struct.
Écrire des fonctions qui prennent un paramètre, pour ne pas répéter le même code.
Le matériel
Une carte Arduino Uno ou Mega
2 LED RGB à cathode commune
6 résistances de 220 Ω (une par couleur)
Une breadboard et des fils
Le câblage
LED
Rouge
Vert
Bleu
Commun
LED 1
broche 9
broche 10
broche 11
GND
LED 2
broche 5
broche 6
broche 7
GND
Chaque couleur passe par sa propre résistance de 220 Ω. La patte la plus longue de la LED est la broche commune.
La struct
Une struct permet de créer notre propre type de variable, qui regroupe plusieurs valeurs. Une LED RGB a toujours 3 broches, donc c'est le bon outil :
structRGB{intred;intgreen;intblue;};
Ensuite on crée nos deux LED en donnant les broches de chaque couleur :
RGBled1={9,10,11};RGBled2={5,6,7};
Les fonctions
On écrit une fonction pour chaque couleur. Elle reçoit la LED en paramètre, donc la même fonction marche pour les deux LED :
/* * Projet : changer la couleur de deux LED RGB avec une struct * Zakpy Academy - RHIBA Zakaria * Carte : Arduino Uno ou Mega * * LED RGB à cathode commune : la broche commune va au GND, * et chaque couleur passe par une résistance de 220 ohms. */// Une LED RGB, c'est 3 broches : rouge, vert et bleustructRGB{intred;intgreen;intblue;};RGBled1={9,10,11};// broches de la première LEDRGBled2={5,6,7};// broches de la deuxième LED// Met les 3 broches d'une LED en sortievoidSetupLed(RGBled){pinMode(led.red,OUTPUT);pinMode(led.green,OUTPUT);pinMode(led.blue,OUTPUT);}// Allume la couleur rouge seulementvoidred(RGBled){digitalWrite(led.red,HIGH);digitalWrite(led.green,LOW);digitalWrite(led.blue,LOW);}// Allume la couleur verte seulementvoidgreen(RGBled){digitalWrite(led.red,LOW);digitalWrite(led.green,HIGH);digitalWrite(led.blue,LOW);}// Allume la couleur bleue seulementvoidblue(RGBled){digitalWrite(led.red,LOW);digitalWrite(led.green,LOW);digitalWrite(led.blue,HIGH);}voidsetup(){SetupLed(led1);SetupLed(led2);}voidloop(){red(led1);red(led2);delay(1000);// rouge pendant 1 sgreen(led1);green(led2);delay(1000);// vert pendant 1 sblue(led1);blue(led2);delay(1000);// bleu pendant 1 s}
Pour aller plus loin
Mélange les couleurs : rouge + vert donne du jaune, rouge + bleu donne du violet.
Utilise analogWrite() à la place de digitalWrite() pour avoir des nuances de couleur. Attention, il faut des broches PWM (sur la Uno, la broche 7 n'est pas PWM).
Si ta LED est à anode commune, la broche commune va au 5 V et il faut inverser HIGH et LOW.
مشروع Arduino رقم 2: مصابيح RGB و struct
إعداد: RHIBA Zakaria، Zakpy Academy
في هذا المشروع نغير لون مصباحين RGB في الوقت نفسه: الأحمر، ثم الأخضر، ثم الأزرق. ولكي تبقى الشيفرة منظمة حتى مع عدة مصابيح، نستخدم struct.
مصباح RGB هو في الحقيقة ثلاثة مصابيح في غلاف واحد: أحمر وأخضر وأزرق. للحصول على اللون الأحمر نشغل منفذ اللون الأحمر ونطفئ منفذي الأخضر والأزرق.
ما الذي سنتعلمه
طريقة عمل مصباح RGB.
تجميع عدة متغيرات داخل struct.
كتابة دوال تستقبل معاملًا، حتى لا نكرر الشيفرة نفسها.
المكونات
لوحة Arduino Uno أو Mega
مصباحا RGB بمهبط مشترك
6 مقاومات 220 أوم (واحدة لكل لون)
لوحة تجارب وأسلاك توصيل
التوصيل
المصباح
الأحمر
الأخضر
الأزرق
المشترك
المصباح 1
المنفذ 9
المنفذ 10
المنفذ 11
GND
المصباح 2
المنفذ 5
المنفذ 6
المنفذ 7
GND
كل لون يمر عبر مقاومة خاصة به قيمتها 220 أوم. الساق الأطول في المصباح هي المنفذ المشترك.
البنية struct
تسمح struct بإنشاء نوع متغير خاص بنا يجمع عدة قيم. ولأن لمصباح RGB ثلاثة منافذ دائمًا، فهي الأداة المناسبة:
structRGB{intred;intgreen;intblue;};
بعد ذلك ننشئ المصباحين ونحدد منفذ كل لون:
RGBled1={9,10,11};RGBled2={5,6,7};
الدوال
نكتب دالة لكل لون. تستقبل الدالة المصباح كمعامل، فتعمل الدالة نفسها مع المصباحين:
التعليقات داخل الشيفرة باللغة الفرنسية، كما في ملف .ino الذي يمكنك تحميله.
/* * Projet : changer la couleur de deux LED RGB avec une struct * Zakpy Academy - RHIBA Zakaria * Carte : Arduino Uno ou Mega * * LED RGB à cathode commune : la broche commune va au GND, * et chaque couleur passe par une résistance de 220 ohms. */// Une LED RGB, c'est 3 broches : rouge, vert et bleustructRGB{intred;intgreen;intblue;};RGBled1={9,10,11};// broches de la première LEDRGBled2={5,6,7};// broches de la deuxième LED// Met les 3 broches d'une LED en sortievoidSetupLed(RGBled){pinMode(led.red,OUTPUT);pinMode(led.green,OUTPUT);pinMode(led.blue,OUTPUT);}// Allume la couleur rouge seulementvoidred(RGBled){digitalWrite(led.red,HIGH);digitalWrite(led.green,LOW);digitalWrite(led.blue,LOW);}// Allume la couleur verte seulementvoidgreen(RGBled){digitalWrite(led.red,LOW);digitalWrite(led.green,HIGH);digitalWrite(led.blue,LOW);}// Allume la couleur bleue seulementvoidblue(RGBled){digitalWrite(led.red,LOW);digitalWrite(led.green,LOW);digitalWrite(led.blue,HIGH);}voidsetup(){SetupLed(led1);SetupLed(led2);}voidloop(){red(led1);red(led2);delay(1000);// rouge pendant 1 sgreen(led1);green(led2);delay(1000);// vert pendant 1 sblue(led1);blue(led2);delay(1000);// bleu pendant 1 s}
للتعمق أكثر
امزج الألوان: الأحمر مع الأخضر يعطي الأصفر، والأحمر مع الأزرق يعطي البنفسجي.
استخدم analogWrite() بدل digitalWrite() للحصول على درجات من الألوان. انتبه، تحتاج إلى منافذ PWM (في لوحة Uno المنفذ 7 ليس من نوع PWM).
إذا كان مصباحك بمصعد مشترك، يوصل المنفذ المشترك بـ 5 فولت ويجب عكس HIGH و LOW.