ผทอยากจะสอบถามวิธีการนำค่าจากKeypad แล้วส่งค่านั้นให้Servoไปทำต่อตามจำนวนที่คีย์เข้าไปต้องทำยังไงหรอครับ
1 Likes
#include <Keypad.h> // D2-D9 - ขวาไปซ้าย
#include <LiquidCrystal_I2C.h> // gnd,vcc 5v,asd a4,scl a5
#include <Wire.h>
#include <Servo.h>
LiquidCrystal_I2C lcd(0x27,16,2);
Servo myservo;
char customKey;
char val[10];
int num = 0, k = 0;
int value = 0;
int i = 0;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup () {
myservo.attach(12); //กำหนดขาที่ 12
Serial.begin(9600);
lcd.begin(); //กำหนดค่าเริ่มต้นLCD
lcd.setBacklight(255); //คือกำหนดแสงที่จอ LCD
lcd.print("Amount"); //คือ การแสดงผล
lcd.setCursor(0, 1); //เซต ตำแหน่ง 0,1
delay(3000); //สั่งรีเลย์ เท่ากับ 3วิ
lcd.clear(); //คือ ลบผลจอ lcd
}
}
void loop () {
customKey = keypad.getKey();
if (customKey != NO_KEY){
val[k] = customKey;
lcd.setCursor(0,1);
lcd.print(val[k]);
k++;
if(customKey){
Serial.println(customKey);
}
if(customKey == '#'){
value = atoi(val);
Serial.println(value);
k = 0;
lcd.setCursor(0,1);
lcd.print(value);
delay(3000); //สั่งรีเลย์ เท่ากับ 3วิ
lcd.clear(); //คือ ลบผลจอ lcd
}
if(value > 0){
for(int i = 0 ; i < value ; i++)
myservo.write(0);
delay(1000);
myservo.write(35);
delay(1000);
lcd.setCursor(0, 0); //บนจอบรรทัดที่ 1 แสดงข้อความ
lcd.print("Count");
lcd.setCursor(0, 1); //แสดงค่าจำนวนครั้งที่ทำไป
lcd.print(i);
Serial.println(i);
}
}
}
ตัวอย่างพื้นฐานครับลองศึกษาดูก่อน