LAB1_IOT
LAB1_IOT
Nhóm:
Lê Nhựt Hào
Mai Thiên Ân
Đỗ Việt Hoàng
Nguyễn Văn Nhật
- Bài 2: Led RGB
- Code:
// Make an RGB LED display a rainbow of colors!
// Chân 9, 10, 11 hỗ trợ PWM
const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;
void loop() {
mainColors(); // Red, Green, Blue, Yellow, Cyan, Purple, White
showSpectrum(); // Gradual fade from Red to Green to Blue to Red
}
/******************************************************************
* void mainColors()
* This function displays the eight "main" colors that the RGB LED
* can produce. If you'd like to use one of these colors in your
* own sketch, you can copy and paste that section into your code.
/*****************************************************************/
void mainColors() {
// all LEDs off
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
delay(DISPLAY_TIME);
// Red
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
delay(DISPLAY_TIME);
// Green
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
delay(DISPLAY_TIME);
// Blue
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
delay(DISPLAY_TIME);
/******************************************************************
* void showSpectrum()
*
* Steps through all the colors of the RGB LED, displaying a rainbow.
* showSpectrum() calls a function RGB(int color) that translates a number
* from 0 to 767 where 0 = all RED, 767 = all RED
*
* Breaking down tasks down into individual functions like this
* makes your code easier to follow, and it allows.
* parts of your code to be re-used.
/*****************************************************************/
void showSpectrum() {
for (int x = 0; x <= 767; x++) {
RGB(x); // Increment x and call RGB() to progress through colors.
delay(10);
}
}
/******************************************************************
* void RGB(int color)
*
* RGB(###) displays a single color on the RGB LED.
* Call RGB(###) with the number of a color you want
* to display. For example, RGB(0) displays pure RED, RGB(255)
* displays pure green.
*
* This function translates a number between 0 and 767 into a
* specific color on the RGB LED. If you have this number count
* through the whole range (0 to 767), the LED will smoothly
* change color through the entire spectrum.
*
* The "base" numbers are:
* 0 = pure red
* 255 = pure green
* 511 = pure blue
* 767 = pure red (again)
*
* Numbers between the above colors will create blends. For
* example, 640 is midway between 512 (pure blue) and 767
* (pure red). It will give you a 50/50 mix of blue and red,
* resulting in purple.
/*****************************************************************/
void RGB(int color) {
int redIntensity;
int greenIntensity;
int blueIntensity;
// "send" intensity values to the Red, Green, Blue Pins using analogWrite()
analogWrite(RED_PIN, redIntensity);
analogWrite(GREEN_PIN, greenIntensity);
analogWrite(BLUE_PIN, blueIntensity);
}
void setup() {
// use a for loop to initialize each pin as an output:
for (int thisPin = 2; thisPin < 8; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 2; thisPin < 8; thisPin++) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);
}
- Code:
#define ROW_1 2
#define ROW_2 7
#define ROW_3 19
#define ROW_4 5
#define ROW_5 13
#define ROW_6 18
#define ROW_7 12
#define ROW_8 16
#define COL_1 9
#define COL_2 8
#define COL_3 4
#define COL_4 17
#define COL_5 3
#define COL_6 10
#define COL_7 11
#define COL_8 6
float timeCount = 0;
const int speedChange = 100;
void setup() {
// Open serial port
Serial.begin(9600);
void loop() {
delay(5);
timeCount += 1;
if (timeCount < speedChange) {
drawMatrix(smileFace);
} else if (timeCount < 2 * speedChange) {
drawMatrix(sadFace);
} else {
// back to the start
timeCount = 0;
}
}