Lesson 58 : What is multidimensional Array? Example of RGB LED
Lesson 58 : What is multidimensional Array? Example of RGB LED
Please select other codes for this lecture from the links below.
Part 6: Arduino Practial Programming
In this lesson we learn about multidimensional array. Regular array is single dimension which is list of values in one variable. But when you have related values like lenght and width of objects, then we can define two dimensional array to hold lenght and width for the same object in the same index. This allows to have dimensions for example of items in a multidimensional array.
If we want for example to meausre voltage and current at different intervals of time we can use a two dimensional array. or we can use a three dimensional array two assign values of color red, green and blue for RGB LED. In the next lession we practially use it with RGB LED. Please watch the video for full details.
- 00:00 Introduction to multidimensional array
- 04:25 Reading value of a multidimensional array (this code)
- 05:50 Updating value of a multidimensional array
- 08:57 Defining 3 dimensional array
- 10:29 Dreading 3 dimensional array values
- 11:37 Updating 3 dimensional array values
- 12:39 Defining empty 3 dimensional array
/*
* Lesson 58: What is multidimensional Array? RGB LED example
* Works for both Common Anode and Common Cathode RGB LEDs
* This Arduino sktech is to control color on REG LED
Please watch full detailed video: https://youtu.be/YuGJpFeoq5I
Location of this code: https://robojax.com/course1/?vid=lecture59
* Written by Ahmad Shamshiri for Robojax.com
* on October 06th 2018 at 14:00 in Ajax, Ontario, Canada
If you found this tutorial helpful, please support me so I can continue creating
content like this. You can support me on Patreon http://robojax.com/L/?id=63
or make donation using PayPal http://robojax.com/L/?id=64
* * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been download from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
int RED = 11;// the PWM pin the RED LED is attached to
int GREEN = 10;// the PWM pin the GREEN LED is attached to
int BLUE = 9;// the PWM pin the BLUE LED is attached to
int colorTime = 2000; // time to wait between color change
int type =1; // 0 for common cathode LED
// 1 for common anode LED
int color[8][3] =
{
{255,255,255},// white
{255,0, 0},// red
{0, 255, 0},// green
{0, 0, 255},// blue
{255,255, 0},// yellow
{0, 255,255},// a qua
{255,0, 255},// pink
{255,125, 0},//Orange
};
String colorName[8]=
{
"White",
"Red",
"Green",
"Blue",
"Yellow",
"Aqua",
"Pink",
"Orange"
};
void setup() {
// Robojax.com RGB LED code 20181006
Serial.begin(9600);
// declare pin 11,12, 13 for RED , GREEN and BLUE to be an output:
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// Robojax.com RGB LED code 20181006
delay(3000);
for(int j=0; j<8; j++)
{
RGB_LED(color[j][0], color[j][1], color[j][2]);
Serial.println(colorName[j]);
delay(colorTime);// delay for each color
}
}
/*
sends correct value to R, G and B and LED pin
this funciton is for commond Cathode LEDs and converts regular PWM
@param r is received red value
@param g is received green value
@param b is received blue value
*/
void RGB_LED(int r, int g, int b)
{
if(type == 1)
{
r = 255-r;
g = 255-g;
b = 255-b;
}
analogWrite(RED, r);// set value for RED LED
analogWrite(GREEN, g);// set value for GREEN LED
analogWrite(BLUE, b);// set value for BLUE LED
}