搜索代码

Lesson 58-3: RGB LED Control with Common Anode

Lesson 58-3: RGB LED Control with Common Anode

476-Lesson 58: What is a multidimensional array? Example of an RGB LED
语言: C++
/*
 * Lesson 58: What is a multidimensional Array? RGB LED example
 
 * This Arduino sketch is to control color on an RGB LED
 
  Please watch the 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 6th, 2018 at 14:00 in Ajax, Ontario, Canada

 
  
 * 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 downloaded 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 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},// aqua
               {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,10, 9 for RED , GREEN and BLUE to be an output:
  pinMode(RED, OUTPUT);
     digitalWrite(RED,LOW);// turn RED led off
  pinMode(GREEN, OUTPUT);
     digitalWrite(GREEN,LOW);// turn RED led off  
  pinMode(BLUE, OUTPUT);
     digitalWrite(BLUE,LOW);// turn RED led off  
}

// the loop routine runs over and over again forever:
void loop() {

  // Robojax.com RGB LED code 20181006

    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
      }
  Serial.println("====Repeat");
  delay(2000);
}

/*
 * Written by Ahmad Shamshiri for Robojax.com
 * 
  Sends correct values to R, G, and B LED pins.
  
  @param r is the received red value
  @param g is the received green value
  @param b is the received blue value
 */
void RGB_LED(int r, int g, int 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    
                      
}

资源与参考

尚无可用资源。

文件📁

没有可用的文件。