18. slide_1
ตัวอย่างการเขียนโปรแกรม ควบคุมแบบ Slider
ถ้าดู video ไม่ได้ click
สำหรับการประยุกต์ใช้กับ Arduino หรือไมโครคอนโทรลเลอร์ ดูจากตัวอย่างในเว็บนี้ได้
---------------------------------------------------------------------------------------------------------------------
ถ้าต้องการส่งข้อมูลของการสไลด์ออกทางพอร์ทอนุกรม ไปหา Arduino ให้ใช้ ฟังก์ชั่น
mouseReleased() โดยทำดังนี้
กำหนดค่าตัวแปรใหม่ 2 ตัว
int digit;
String sa;
เขียนฟังก์ชั่นส่งข้อมูลภายใต้ ฟังก์ชั่น mouseReleased() ดังนี้
void mouseReleased() {
println(Yold);
if(Yold>99) digit = 3;
else if(Yold>9) digit = 2;
else digit = 1;
sa = nf(Yold, digit);
myPort.write(sa);
myPort.write(13);
}
------------------------------------------------------------------------------------------------------------------------
สำหรับตัวอย่างโปรแกรมบน Arduino สำหรับรับข้อมูลตัวเลขที่เป็น String มีค่าตั้งแต่ 0-999
แล้วนำแสดงออกที่ LCD เป็นดังนี้
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define INLENGTH 4
#define lf 10
#define cr 13
char inString[16];
int inCount;
int incomingByte = 0;
unsigned int number;
float fnum;
void setup()
{
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Read int. 0-999");
}
void loop() {
}
void serialEvent()
{
inCount = 0;
do
{
while (!Serial.available()); // wait for input
inString[inCount] = Serial.read(); // get it
if ((inString [inCount] == cr)||(inString [inCount] == lf)) break;
++inCount;
} while(inCount < INLENGTH);
inString[inCount] = 0; // null terminate the string
number = atoi(inString);
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(number);
}