0% found this document useful (0 votes)
2 views7 pages

LAB1_IOT

The document contains code examples for controlling RGB LEDs and LED matrices using Arduino. It includes functions to display colors, create a rainbow spectrum, and animate LED patterns. The code is structured into separate functions for clarity and reusability, demonstrating various LED configurations and effects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

LAB1_IOT

The document contains code examples for controlling RGB LEDs and LED matrices using Arduino. It includes functions to display colors, create a rainbow spectrum, and animate LED patterns. The code is structured into separate functions for clarity and reusability, demonstrating various LED configurations and effects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

LAB1

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;

const int DISPLAY_TIME = 1000;


void setup() {
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}

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);

// Yellow (Red and Green)


digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
delay(DISPLAY_TIME);

// Cyan (Green and Blue)


digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
delay(DISPLAY_TIME);

// Purple (Red and Blue)


digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
delay(DISPLAY_TIME);

// White (turn all the LEDs on)


digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
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;

color = constrain(color, 0, 767); // constrain the input value to a range of values


from 0 to 767

// if statement breaks down the "color" into three ranges:


if (color <= 255) // RANGE 1 (0 - 255) - red to green
{
redIntensity = 255 - color; // red goes from on to off
greenIntensity = color; // green goes from off to on
blueIntensity = 0; // blue is always off
} else if (color <= 511) // RANGE 2 (256 - 511) - green to blue
{
redIntensity = 0; // red is always off
greenIntensity = 511 - color; // green on to off
blueIntensity = color - 256; // blue off to on
} else // RANGE 3 ( >= 512)- blue to red
{
redIntensity = color - 512; // red off to on
greenIntensity = 0; // green is always off
blueIntensity = 767 - color; // blue on to off
}

// "send" intensity values to the Red, Green, Blue Pins using analogWrite()
analogWrite(RED_PIN, redIntensity);
analogWrite(GREEN_PIN, greenIntensity);
analogWrite(BLUE_PIN, blueIntensity);
}

- Bài 1: Dãy đèn Led


- Code:
int timer = 100; // The higher the number, the slower the timing.

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);
}

// loop from the highest pin to the lowest:


for (int thisPin = 7; thisPin >= 2; thisPin--) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);
}
}

- Bài 3: Led Matrix

- 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

const byte rows[] = {


ROW_1,
ROW_2,
ROW_3,
ROW_4,
ROW_5,
ROW_6,
ROW_7,
ROW_8
};
const byte cols[] = {
COL_1,
COL_2,
COL_3,
COL_4,
COL_5,
COL_6,
COL_7,
COL_8
};

// The display buffer


// It's prefilled with a smiling face (1 = ON, 0 = OFF)
// https://xantorohara.github.io/led-matrix-editor/
byte smileFace[] = {
B11000011,
B10111101,
B01011010,
B01111110,
B01011010,
B01100110,
B10111101,
B11000011
};
byte sadFace[] = {
B11000011,
B10111101,
B01011010,
B01111110,
B01100110,
B01011010,
B10111101,
B11000011
};

float timeCount = 0;
const int speedChange = 100;

void setup() {
// Open serial port
Serial.begin(9600);

// Set all used pins to OUTPUT


// This is very important! If the pins are set to input
// the display will be very dim.
for (byte i = 2; i <= 13; i++)
pinMode(i, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(A4, OUTPUT);
pinMode(A5, OUTPUT);
}

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;
}
}

void drawMatrix(byte data[]) {


// Turn on each row in series
for (byte i = 0; i < 8; i++) {
digitalWrite(rows[i], HIGH); //initiate whole row
// Turn on each point in row
for (byte a = 0; a < 8; a++) {
// if You set (~data[i] >> a) then You will have positive
digitalWrite(cols[a], (data[i] >> a) & 0x01); // initiate whole column

delayMicroseconds(100); // uncoment deley for diferent speed of display

digitalWrite(cols[a], HIGH); // reset whole column


}
digitalWrite(rows[i], LOW); // reset whole row
// otherwise last row will intersect with next row
}
}

You might also like