Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat dengan menggunakan arduino untuk menginputkan data diri seperti data umur, jenis kelamin, tinggi badan dan data sensor. alat ini dilengkapi dengan datalogger berupa SD Card dengan input data menggunakan keypad matrix 4x4. untuk lebih jelasnya berikut adalah skema dan programnya.
a. Arduino Uno
b. Keypad Matrix 4x4
c. Lcd 20x4 + I2C
d. SD Card Module
e. Program Arduino IDE
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <SD.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x3F,20,4);
//mosi - 11
//miso - 12
//clk - 13
//cs - 4
long xx;
int adcsensor;
File myFile;
int x;
int y;
int ket;
int fix;
float rasio1;
float rasio2;
long umurx;
long umur;
long beratx;
long berat;
long tinggix;
long tinggi;
int klm;
int dataadc;
int dataadc2;
float tegangan;
float p;
float v;
float akar;
float q;
float tegangan2;
float p2;
float v2;
float akar2;
float q2;
float hasilku;
float hasilku2;
float fvc;
float fvcx;
float fev1;
float fev1x;
unsigned long simpan[10];
int zz;
char customKey;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {2,3,10,5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6,7,8,9}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
lcd.begin();
lcd.noCursor();
lcd.clear();
}
void loop() {
lcd.setCursor(0,0);
lcd.print("Jenis Kelamin= ");
if(klm == 1){
lcd.print("L");
}
if(klm == 2){
lcd.print("P");
}
lcd.setCursor(0,1);
lcd.print("Umur= ");
lcd.print(umur);
lcd.setCursor(0,2);
lcd.print("Tinggi= ");
lcd.print(tinggi);
customKey = customKeypad.getKey();
if(customKey == 'D'){
lcd.clear();
delay(1000);
setting1();
setting2();
setting4();
}
if(customKey == 'C'){
lcd.clear();
delay(1000);
sensing1();
berhenti();
sensing2();
berhenti();
lcd.clear();
delay(1000);
hasil();
savesd();
}
}
void berhenti(){
customKey = customKeypad.getKey();
if(customKey == '*'){
lcd.clear();
delay(1000);
return;
}
berhenti();
}
void sensing1(){
dataadc = analogRead(0);
tegangan = dataadc * (5.0 / 1023.0);
p = (tegangan-0.4)/0.225;
v = ((2*p/1.2)/0.625);
akar = sqrt(v);
q = (0.0005*akar);
fvc = q;
lcd.setCursor(0,0);
lcd.print("FVC=");
lcd.print(fvc);
lcd.print(" ");
delay(1000);
simpan[zz] = fvc;
zz++;
if(zz > 5){
hasilku = simpan[1] + simpan[2] + simpan[3] + simpan[4] + simpan[5];
zz = 0;
return;
}
sensing1();
}
void sensing2(){
dataadc2 = analogRead(0);
tegangan2 = dataadc2 * (5.0 / 1023.0);
p2 = (tegangan2-0.4)/0.225;
v2 = ((2*p2/1.2)/0.625);
akar2 = sqrt(v2);
q2 = (0.0005*akar2);
fev1 = q2;
lcd.setCursor(0,0);
lcd.print("FEV=");
lcd.print(fev1);
lcd.print(" ");
delay(1000);
hasilku2 = fev1;
return;
}
void setting1(){
while( 1 )
{
customKey = customKeypad.getKey();
lcd.setCursor(0,0);
lcd.print("-> Jenis Kelamin= ");
if(customKey == 'A'){
lcd.print("L");
klm = 1;
}
if(customKey == 'B'){
lcd.print("P");
klm = 2;
}
if(customKey == 'D'){
lcd.clear();
delay(1000);
return;
}
lcd.setCursor(0,1);
lcd.print("Umur= ");
lcd.print(umur);
lcd.setCursor(0,2);
lcd.print("Tinggi= ");
lcd.print(tinggi);
}
setting1();
}
void setting2(){
while( 1 )
{
lcd.setCursor(0,0);
lcd.print("Jenis Kelamin= ");
if(klm == 1){
lcd.print("L");
}
if(klm == 2){
lcd.print("P");
}
customKey = customKeypad.getKey();
lcd.setCursor(0,1);
lcd.print("-> Umur= ");
if(customKey >= '0' && customKey <= '9')
{
umurx = umurx * 10 + (customKey - '0');
lcd.print(umurx);
}
if(customKey == 'D'){
lcd.clear();
delay(1000);
umur = umurx;
return;
}
lcd.setCursor(0,2);
lcd.print("Tinggi= ");
lcd.print(tinggi);
}
setting2();
}
void setting4(){
while( 1 )
{
lcd.setCursor(0,0);
lcd.print("Jenis Kelamin= ");
if(klm == 1){
lcd.print("L");
}
if(klm == 2){
lcd.print("P");
}
lcd.setCursor(0,1);
lcd.print("Umur= ");
lcd.print(umur);
customKey = customKeypad.getKey();
lcd.setCursor(0,2);
lcd.print("-> Tinggi= ");
if(customKey >= '0' && customKey <= '9')
{
tinggix = tinggix * 10 + (customKey - '0');
lcd.print(tinggix);
}
if(customKey == 'D'){
lcd.clear();
delay(1000);
tinggi = tinggix;
return;
}
}
setting4();
}
void hasil(){
if(klm == 1){
fvcx = -5.44018 + 0.06114 * umur + 0.04849 * tinggi + 1.62398 * 1 - 0.07768 * umur;
fev1x = -4.10074 + 0.04864 * umur + 0.03947 * tinggi + 1.4969 * 1 - 0.07433 * umur;
rasio1 = fev1 / fvc;
rasio2 = 96.63286 - 0.36507 * umur;
lcd.setCursor(0,0);
lcd.print("FVC=");
lcd.print(fvc);
lcd.print(" / ");
lcd.print(fvcx);
lcd.setCursor(0,1);
lcd.print("FEV1=");
lcd.print(fev1);
lcd.print(" / ");
lcd.print(fev1x);
lcd.setCursor(0,2);
lcd.print("Rasio=");
lcd.print(rasio1);
lcd.print(" / ");
lcd.print(rasio2);
if((rasio1 >= 0.75 * rasio2) && (fvc >= 0.8 * fvcx)){
//normal
lcd.setCursor(0,3);
lcd.print("NORMAL ");
ket = 1;
}
if((rasio1 >= 0.75 * rasio2) && (fvc < 0.8 * fvcx)){
//restriktif
lcd.setCursor(0,3);
lcd.print("RESTRIKTIF ");
ket = 2;
}
if((rasio1 < 0.75 * rasio2) && (fvc > 0.8 * fvcx) && (fev1 < 0.95 * fev1x)){
//gangguan obstrukstif
lcd.setCursor(0,3);
lcd.print("OBSTRUKTIF ");
ket = 3;
}
}
if(klm == 2){
fvcx = -3.37068 + 0.02824 * umur + 0.03583 * tinggi + 1.00051 * 1 - 0.04546 * umur;
fev1x = -2.39380 + 0.01684 * umur + 0.02935 * tinggi + 0.85319 * 1 - 0.03894 * umur;
rasio1 = fev1 / fvc;
rasio2 = 97.89444 - 0.31804 * umur;
lcd.setCursor(0,0);
lcd.print("FVC=");
lcd.print(fvc);
lcd.print(" / ");
lcd.print(fvcx);
lcd.setCursor(0,1);
lcd.print("FEV1=");
lcd.print(fev1);
lcd.print(" / ");
lcd.print(fev1x);
lcd.setCursor(0,2);
lcd.print("Rasio=");
lcd.print(rasio1);
lcd.print(" / ");
lcd.print(rasio2);
if((rasio1 >= 0.75 * rasio2) && (fvc >= 0.8 * fvcx)){
//normal
lcd.setCursor(0,3);
lcd.print("NORMAL ");
ket = 1;
}
if((rasio1 >= 0.75 * rasio2) && (fvc < 0.8 * fvcx)){
//restriktif
lcd.setCursor(0,3);
lcd.print("RESTRIKTIF ");
ket = 2;
}
if((rasio1 < 0.75 * rasio2) && (fvc > 0.8 * fvcx) && (fev1 < 0.95 * fev1x)){
//gangguan obstrukstif
lcd.setCursor(0,3);
lcd.print("OBSTRUKTIF ");
ket = 3;
}
}
customKey = customKeypad.getKey();
if(customKey == '*'){
lcd.clear();
delay(1000);
return;
}
hasil();
}
void savesd(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("saving.....");
delay(3000);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
myFile.print("Jenis Kelamin= ");
if(klm == 1){
myFile.println("L");
}
if(klm == 2){
myFile.println("P");
}
myFile.print("Umur= ");
myFile.println(umur);
myFile.print("Tinggi= ");
myFile.println(tinggi);
myFile.print("Kondisi= ");
if(ket == 1){
myFile.println("NORMAL");
}
if(ket == 2){
myFile.println("RESTRIKTIF");
}
if(ket == 3){
myFile.println("OBSTRUKTIF");
}
myFile.println(" ");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
lcd.setCursor(0,0);
lcd.print("DONE..");
delay(1000);
lcd.clear();
}
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <SD.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x3F,20,4);
//mosi - 11
//miso - 12
//clk - 13
//cs - 4
long xx;
int adcsensor;
File myFile;
int x;
int y;
int ket;
int fix;
float rasio1;
float rasio2;
long umurx;
long umur;
long beratx;
long berat;
long tinggix;
long tinggi;
int klm;
int dataadc;
int dataadc2;
float tegangan;
float p;
float v;
float akar;
float q;
float tegangan2;
float p2;
float v2;
float akar2;
float q2;
float hasilku;
float hasilku2;
float fvc;
float fvcx;
float fev1;
float fev1x;
unsigned long simpan[10];
int zz;
char customKey;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {2,3,10,5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6,7,8,9}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
lcd.begin();
lcd.noCursor();
lcd.clear();
}
void loop() {
lcd.setCursor(0,0);
lcd.print("Jenis Kelamin= ");
if(klm == 1){
lcd.print("L");
}
if(klm == 2){
lcd.print("P");
}
lcd.setCursor(0,1);
lcd.print("Umur= ");
lcd.print(umur);
lcd.setCursor(0,2);
lcd.print("Tinggi= ");
lcd.print(tinggi);
customKey = customKeypad.getKey();
if(customKey == 'D'){
lcd.clear();
delay(1000);
setting1();
setting2();
setting4();
}
if(customKey == 'C'){
lcd.clear();
delay(1000);
sensing1();
berhenti();
sensing2();
berhenti();
lcd.clear();
delay(1000);
hasil();
savesd();
}
}
void berhenti(){
customKey = customKeypad.getKey();
if(customKey == '*'){
lcd.clear();
delay(1000);
return;
}
berhenti();
}
void sensing1(){
dataadc = analogRead(0);
tegangan = dataadc * (5.0 / 1023.0);
p = (tegangan-0.4)/0.225;
v = ((2*p/1.2)/0.625);
akar = sqrt(v);
q = (0.0005*akar);
fvc = q;
lcd.setCursor(0,0);
lcd.print("FVC=");
lcd.print(fvc);
lcd.print(" ");
delay(1000);
simpan[zz] = fvc;
zz++;
if(zz > 5){
hasilku = simpan[1] + simpan[2] + simpan[3] + simpan[4] + simpan[5];
zz = 0;
return;
}
sensing1();
}
void sensing2(){
dataadc2 = analogRead(0);
tegangan2 = dataadc2 * (5.0 / 1023.0);
p2 = (tegangan2-0.4)/0.225;
v2 = ((2*p2/1.2)/0.625);
akar2 = sqrt(v2);
q2 = (0.0005*akar2);
fev1 = q2;
lcd.setCursor(0,0);
lcd.print("FEV=");
lcd.print(fev1);
lcd.print(" ");
delay(1000);
hasilku2 = fev1;
return;
}
void setting1(){
while( 1 )
{
customKey = customKeypad.getKey();
lcd.setCursor(0,0);
lcd.print("-> Jenis Kelamin= ");
if(customKey == 'A'){
lcd.print("L");
klm = 1;
}
if(customKey == 'B'){
lcd.print("P");
klm = 2;
}
if(customKey == 'D'){
lcd.clear();
delay(1000);
return;
}
lcd.setCursor(0,1);
lcd.print("Umur= ");
lcd.print(umur);
lcd.setCursor(0,2);
lcd.print("Tinggi= ");
lcd.print(tinggi);
}
setting1();
}
void setting2(){
while( 1 )
{
lcd.setCursor(0,0);
lcd.print("Jenis Kelamin= ");
if(klm == 1){
lcd.print("L");
}
if(klm == 2){
lcd.print("P");
}
customKey = customKeypad.getKey();
lcd.setCursor(0,1);
lcd.print("-> Umur= ");
if(customKey >= '0' && customKey <= '9')
{
umurx = umurx * 10 + (customKey - '0');
lcd.print(umurx);
}
if(customKey == 'D'){
lcd.clear();
delay(1000);
umur = umurx;
return;
}
lcd.setCursor(0,2);
lcd.print("Tinggi= ");
lcd.print(tinggi);
}
setting2();
}
void setting4(){
while( 1 )
{
lcd.setCursor(0,0);
lcd.print("Jenis Kelamin= ");
if(klm == 1){
lcd.print("L");
}
if(klm == 2){
lcd.print("P");
}
lcd.setCursor(0,1);
lcd.print("Umur= ");
lcd.print(umur);
customKey = customKeypad.getKey();
lcd.setCursor(0,2);
lcd.print("-> Tinggi= ");
if(customKey >= '0' && customKey <= '9')
{
tinggix = tinggix * 10 + (customKey - '0');
lcd.print(tinggix);
}
if(customKey == 'D'){
lcd.clear();
delay(1000);
tinggi = tinggix;
return;
}
}
setting4();
}
void hasil(){
if(klm == 1){
fvcx = -5.44018 + 0.06114 * umur + 0.04849 * tinggi + 1.62398 * 1 - 0.07768 * umur;
fev1x = -4.10074 + 0.04864 * umur + 0.03947 * tinggi + 1.4969 * 1 - 0.07433 * umur;
rasio1 = fev1 / fvc;
rasio2 = 96.63286 - 0.36507 * umur;
lcd.setCursor(0,0);
lcd.print("FVC=");
lcd.print(fvc);
lcd.print(" / ");
lcd.print(fvcx);
lcd.setCursor(0,1);
lcd.print("FEV1=");
lcd.print(fev1);
lcd.print(" / ");
lcd.print(fev1x);
lcd.setCursor(0,2);
lcd.print("Rasio=");
lcd.print(rasio1);
lcd.print(" / ");
lcd.print(rasio2);
if((rasio1 >= 0.75 * rasio2) && (fvc >= 0.8 * fvcx)){
//normal
lcd.setCursor(0,3);
lcd.print("NORMAL ");
ket = 1;
}
if((rasio1 >= 0.75 * rasio2) && (fvc < 0.8 * fvcx)){
//restriktif
lcd.setCursor(0,3);
lcd.print("RESTRIKTIF ");
ket = 2;
}
if((rasio1 < 0.75 * rasio2) && (fvc > 0.8 * fvcx) && (fev1 < 0.95 * fev1x)){
//gangguan obstrukstif
lcd.setCursor(0,3);
lcd.print("OBSTRUKTIF ");
ket = 3;
}
}
if(klm == 2){
fvcx = -3.37068 + 0.02824 * umur + 0.03583 * tinggi + 1.00051 * 1 - 0.04546 * umur;
fev1x = -2.39380 + 0.01684 * umur + 0.02935 * tinggi + 0.85319 * 1 - 0.03894 * umur;
rasio1 = fev1 / fvc;
rasio2 = 97.89444 - 0.31804 * umur;
lcd.setCursor(0,0);
lcd.print("FVC=");
lcd.print(fvc);
lcd.print(" / ");
lcd.print(fvcx);
lcd.setCursor(0,1);
lcd.print("FEV1=");
lcd.print(fev1);
lcd.print(" / ");
lcd.print(fev1x);
lcd.setCursor(0,2);
lcd.print("Rasio=");
lcd.print(rasio1);
lcd.print(" / ");
lcd.print(rasio2);
if((rasio1 >= 0.75 * rasio2) && (fvc >= 0.8 * fvcx)){
//normal
lcd.setCursor(0,3);
lcd.print("NORMAL ");
ket = 1;
}
if((rasio1 >= 0.75 * rasio2) && (fvc < 0.8 * fvcx)){
//restriktif
lcd.setCursor(0,3);
lcd.print("RESTRIKTIF ");
ket = 2;
}
if((rasio1 < 0.75 * rasio2) && (fvc > 0.8 * fvcx) && (fev1 < 0.95 * fev1x)){
//gangguan obstrukstif
lcd.setCursor(0,3);
lcd.print("OBSTRUKTIF ");
ket = 3;
}
}
customKey = customKeypad.getKey();
if(customKey == '*'){
lcd.clear();
delay(1000);
return;
}
hasil();
}
void savesd(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("saving.....");
delay(3000);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
myFile.print("Jenis Kelamin= ");
if(klm == 1){
myFile.println("L");
}
if(klm == 2){
myFile.println("P");
}
myFile.print("Umur= ");
myFile.println(umur);
myFile.print("Tinggi= ");
myFile.println(tinggi);
myFile.print("Kondisi= ");
if(ket == 1){
myFile.println("NORMAL");
}
if(ket == 2){
myFile.println("RESTRIKTIF");
}
if(ket == 3){
myFile.println("OBSTRUKTIF");
}
myFile.println(" ");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
lcd.setCursor(0,0);
lcd.print("DONE..");
delay(1000);
lcd.clear();
}
f. VIDEO HASILNYA