Embeded สัปดาห์ที่ 1 Arduino - Basic I/O (Digital Input/Output, Shields, Sensors)
วันก่อนวันแรกที่เรียนวิชา Embedded พอขึ้นไปที่ห้องเรียนนึกว่าจะได้เรียน อาจารย์ให้รื้อแผงสายไฟ แล้วย้ายโต๊ะ ระหว่างนั้นอาจารย์ก็พูดเกี่ยวการเรียนเมื่อปีที่ผ่านมา แล้วก็แจกกล่องอุปกรณ์การเรียน ในกล่องมี NodeMCU led7seg Max7219 เซ็นเซอร์วัดอุณหภูมิ สายไฟ บลาๆ อีกหลายอย่าง กว่าจะเสร็จก็หมดเวลาเรียน วันนั้นก็ไม่ได้เรียน มาย้ายโต๊ะกับเอาของเฉยๆ
วันเรียนจริงๆ
มาถึงวันที่เรียนจริงๆ โดยวันนี้อาจารย์นัดมากินข้าวก่อนโดยอาหารที่จารเลี้ยงคือราดน้า แต่ผมดันมาช้าไปสักหน่อย หมี่กรอบที่เป็นของชอบเลยเกือบจะหมดแล้ว พอมาถึงอาจารย์ก็ให้จัดการหาอะไรกินเอง ซึ่งมีราดหน้าที่แยกทุกอยากให้เราปรุงได้ตามใจชอบ มีน้ำที่แช่เย็นในถัวโฟม มีขนมกินเล่น จริงๆแล้วที่ว่ามาทั้งหมดเป็นของค่ายอะไรซักอย่าง ซึ่งเราไปกินของเขารึปล่าวอันนี้ก็ไม่แน่ใจ หลังจากที่คนอื่นกินเสดหมดแล้วแล้วอาจารย์ก็ให้ไปที่ห้อง แต่ผมดันตักมาเยอะเกิน ตักผักมาด้วยทั้งที่รู้ว่าตัวเองก็ไม่กิน เส้นใหญ่ก็รู้สึกจะเปริ้ยวนิดหน่อย เป็นไงหละกินไปหมด แอบทิ้งในถุงขยะ พอมาถึงห้องเรียนก็เริ่มจัดแจงที่นั่งจัดข้าวของให้เรียบร้อย แต่ยังไม่เห็นอาจารย์มาสอน สักพักกลุ่มเรียนในเฟสก็แจ้งเตือน จารย์โพสใบงานบอกให้ลองทำเองไปก่อน กระผมก็คนไม่เคยทำมาก่อนก็รออาจารย์สิครับ(ทำไม่เป็นจริงๆ) อีกซัก 10 นาทีอาจารย์ก็เข้ามาสอน อาจารย์สอนเร็วมว้ากกก ประหนึ่งเรานักศึกษาคือเทพมาจุติ แต่ดีที่ผมนั่งหน้าเลยพอทำตามอาจารย์ทันบ้างไม่ทันบ้าง อาจารย์สอนการใช้งาน NodeMCU ผ่าน Arduino IDE โดยเริ่มจากให้ตัวโปรแกรมรู้จักบอร์ดก่อน ลงนู้นนี่นั้นให้เรียบร้อย จากนั้นสิ่งแรกที่อาจารย์ให้ทำคือ ทดสอบไฟกระพริบก่อน จากนั้นก็ลองทำ Web Server แล้วก็ Access point แล้วก็แสดง ตัวเลขออก ledMax7219 ขั้นแรกให้นับลงก่อน ขั้นต่อมาก็อ่านอุณหภูมิกับความชื้นจากเซนเซอร์แล้วแสดงออก Max7219 จบการเรียนการสอนของวันแรกไป หลังจากเรียนเสร็จก็แจกจ่ายราดหน้าแต่ดันฝนดันตก กว่าจะได้กลับ ก็ 17.00 น.
สำหรับงานที่ทำวันนี้มี 5 ข้อ ดังนี้
ข้อ 1. ต้องการแสดงข้อความ
Code :
#include <SPI.h>
#include "LedMatrix.h"
#define NUMBER_OF_DEVICES 1
#define CS_PIN D8
LedMatrix ledMatrix = LedMatrix(NUMBER_OF_DEVICES, CS_PIN);
void setup() {
Serial.begin(115200); // For debugging output
ledMatrix.init();
ledMatrix.setIntensity(4); // range is 0-15
ledMatrix.clear();
ledMatrix.commit();
}
void loop() {
// ledMatrix.clear();
// ledMatrix.commit(); // commit send buffer to the displays
// delay(1000);
Send2MAX7129(7, 16, 0);
Send2MAX7129(6, 17, 0);
Send2MAX7129(5, 18, 0);
Send2MAX7129(4, 19, 0);
Send2MAX7129(3, 20, 0);
Send2MAX7129(2, 21, 0);
Send2MAX7129(1, 22, 0);
delay(1000);
}
//===================================================
//===================================================
void Send2MAX7129(byte SegPosition, byte Value, bool dotDigit)
{ const static byte charTable [] =
{ B01111110, B00110000, B01101101, B01111001,
B00110011, B01011011, B01011111, B01110000,
B01111111, B01111011, B01110111, B00011111,
B00001101, B00111101, B01001111, B01000111,
B01110111, B00000101, B00111101, B00011100,
B00010000, B00010101, B00011101
};
Value = charTable[Value];
if (dotDigit == 1) Value |= 0x80;
digitalWrite(CS_PIN, LOW);
SPI.transfer (SegPosition);
SPI.transfer (Value);
digitalWrite (CS_PIN, HIGH);
Serial.print(Value);
}
//===================================================
// ##SegPosition >> 87654321 ##SegData >> tabcdefg
//===================================================
void SendData2MAX7129(byte SegPosition, byte SegData)
{ digitalWrite(CS_PIN, LOW);
SPI.transfer (SegPosition);
SPI.transfer (SegData);
digitalWrite (CS_PIN, HIGH);
}
สำหรับงานที่ทำวันนี้มี 5 ข้อ ดังนี้
ข้อ 1. ต้องการแสดงข้อความ
Code :
#include <SPI.h>
#include "LedMatrix.h"
#define NUMBER_OF_DEVICES 1
#define CS_PIN D8
LedMatrix ledMatrix = LedMatrix(NUMBER_OF_DEVICES, CS_PIN);
void setup() {
Serial.begin(115200); // For debugging output
ledMatrix.init();
ledMatrix.setIntensity(4); // range is 0-15
ledMatrix.clear();
ledMatrix.commit();
}
void loop() {
// ledMatrix.clear();
// ledMatrix.commit(); // commit send buffer to the displays
// delay(1000);
Send2MAX7129(7, 16, 0);
Send2MAX7129(6, 17, 0);
Send2MAX7129(5, 18, 0);
Send2MAX7129(4, 19, 0);
Send2MAX7129(3, 20, 0);
Send2MAX7129(2, 21, 0);
Send2MAX7129(1, 22, 0);
delay(1000);
}
//===================================================
//===================================================
void Send2MAX7129(byte SegPosition, byte Value, bool dotDigit)
{ const static byte charTable [] =
{ B01111110, B00110000, B01101101, B01111001,
B00110011, B01011011, B01011111, B01110000,
B01111111, B01111011, B01110111, B00011111,
B00001101, B00111101, B01001111, B01000111,
B01110111, B00000101, B00111101, B00011100,
B00010000, B00010101, B00011101
};
Value = charTable[Value];
if (dotDigit == 1) Value |= 0x80;
digitalWrite(CS_PIN, LOW);
SPI.transfer (SegPosition);
SPI.transfer (Value);
digitalWrite (CS_PIN, HIGH);
Serial.print(Value);
}
//===================================================
// ##SegPosition >> 87654321 ##SegData >> tabcdefg
//===================================================
void SendData2MAX7129(byte SegPosition, byte SegData)
{ digitalWrite(CS_PIN, LOW);
SPI.transfer (SegPosition);
SPI.transfer (SegData);
digitalWrite (CS_PIN, HIGH);
}
ข้อ 2. ใช้ Arduino IDE ในการพัฒนาโปรแกรมให้อ่านอุณหภูมิจากเซ็นเซอร์แล้วแสดงผลที่ MAX7219_7Segment Display โดยมีรูปแบบดังรูป
Code :
#include <SPI.h>
#include "LedMatrix.h"
#include <Wire.h>
#include "ClosedCube_HDC1080.h"
ClosedCube_HDC1080 hdc1080;
#define NUMBER_OF_DEVICES 1
#define CS_PIN D8
LedMatrix ledMatrix = LedMatrix(NUMBER_OF_DEVICES, CS_PIN);
void setup() {
Serial.begin(115200); // For debugging output
ledMatrix.init();
ledMatrix.setIntensity(8); // range is 0-15
ledMatrix.clear();
ledMatrix.commit(); // commit send buffer to the displays
Serial.begin(9600);
Serial.println("ClosedCube HDC1080 Arduino Test");
// Heater off, 14 bit Temperature and Humidity Measurement Resolution
hdc1080.begin(0x40);
Serial.print("Manufacturer ID=0x");
Serial.println(hdc1080.readManufacturerId(), HEX); // 0x5449 ID of Texas Instruments
Serial.print("Device ID=0x");
Serial.println(hdc1080.readDeviceId(), HEX); // 0x1050 ID of the device
}
void loop() {
{
Serial.print("T=");
Serial.print(hdc1080.readTemperature());
Serial.print("C, RH=");
Serial.print(hdc1080.readHumidity());
Serial.println("%");
delay(300);
int xx = (int)(hdc1080.readTemperature() * 10);
Send2MAX7129(8,xx/100, 0); xx = xx %100;
Send2MAX7129(7,xx/10, 1); xx = xx %10;
Send2MAX7129(6,xx, 0);
Send2MAX7129(5,16, 0);
int yy = (int)(hdc1080.readHumidity() * 10);
Send2MAX7129(4,yy/100, 0); yy = yy % 100;
Send2MAX7129(3,yy/10, 1); yy = yy %10;
Send2MAX7129(2,yy, 0);
Send2MAX7129(1,17, 0);
delay(300);
}
}
//===================================================
//===================================================
void Send2MAX7129(byte SegPosition, byte Value, bool dotDigit)
{ const static byte charTable [] ={ B01111110, B00110000, B01101101, B01111001,
B00110011, B01011011, B01011111, B01110000,
B01111111, B01111011, B01110111, B00011111,
B00001101, B00111101, B01001111, B01000111,
B00001101, B00010111}; //
Value = charTable[Value];
if (dotDigit == 1) Value |= 0x80;
digitalWrite(CS_PIN, LOW);
SPI.transfer (SegPosition);
SPI.transfer (Value);
digitalWrite (CS_PIN, HIGH);
Serial.print(Value);
}
//===================================================
// ##SegPosition >> 87654321 ##SegData >> tabcdefg
//===================================================
void SendData2MAX7129(byte SegPosition, byte SegData)
{ digitalWrite(CS_PIN, LOW);
SPI.transfer (SegPosition);
SPI.transfer (SegData);
digitalWrite (CS_PIN, HIGH);
}
ข้อ 3.จากการพัฒนาโปรแกรมโดยใช้ Lua Scrip ให้ใช้ NodeMCU อ่านค่าจาก VR นําค่าที่ได้มาแสดง เป็น แรงดันที่อ่านเข้ามา ด้วยทศนิยม 4 ตําแหน่ง
Analog Input = [0 – 3.3]
Analog Input = [0 – 3.3]
Digital Read = [0 - 1024]
Digital Display = [0.0000 – 3.300]
Code :
Code :
-- Blink using timer alarm --
timerId = 0
dly = 500
ledPin = 1
gpio.mode(ledPin,gpio.OUTPUT)
ledState = 0
tmr.alarm( timerId, dly, 1, function()
ledState = 1 - ledState;
gpio.write(ledPin, ledState);
print(string.format("adc = %.4f", (adc.read(0)/1023)*3.3))
end)
ข้อ 4. จากการพัฒนาโปรแกรมโดยใช้ Lua Scrip ให้ใช้ NodeMCU ต่อกับ DS1820 แล้วอ่านค่าและ แสดงอุณหภูมิ 4 ตําแหน่ง พร้อมแสดงออกที่ MAX7219 Display Board
Code :
ข้อ 4. จากการพัฒนาโปรแกรมโดยใช้ Lua Scrip ให้ใช้ NodeMCU ต่อกับ DS1820 แล้วอ่านค่าและ แสดงอุณหภูมิ 4 ตําแหน่ง พร้อมแสดงออกที่ MAX7219 Display Board
Code :
pin = 4
function getTemp() -- 18b20 Example
ow.setup(pin)
count = 0
repeat
count = count + 1
addr = ow.reset_search(pin)
addr = ow.search(pin)
tmr.wdclr()
until (addr ~= nil) or (count > 100)
if addr == nil then
print("No more addresses.")
else
--print(addr:byte(1,8))
crc = ow.crc8(string.sub(addr,1,7))
if crc == addr:byte(8) then
if (addr:byte(1) == 0x10) or (addr:byte(1) == 0x28) then
--print("Device is a DS18S20 family device.")
ow.reset(pin)
ow.select(pin, addr)
ow.write(pin, 0x44, 1)
tmr.delay(10000)
present = ow.reset(pin)
ow.select(pin, addr)
ow.write(pin,0xBE,1)
--print("P = "..present)
data = nil
data = string.char(ow.read(pin))
for i = 1, 8 do
data = data .. string.char(ow.read(pin))
end
--print(data:byte(1,9))
crc = ow.crc8(string.sub(data,1,8))
--print("CRC= "..crc)
if crc == data:byte(9) then
t = ((data:byte(1)+data:byte(2)*256)*625)/10000
print("Temperature = "..t.." Centigrade")
end
tmr.wdclr()
else
print("Device family is not recognized.")
end
else
print("CRC is not valid!")
end
end
--print()
end
-- MAIN
tmr.alarm(1, 1000, 1, function() getTemp() end)
Comments
Post a Comment