Last active
November 12, 2019 04:36
-
-
Save tranphuquy19/a62dbaba7287b60bb039d2592321a569 to your computer and use it in GitHub Desktop.
Lap lập trình nhúng
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
shiftOut với 8 LED bằng 1 IC HC595 | |
*/ | |
//chân ST_CP của 74HC595 | |
int latchPin = 8; | |
//chân SH_CP của 74HC595 | |
int clockPin = 12; | |
//Chân DS của 74HC595 | |
int dataPin = 11; | |
//Trạng thái của LED, hay chính là byte mà ta sẽ gửi qua shiftOut | |
byte ledStatus; | |
void setup() { | |
//Bạn BUỘC PHẢI pinMode các chân này là OUTPUT | |
pinMode(latchPin, OUTPUT); | |
pinMode(clockPin, OUTPUT); | |
pinMode(dataPin, OUTPUT); | |
} | |
void loop() { | |
//Sáng tuần tự | |
ledStatus = 0;//mặc định là không có đèn nào sáng hết (0 = 0b00000000) | |
for (int i = 0; i < 8; i++) { | |
ledStatus = (ledStatus << 1) | 1; | |
/** | |
Bắt buộc phải có để shiftOut | |
**/ | |
digitalWrite(latchPin, LOW); //các đèn LED sẽ không sáng khi bạn digital LOW | |
//ShiftOut ra IC | |
shiftOut(dataPin, clockPin, MSBFIRST, ledStatus); | |
digitalWrite(latchPin, HIGH);//các đèn LED sẽ sáng với trạng thái vừa được cập nhập | |
/** | |
Kết thúc bắt buộc phải có | |
**/ | |
delay(500); // Dừng chương trình khoảng 500 mili giây để thấy các hiệu ứng của đèn LED | |
} | |
//Tắt tuần tự | |
for (int i = 0;i<8;i++) { | |
ledStatus <<= 1; //Đẩy tất cả các bit qua bên trái 1 bit | |
digitalWrite(latchPin, LOW); | |
shiftOut(dataPin, clockPin, MSBFIRST, ledStatus); | |
digitalWrite(latchPin, HIGH); | |
delay(500); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
shiftOut với 8 LED bằng 1 IC HC595 | |
*/ | |
//chân ST_CP của 74HC595 | |
int latchPin = 8; | |
//chân SH_CP của 74HC595 | |
int clockPin = 12; | |
//Chân DS của 74HC595 | |
int dataPin = 11; | |
//Trạng thái của LED, hay chính là byte mà ta sẽ gửi qua shiftOut | |
byte ledStatus; | |
void setup() { | |
//Bạn BUỘC PHẢI pinMode các chân này là OUTPUT | |
pinMode(latchPin, OUTPUT); | |
pinMode(clockPin, OUTPUT); | |
pinMode(dataPin, OUTPUT); | |
} | |
void loop() { | |
//Sáng tuần tự | |
ledStatus = 2; | |
for (int i = 0; i < 8; i++) { | |
ledStatus = (ledStatus << 1) | 1; | |
digitalWrite(latchPin, LOW); | |
shiftOut(dataPin, clockPin, MSBFIRST, ledStatus); | |
digitalWrite(latchPin, HIGH); | |
delay(700); | |
} | |
digitalWrite(latchPin, LOW); | |
shiftOut(dataPin, clockPin, MSBFIRST, 0); | |
digitalWrite(latchPin, HIGH); | |
delay(700); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1 bầy đuổi 2 thằng | |
/* | |
shiftOut với 8 LED bằng 1 IC HC595 | |
*/ | |
//chân ST_CP của 74HC595 | |
int latchPin = 8; | |
//chân SH_CP của 74HC595 | |
int clockPin = 12; | |
//Chân DS của 74HC595 | |
int dataPin = 11; | |
//Trạng thái của LED, hay chính là byte mà ta sẽ gửi qua shiftOut | |
byte ledStatus; | |
void setup() { | |
//Bạn BUỘC PHẢI pinMode các chân này là OUTPUT | |
pinMode(latchPin, OUTPUT); | |
pinMode(clockPin, OUTPUT); | |
pinMode(dataPin, OUTPUT); | |
} | |
void loop() { | |
//Sáng tuần tự | |
ledStatus = 6; | |
for (int i = 0; i < 8; i++) { | |
ledStatus = (ledStatus << 1) | 1; | |
digitalWrite(latchPin, LOW); | |
shiftOut(dataPin, clockPin, MSBFIRST, ledStatus); | |
digitalWrite(latchPin, HIGH); | |
delay(700); | |
} | |
digitalWrite(latchPin, LOW); | |
shiftOut(dataPin, clockPin, MSBFIRST, 0); | |
digitalWrite(latchPin, HIGH); | |
delay(700); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//File mô phỏng proteus https://drive.google.com/file/d/1O0VOQ-Lt_XiESBPLek5U5liRMbymocJ-/view?usp=sharing | |
/* | |
shiftOut với 8 LED bằng 1 IC HC595 | |
*/ | |
//chân ST_CP của 74HC595 | |
int latchPin = 8; | |
//chân SH_CP của 74HC595 | |
int clockPin = 12; | |
//Chân DS của 74HC595 | |
int dataPin = 11; | |
//Trạng thái của LED, hay chính là byte mà ta sẽ gửi qua shiftOut | |
const int HC595_COUNT = 2;//Số lượng 595 mắc nối tiếp | |
byte ledStatus[HC595_COUNT]= {0}; | |
void setup() { | |
//Bạn BUỘC PHẢI pinMode các chân này là OUTPUT | |
pinMode(latchPin, OUTPUT); | |
pinMode(clockPin, OUTPUT); | |
pinMode(dataPin, OUTPUT); | |
} | |
void fillValueToArray(byte value) { | |
for (int i = 0;i < HC595_COUNT; i += 1) { | |
ledStatus[i] = value; | |
} | |
} | |
void shiftOutHC595(int dataPin, int clockPin, byte ledStatus[]) { | |
digitalWrite(latchPin, LOW); | |
for (int i = 0; i < HC595_COUNT; i++) { | |
shiftOut(dataPin,clockPin,MSBFIRST,ledStatus[i]); | |
} | |
digitalWrite(latchPin, HIGH); | |
} | |
void loop() { | |
//Sáng tuần tự | |
//vì ledStatus là một mảng vì vậy để mặc định tất cả đèn tắt thì chúng ta phải for đến từng giá trị của mảng rồi đặt giá trị là 0. | |
fillValueToArray(0); | |
//Bật tuần tự | |
for (int i = 0; i < HC595_COUNT; i++) { | |
for (byte j=0;j<8;j++) { | |
ledStatus[i] = (ledStatus[i] << 1) | 1; | |
shiftOutHC595(dataPin,clockPin,ledStatus); | |
delay(100); // Dừng chương trình khoảng 500 mili giây để thấy các hiệu ứng của đèn LED | |
} | |
} | |
//Tắt tuần tự | |
for (int i = 0; i < HC595_COUNT; i++) { | |
for (byte j=0;j<8;j++) { | |
ledStatus[i] = (ledStatus[i] << 1); | |
shiftOutHC595(dataPin,clockPin,ledStatus); | |
delay(100); // Dừng chương trình khoảng 500 mili giây để thấy các hiệu ứng của đèn LED | |
} | |
} | |
//Nhấp nháy nhanh | |
for (byte k = 0; k < 20; k++) { | |
fillValueToArray(0b10101010); | |
shiftOutHC595(dataPin,clockPin,ledStatus); | |
delay(50); | |
fillValueToArray(0b01010101); | |
shiftOutHC595(dataPin,clockPin,ledStatus); | |
delay(50); | |
} | |
//sáng 1 đèn rồi cách 1 đèn ko sáng rồi lại sáng những đèn chưa bật | |
fillValueToArray(0); | |
for (int i = 0; i < HC595_COUNT; i++) { | |
for (byte j = 0;j<8;j += 2) { | |
ledStatus[i] |= 1 << j; | |
shiftOutHC595(dataPin,clockPin,ledStatus); | |
delay(200); | |
} | |
} | |
for (int i = 0; i < HC595_COUNT; i++) { | |
for (byte j = 1;j<8;j += 2) { | |
ledStatus[i] |= 1 << j; | |
shiftOutHC595(dataPin,clockPin,ledStatus); | |
delay(200); | |
} | |
} | |
//Tắt dần theo thứ tự trên | |
for (int i = HC595_COUNT - 1; i >= 0; i--) { | |
for (int j = 7;j >= 0;j -= 2) { | |
ledStatus[i] &= ~(1 << j); | |
shiftOutHC595(dataPin,clockPin,ledStatus); | |
delay(200); | |
} | |
} | |
for (int i = HC595_COUNT - 1; i >= 0; i--) { | |
for (int j = 6;j >= 0;j -= 2) { | |
ledStatus[i] &= ~(1 << j); | |
shiftOutHC595(dataPin,clockPin,ledStatus); | |
delay(200); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//**************************************************************// | |
// Name : shiftOutCode, Dual One By One // | |
// Author : Carlyn Maw, Tom Igoe // | |
// Date : 25 Oct, 2006 // | |
// Version : 1.0 // | |
// Notes : Code for using a 74HC595 Shift Register // | |
// : to count from 0 to 255 // | |
//**************************************************************// | |
//Pin connected to ST_CP of 74HC595 | |
int latchPin = 8; | |
//Pin connected to SH_CP of 74HC595 | |
int clockPin = 12; | |
////Pin connected to DS of 74HC595 | |
int dataPin = 11; | |
//holder for infromation you're going to pass to shifting function | |
byte data = 0; | |
void setup() { | |
//set pins to output because they are addressed in the main loop | |
pinMode(latchPin, OUTPUT); | |
} | |
void loop() { | |
//function that blinks all the LEDs | |
//gets passed the number of blinks and the pause time | |
blinkAll_2Bytes(1,500); | |
// light each pin one by one using a function A | |
for (int j = 0; j < 8; j++) { | |
//ground latchPin and hold low for as long as you are transmitting | |
digitalWrite(latchPin, 0); | |
//red LEDs | |
lightShiftPinA(7-j); | |
//green LEDs | |
lightShiftPinA(j); | |
//return the latch pin high to signal chip that it | |
//no longer needs to listen for information | |
digitalWrite(latchPin, 1); | |
delay(1000); | |
} | |
// light each pin one by one using a function A | |
for (int j = 0; j < 8; j++) { | |
//ground latchPin and hold low for as long as you are transmitting | |
digitalWrite(latchPin, 0); | |
//red LEDs | |
lightShiftPinB(j); | |
//green LEDs | |
lightShiftPinB(7-j); | |
//return the latch pin high to signal chip that it | |
//no longer needs to listen for information | |
digitalWrite(latchPin, 1); | |
delay(1000); | |
} | |
} | |
//This function uses bitwise math to move the pins up | |
void lightShiftPinA(int p) { | |
//defines a local variable | |
int pin; | |
//this is line uses a bitwise operator | |
//shifting a bit left using << is the same | |
//as multiplying the decimal number by two. | |
pin = 1<< p; | |
//move 'em out | |
shiftOut(dataPin, clockPin, pin); | |
} | |
//This function uses that fact that each bit in a byte | |
//is 2 times greater than the one before it to | |
//shift the bits higher | |
void lightShiftPinB(int p) { | |
//defines a local variable | |
int pin; | |
//start with the pin = 1 so that if 0 is passed to this | |
//function pin 0 will light. | |
pin = 1; | |
for (int x = 0; x < p; x++) { | |
pin = pin * 2; | |
} | |
//move 'em out | |
shiftOut(dataPin, clockPin, pin); | |
} | |
// the heart of the program | |
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) { | |
// This shifts 8 bits out MSB first, | |
//on the rising edge of the clock, | |
//clock idles low | |
//internal function setup | |
int i=0; | |
int pinState; | |
pinMode(myClockPin, OUTPUT); | |
pinMode(myDataPin, OUTPUT); | |
//clear everything out just in case to | |
//prepare shift register for bit shifting | |
digitalWrite(myDataPin, 0); | |
digitalWrite(myClockPin, 0); | |
//for each bit in the byte myDataOut� | |
//NOTICE THAT WE ARE COUNTING DOWN in our for loop | |
//This means that %00000001 or "1" will go through such | |
//that it will be pin Q0 that lights. | |
for (i=7; i>=0; i--) { | |
digitalWrite(myClockPin, 0); | |
//if the value passed to myDataOut and a bitmask result | |
// true then... so if we are at i=6 and our value is | |
// %11010100 it would the code compares it to %01000000 | |
// and proceeds to set pinState to 1. | |
if ( myDataOut & (1<<i) ) { | |
pinState= 1; | |
} | |
else { | |
pinState= 0; | |
} | |
//Sets the pin to HIGH or LOW depending on pinState | |
digitalWrite(myDataPin, pinState); | |
//register shifts bits on upstroke of clock pin | |
digitalWrite(myClockPin, 1); | |
//zero the data pin after shift to prevent bleed through | |
digitalWrite(myDataPin, 0); | |
} | |
//stop shifting | |
digitalWrite(myClockPin, 0); | |
} | |
//blinks both registers based on the number of times you want to | |
//blink "n" and the pause between them "d" | |
//starts with a moment of darkness to make sure the first blink | |
//has its full visual effect. | |
void blinkAll_2Bytes(int n, int d) { | |
digitalWrite(latchPin, 0); | |
shiftOut(dataPin, clockPin, 0); | |
shiftOut(dataPin, clockPin, 0); | |
digitalWrite(latchPin, 1); | |
delay(200); | |
for (int x = 0; x < n; x++) { | |
digitalWrite(latchPin, 0); | |
shiftOut(dataPin, clockPin, 255); | |
shiftOut(dataPin, clockPin, 255); | |
digitalWrite(latchPin, 1); | |
delay(d); | |
digitalWrite(latchPin, 0); | |
shiftOut(dataPin, clockPin, 0); | |
shiftOut(dataPin, clockPin, 0); | |
digitalWrite(latchPin, 1); | |
delay(d); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//[-,+,~tro,12,-,11,*,*,*,*,6,5,4,3,+,-] | |
//[---Màn hình---] | |
#include <LiquidCrystal.h> | |
#define btn 2 | |
LiquidCrystal lcd(12, 11, 6, 5, 4, 3); | |
bool isDoC = true; | |
void toggleDo(){ | |
isDoC = !isDoC; | |
} | |
float getDoC() { | |
float temperature = (5.0 * analogRead(A0) * 100.0 / 1024.0); | |
return temperature; | |
} | |
void doC() { | |
lcd.clear(); | |
lcd.print(String(getDoC()) + " oC"); | |
// delay(1000); | |
} | |
void doF() { | |
lcd.clear(); | |
float f = getDoC()*1.8 + 32; | |
lcd.print(String(f) + " oF"); | |
} | |
void setup() { | |
//Thông báo đây là LCD 1602 | |
lcd.begin(16, 2); | |
pinMode(btn, INPUT_PULLUP); | |
attachInterrupt(0, toggleDo, LOW); | |
} | |
void loop() { | |
isDoC ? doC() : doF(); | |
delay(500); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
shiftOut với 8 LED bằng 1 IC HC595 | |
*/ | |
//chân ST_CP của 74HC595 | |
int latchPin = 8; | |
//chân SH_CP của 74HC595 | |
int clockPin = 12; | |
//Chân DS của 74HC595 | |
int dataPin = 11; | |
const int HC595_COUNT = 2;//Số lượng 595 mắc nối tiếp | |
byte ledStatus[HC595_COUNT] = {0, 0}; | |
void setup() { | |
//Bạn BUỘC PHẢI pinMode các chân này là OUTPUT | |
pinMode(latchPin, OUTPUT); | |
pinMode(clockPin, OUTPUT); | |
pinMode(dataPin, OUTPUT); | |
} | |
void fillValueToArray(byte value) { | |
for (int i = 0; i < HC595_COUNT; i += 1) { | |
ledStatus[i] = value; | |
} | |
} | |
void shiftOutHC595(int dataPin, int clockPin, byte ledStatus[]) { | |
digitalWrite(latchPin, LOW); | |
for (int i = 0; i < HC595_COUNT; i++) { | |
shiftOut(dataPin, clockPin, LSBFIRST, ledStatus[i]); | |
} | |
digitalWrite(latchPin, HIGH); | |
} | |
void drawImage(byte data[]) { | |
fillValueToArray(0); | |
int row[] = {1, 2, 4, 8, 16, 32, 64, 128}; | |
// int column[] = {128, 64, 32, 16, 8, 4, 2, 1}; | |
for (int i = 0 ; i < 8; i++) { | |
ledStatus[0] = ~data[i]; | |
ledStatus[1] = row[i]; | |
shiftOutHC595(dataPin, clockPin, ledStatus); | |
} | |
} | |
const byte IMAGES[][8] = { | |
{ | |
B11111110, | |
B10010010, | |
B10010010, | |
B10010010, | |
B10000010, | |
B10000010, | |
B11111110, | |
B00000000 | |
}, { | |
B11111110, | |
B10000110, | |
B10001010, | |
B10010010, | |
B10000010, | |
B10000010, | |
B11111110, | |
B00000000 | |
}, { | |
B11111110, | |
B10000010, | |
B10000010, | |
B10011110, | |
B10000010, | |
B10000010, | |
B11111110, | |
B00000000 | |
}, { | |
B11111110, | |
B10000010, | |
B10000010, | |
B10010010, | |
B10001010, | |
B10000110, | |
B11111110, | |
B00000000 | |
}, { | |
B11111110, | |
B10000010, | |
B10000010, | |
B10010010, | |
B10010010, | |
B10010010, | |
B11111110, | |
B00000000 | |
}, { | |
B11111110, | |
B10000010, | |
B10000010, | |
B10010010, | |
B10100010, | |
B11000010, | |
B11111110, | |
B00000000 | |
}, { | |
B11111110, | |
B10000010, | |
B10000010, | |
B11110010, | |
B10000010, | |
B10000010, | |
B11111110, | |
B00000000 | |
}, { | |
B11111110, | |
B11000010, | |
B10100010, | |
B10010010, | |
B10000010, | |
B10000010, | |
B11111110, | |
B00000000 | |
} | |
}; | |
int i = sizeof(IMAGES) / 8; | |
void loop() { | |
for (int loop = 0; loop < 750; loop ++ ) { | |
drawImage(IMAGES[i - 1]); | |
} | |
if (--i == 0) { | |
i = sizeof(IMAGES) / 8; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Code điều chỉnh 3 led Đỏ, Xanh, Vàng độ sáng từ 0, 255 qua Serial. Cú pháp R255 led Red sáng MAX=255, Y0 led Yellow sáng MIN=0... | |
* Created by @tranphuquy19 on 26/09/2019 | |
* Email: [email protected] | |
*/ | |
#include <Arduino.h> | |
int red3 = 3; | |
int blue5 = 5; | |
int yellow6 = 6; | |
void highLed(int led, int value); | |
void logger(String led, int value); | |
void setup() | |
{ | |
pinMode(red3, HIGH); | |
pinMode(blue5, HIGH); | |
pinMode(yellow6, HIGH); | |
Serial.begin(9600); | |
} | |
void loop() | |
{ | |
if (Serial.available()) | |
{ | |
String data = Serial.readString(); | |
char led; | |
int value; | |
led = data.charAt(0); | |
value = constrain(data.substring(1, data.length()).toInt(), 0, 255); | |
// DEBUG | |
// Serial.println("--------"); | |
// Serial.println(led); | |
// Serial.println(value); | |
switch (led) | |
{ | |
case 'R': | |
highLed(red3, value); | |
logger("Do", value); | |
break; | |
case 'B': | |
highLed(blue5, value); | |
logger("Xanh", value); | |
break; | |
case 'Y': | |
highLed(yellow6, value); | |
logger("Y", value); | |
break; | |
default: | |
Serial.println("Cac thong so bi sai"); | |
break; | |
} | |
} | |
} | |
void highLed(int led, int value) | |
{ | |
analogWrite(led, value); | |
//delay(200); | |
} | |
void logger(String led, int value) | |
{ | |
Serial.print("Ban bat den " + led + ", do sang: " + value); | |
float percent = (float)value/2.55; | |
Serial.print(" ("); | |
Serial.print(percent); | |
Serial.print("%)\n"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
shiftOut với 8 LED bằng 1 IC HC595 | |
*/ | |
//chân ST_CP của 74HC595 | |
int latchPin = 8; | |
//chân SH_CP của 74HC595 | |
int clockPin = 12; | |
//Chân DS của 74HC595 | |
int dataPin = 11; | |
const int HC595_COUNT = 2;//Số lượng 595 mắc nối tiếp | |
byte ledStatus[HC595_COUNT] = {0, 0}; | |
void setup() { | |
//Bạn BUỘC PHẢI pinMode các chân này là OUTPUT | |
pinMode(latchPin, OUTPUT); | |
pinMode(clockPin, OUTPUT); | |
pinMode(dataPin, OUTPUT); | |
} | |
void fillValueToArray(byte value) { | |
for (int i = 0; i < HC595_COUNT; i += 1) { | |
ledStatus[i] = value; | |
} | |
} | |
void shiftOutHC595(int dataPin, int clockPin, byte ledStatus[]) { | |
digitalWrite(latchPin, LOW); | |
for (int i = 0; i < HC595_COUNT; i++) { | |
shiftOut(dataPin, clockPin, MSBFIRST, ledStatus[i]); | |
} | |
digitalWrite(latchPin, HIGH); | |
} | |
void drawImage(byte data[]) { | |
fillValueToArray(0); | |
int row[] = {1, 2, 4, 8, 16, 32, 64, 128}; | |
// int column[] = {128, 64, 32, 16, 8, 4, 2, 1}; | |
for (int i = 0 ; i < 8; i++) { | |
ledStatus[0] = ~data[i]; | |
ledStatus[1] = row[i]; | |
shiftOutHC595(dataPin, clockPin, ledStatus); | |
} | |
} | |
const byte IMAGES[][8] = { | |
{ | |
B00000000, | |
B00000000, | |
B00000001, | |
B00000001, | |
B00000001, | |
B00000000, | |
B00000000, | |
B00000000 | |
}, { | |
B00000000, | |
B00000001, | |
B00000010, | |
B00000010, | |
B00000010, | |
B00000001, | |
B00000000, | |
B00000000 | |
}, { | |
B00000000, | |
B00000011, | |
B00000100, | |
B00000100, | |
B00000100, | |
B00000011, | |
B00000000, | |
B00000000 | |
}, { | |
B00000000, | |
B00000110, | |
B00001001, | |
B00001001, | |
B00001001, | |
B00000111, | |
B00000000, | |
B00000000 | |
}, { | |
B00000000, | |
B00001100, | |
B00010010, | |
B00010010, | |
B00010010, | |
B00001110, | |
B00000001, | |
B00000000 | |
}, { | |
B00000000, | |
B00011000, | |
B00100100, | |
B00100100, | |
B00100100, | |
B00011100, | |
B00000010, | |
B00000000 | |
}, { | |
B00000000, | |
B00110000, | |
B01001000, | |
B01001000, | |
B01001000, | |
B00111000, | |
B00000100, | |
B00000000 | |
}, { | |
B00000000, | |
B00110001, | |
B01001001, | |
B01001001, | |
B01001001, | |
B00111000, | |
B00000100, | |
B00000000 | |
}, { | |
B00000000, | |
B01100010, | |
B10010010, | |
B10010010, | |
B10010010, | |
B01110001, | |
B00001000, | |
B00000000 | |
}, { | |
B00000000, | |
B11000100, | |
B00100100, | |
B00100100, | |
B00100100, | |
B11100011, | |
B00010000, | |
B00000000 | |
}, { | |
B00000000, | |
B10001001, | |
B01001001, | |
B01001001, | |
B01001001, | |
B11000110, | |
B00100000, | |
B00000000 | |
}, { | |
B00000000, | |
B00010010, | |
B10010010, | |
B10010010, | |
B10010010, | |
B10001100, | |
B01000000, | |
B00000000 | |
}, { | |
B00000000, | |
B00100101, | |
B00100100, | |
B00100100, | |
B00100100, | |
B00011000, | |
B10000000, | |
B00000000 | |
}, { | |
B00000000, | |
B01001010, | |
B01001001, | |
B01001001, | |
B01001001, | |
B00110001, | |
B00000000, | |
B00000000 | |
}, { | |
B00000000, | |
B10010101, | |
B10010010, | |
B10010010, | |
B10010010, | |
B01100010, | |
B00000000, | |
B00000000 | |
}, { | |
B00000000, | |
B00101010, | |
B00100100, | |
B00100100, | |
B00100100, | |
B11000100, | |
B00000000, | |
B00000000 | |
}, { | |
B00000000, | |
B01010100, | |
B01001000, | |
B01001000, | |
B01001000, | |
B10001000, | |
B00000000, | |
B00000000 | |
}, { | |
B00000000, | |
B10101000, | |
B10010000, | |
B10010000, | |
B10010000, | |
B00010000, | |
B00000000, | |
B00000000 | |
}, { | |
B00000000, | |
B01010000, | |
B00100000, | |
B00100000, | |
B00100000, | |
B00100000, | |
B00000000, | |
B00000000 | |
}, { | |
B00000000, | |
B10100000, | |
B01000000, | |
B01000000, | |
B01000000, | |
B01000000, | |
B00000000, | |
B00000000 | |
}, { | |
B00000000, | |
B01000000, | |
B10000000, | |
B10000000, | |
B10000000, | |
B10000000, | |
B00000000, | |
B00000000 | |
}, { | |
B00000000, | |
B10000000, | |
B00000000, | |
B00000000, | |
B00000000, | |
B00000000, | |
B00000000, | |
B00000000 | |
}, { | |
B00000000, | |
B00000000, | |
B00000000, | |
B00000000, | |
B00000000, | |
B00000000, | |
B00000000, | |
B00000000 | |
} | |
}; | |
//Xuất ra QUY | |
int size = sizeof(IMAGES) / 8; | |
int i = 0; | |
void loop() { | |
for (int loop = 0; loop < 300; loop ++ ) { | |
drawImage(IMAGES[i]); | |
} | |
if (++i >= size) { | |
i = 0; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
shiftOut với 8 LED bằng 1 IC HC595 | |
*/ | |
//chân ST_CP của 74HC595 | |
int latchPin = 8; | |
//chân SH_CP của 74HC595 | |
int clockPin = 12; | |
//Chân DS của 74HC595 | |
int dataPin = 11; | |
const int HC595_COUNT = 2;//Số lượng 595 mắc nối tiếp | |
byte ledStatus[HC595_COUNT] = {0, 0}; | |
void setup() { | |
//Bạn BUỘC PHẢI pinMode các chân này là OUTPUT | |
pinMode(latchPin, OUTPUT); | |
pinMode(clockPin, OUTPUT); | |
pinMode(dataPin, OUTPUT); | |
} | |
void fillValueToArray(byte value) { | |
for (int i = 0; i < HC595_COUNT; i += 1) { | |
ledStatus[i] = value; | |
} | |
} | |
void shiftOutHC595(int dataPin, int clockPin, byte ledStatus[]) { | |
digitalWrite(latchPin, LOW); | |
for (int i = 0; i < HC595_COUNT; i++) { | |
shiftOut(dataPin, clockPin, MSBFIRST, ledStatus[i]); | |
} | |
digitalWrite(latchPin, HIGH); | |
} | |
void loop() { | |
fillValueToArray(0); | |
int data[] = {255, 128, 0, 0, 0, 0, 0, 0}; | |
int row[] = {1, 2, 4, 8, 16, 32, 64, 128}; | |
// int column[] = {128, 64, 32, 16, 8, 4, 2, 1}; | |
for(int i = 0 ; i< 8; i++){ | |
ledStatus[0] = ~data[i]; | |
ledStatus[1] = row[i]; | |
shiftOutHC595(dataPin, clockPin, ledStatus); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
shiftOut với 8 LED bằng 1 IC HC595 | |
*/ | |
//chân ST_CP của 74HC595 | |
int latchPin = 8; | |
//chân SH_CP của 74HC595 | |
int clockPin = 12; | |
//Chân DS của 74HC595 | |
int dataPin = 11; | |
const int HC595_COUNT = 2;//Số lượng 595 mắc nối tiếp | |
byte ledStatus[HC595_COUNT] = {0, 0}; | |
void setup() { | |
//Bạn BUỘC PHẢI pinMode các chân này là OUTPUT | |
pinMode(latchPin, OUTPUT); | |
pinMode(clockPin, OUTPUT); | |
pinMode(dataPin, OUTPUT); | |
} | |
void fillValueToArray(byte value) { | |
for (int i = 0; i < HC595_COUNT; i += 1) { | |
ledStatus[i] = value; | |
} | |
} | |
void shiftOutHC595(int dataPin, int clockPin, byte ledStatus[]) { | |
digitalWrite(latchPin, LOW); | |
for (int i = 0; i < HC595_COUNT; i++) { | |
shiftOut(dataPin, clockPin, MSBFIRST, ledStatus[i]); | |
} | |
digitalWrite(latchPin, HIGH); | |
} | |
void loop() { | |
fillValueToArray(0); | |
int data[] = { | |
B00000000, | |
B01100110, | |
B10011001, | |
B10000001, | |
B01000010, | |
B00100100, | |
B00011000, | |
B00000000 | |
}; | |
int row[] = {1, 2, 4, 8, 16, 32, 64, 128}; | |
// int column[] = {128, 64, 32, 16, 8, 4, 2, 1}; | |
for(int i = 0 ; i< 8; i++){ | |
ledStatus[0] = ~data[i]; | |
ledStatus[1] = row[i]; | |
shiftOutHC595(dataPin, clockPin, ledStatus); | |
delay(100); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*https://randomnerdtutorials.com/security-access-using-mfrc522-rfid-reader-with-arduino/ | |
* Created by @tranphuquy19 on 04/11/2019 | |
* SPI là chuẩn giao tiếp nhanh. Ưu điểm là có thể truyền và nhận đồng thời. Thư viện SPI.h (có sẵn) | |
* Các chân theo chuẩn SPI quy định trong Arduino Uno | |
* 13 -> SCK | |
* 12 -> MISO | |
* 11 -> MOSI | |
* 10 -> SS | |
* | |
* Thư viện cho module RFID MFRC522 không có sẵn trong Arduino IDE. Cài đặt: Tools -> Manage Libraries -> Gõ "MFRC522" -> Tìm và nhấn Install | |
* | |
* ----- Chương trình nhận dạng thẻ theo UID ------ | |
* UID: 1A D4 51 02 | |
*/ | |
#include <SPI.h> | |
#include <MFRC522.h> | |
#define SS_PIN 10 | |
#define RST_PIN 9 | |
#define RED_LED 7 | |
#define BLUE_LED 6 | |
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance. | |
void setup() | |
{ | |
pinMode(RED_LED, OUTPUT); | |
pinMode(BLUE_LED, OUTPUT); | |
Serial.begin(9600); // Initiate a serial communication | |
SPI.begin(); // Initiate SPI bus | |
mfrc522.PCD_Init(); // Initiate MFRC522 | |
Serial.println("Approximate your card to the reader..."); | |
Serial.println(); | |
} | |
void ledToggle(int ledIndex){ | |
digitalWrite(ledIndex, HIGH); | |
delay(1000); | |
digitalWrite(ledIndex, LOW); | |
delay(1000); | |
} | |
void loop() | |
{ | |
// Look for new cards | |
if ( ! mfrc522.PICC_IsNewCardPresent()) | |
{ | |
return; | |
} | |
// Select one of the cards | |
if ( ! mfrc522.PICC_ReadCardSerial()) | |
{ | |
return; | |
} | |
//Show UID on serial monitor | |
Serial.print("UID tag :"); | |
String content= ""; | |
byte letter; | |
for (byte i = 0; i < mfrc522.uid.size; i++) | |
{ | |
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); | |
Serial.print(mfrc522.uid.uidByte[i], HEX); | |
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ")); | |
content.concat(String(mfrc522.uid.uidByte[i], HEX)); | |
} | |
Serial.println(); | |
Serial.print("Message : "); | |
content.toUpperCase(); | |
if (content.substring(1) == "1A D4 51 02") //Thay đổi UID của bạn tại đây | |
{ | |
Serial.println("Authorized access"); | |
Serial.println(); | |
ledToggle(BLUE_LED); | |
} | |
else { | |
Serial.println(" Access denied"); | |
ledToggle(RED_LED); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <ESP8266WiFi.h> | |
//Sửa tên Wifi và mật khẩu của mình | |
const char *ssid = "Quy"; | |
const char *password = "1123112323"; | |
// Set web server port number to 80 | |
WiFiServer server(80); | |
// Variable to store the HTTP request | |
String header; | |
// Auxiliar variables to store the current output state | |
String output5State = "off"; | |
String output4State = "off"; | |
// Assign output variables to GPIO pins | |
const int output5 = 5; | |
const int output4 = 4; | |
// Current time | |
unsigned long currentTime = millis(); | |
// Previous time | |
unsigned long previousTime = 0; | |
// Define timeout time in milliseconds (example: 2000ms = 2s) | |
const long timeoutTime = 2000; | |
void setup() | |
{ | |
Serial.begin(115200); | |
// Initialize the output variables as outputs | |
pinMode(output5, OUTPUT); | |
pinMode(output4, OUTPUT); | |
// Set outputs to LOW | |
// digitalWrite(output5, LOW); | |
// digitalWrite(output4, LOW); | |
// Connect to Wi-Fi network with SSID and password | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
delay(500); | |
Serial.print("."); | |
} | |
// Print local IP address and start web server | |
Serial.println(""); | |
Serial.println("WiFi connected."); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
server.begin(); | |
} | |
void loop() | |
{ | |
WiFiClient client = server.available(); // Listen for incoming clients | |
if (client) | |
{ // If a new client connects, | |
Serial.println("New Client."); // print a message out in the serial port | |
String currentLine = ""; // make a String to hold incoming data from the client | |
currentTime = millis(); | |
previousTime = currentTime; | |
while (client.connected() && currentTime - previousTime <= timeoutTime) | |
{ // loop while the client's connected | |
currentTime = millis(); | |
if (client.available()) | |
{ // if there's bytes to read from the client, | |
char c = client.read(); // read a byte, then | |
Serial.write(c); // print it out the serial monitor | |
header += c; | |
if (c == '\n') | |
{ // if the byte is a newline character | |
// if the current line is blank, you got two newline characters in a row. | |
// that's the end of the client HTTP request, so send a response: | |
if (currentLine.length() == 0) | |
{ | |
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) | |
// and a content-type so the client knows what's coming, then a blank line: | |
client.println("HTTP/1.1 200 OK"); | |
client.println("Content-type:text/html"); | |
client.println("Connection: close"); | |
client.println(); | |
// turns the GPIOs on and off | |
if (header.indexOf("GET /5/on") >= 0) | |
{ | |
Serial.println("GPIO 5 on"); | |
output5State = "on"; | |
digitalWrite(output5, HIGH); | |
} | |
else if (header.indexOf("GET /5/off") >= 0) | |
{ | |
Serial.println("GPIO 5 off"); | |
output5State = "off"; | |
digitalWrite(output5, LOW); | |
} | |
else if (header.indexOf("GET /4/on") >= 0) | |
{ | |
Serial.println("GPIO 4 on"); | |
output4State = "on"; | |
digitalWrite(output4, HIGH); | |
} | |
else if (header.indexOf("GET /4/off") >= 0) | |
{ | |
Serial.println("GPIO 4 off"); | |
output4State = "off"; | |
digitalWrite(output4, LOW); | |
} | |
// Display the HTML web page | |
client.println("<!DOCTYPE html><html>"); | |
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"); | |
client.println("<link rel=\"icon\" href=\"data:,\">"); | |
// CSS to style the on/off buttons | |
// Feel free to change the background-color and font-size attributes to fit your preferences | |
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}"); | |
client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;"); | |
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}"); | |
client.println(".button2 {background-color: #77878A;}</style></head>"); | |
// Web Page Heading | |
client.println("<body><h1>ESP8266 Web Server</h1>"); | |
// Display current state, and ON/OFF buttons for GPIO 5 | |
client.println("<p>GPIO 5 - State " + output5State + "</p>"); | |
// If the output5State is off, it displays the ON button | |
if (output5State == "off") | |
{ | |
client.println("<p><a href=\"/5/on\"><button class=\"button\">ON</button></a></p>"); | |
} | |
else | |
{ | |
client.println("<p><a href=\"/5/off\"><button class=\"button button2\">OFF</button></a></p>"); | |
} | |
// Display current state, and ON/OFF buttons for GPIO 4 | |
client.println("<p>GPIO 4 - State " + output4State + "</p>"); | |
// If the output4State is off, it displays the ON button | |
if (output4State == "off") | |
{ | |
client.println("<p><a href=\"/4/on\"><button class=\"button\">ON</button></a></p>"); | |
} | |
else | |
{ | |
client.println("<p><a href=\"/4/off\"><button class=\"button button2\">OFF</button></a></p>"); | |
} | |
client.println("</body></html>"); | |
// The HTTP response ends with another blank line | |
client.println(); | |
// Break out of the while loop | |
break; | |
} | |
else | |
{ // if you got a newline, then clear currentLine | |
currentLine = ""; | |
} | |
} | |
else if (c != '\r') | |
{ // if you got anything else but a carriage return character, | |
currentLine += c; // add it to the end of the currentLine | |
} | |
} | |
} | |
// Clear the header variable | |
header = ""; | |
// Close the connection | |
client.stop(); | |
Serial.println("Client disconnected."); | |
Serial.println(""); | |
} | |
} |
Author
tranphuquy19
commented
Sep 26, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment