int InputTXT_Int() {
int val;
while (Serial.available() == 0) ; // Wait here until input buffer has a character
{
val = Serial.parseInt(); // returns the first valid integer point number from the Serial buffer
while (Serial.available() > 0)
{
Serial.read() ; // clear the keyboard buffer
}
}
return val;
}
float InputTXT_Float() {
float val;
while (Serial.available() == 0) ; // Wait here until input buffer has a character
{
val = Serial.parseFloat(); // returns the first valid floating point number from the Serial buffer
while (Serial.available() > 0) // .parseFloat() can leave non-numeric characters
{
Serial.read() ; // clear the keyboard buffer
}
}
return val;
}
///////////////////////////////////////////////////////////////////////////////////
int motorPin = 9 ;
void setup() {
Serial.begin(9600);
pinMode(motorPin, OUTPUT);
} // void setup()
void loop() {
//--------------เลือกใช้ฟังก์ชั้นการทำงาน--------------//
motorOnThenOff(); // เปิด-ปิด มอเตอร์ให้ทำงานแบบใช้ digitalWrite()
// motorOnThenOffWithSpeed(); // เปิด-ปิด มอเตอร์ให้ทำงานแบบใช้ analogWrite()
// motorOnAcceleration(); // การเร่งความเร็วมอเตอร์แบบอัตโนมัติ
} // void loop()
/////////////////////////////////////////////////////////////////////////////////////
void motorOnThenOff() { // เปิด-ปิด มอเตอร์ให้ทำงานแบบใช้ digitalWrite()
int onTime = 2500 ; // the number of milliseconds for the motor to turn on for
int offTime = 1000 ; // the number of milliseconds for the motor to turn off for
digitalWrite(motorPin, HIGH); // turns the motor On
delay(onTime); // waits for onTime milliseconds
digitalWrite(motorPin, LOW); // turns the motor Off
delay(offTime); // waits for offTime milliseconds
} // void motorOnThenOff()
/////////////////////////////////////////////////////////////////////////////////////
void motorOnThenOffWithSpeed() { // เปิด-ปิด มอเตอร์ให้ทำงานแบบใช้ analogWrite()
int onSpeed = 200 ; // a number between 0 (stopped) and full speed)
int onTime = 2500 ; // the number of milliseconds for the motor to turn on
int offSpeed = 50 ; // a number between 0 (stopped) and 225 (full speed)
int offTime = 1000 ; // the number of milliseconds for the motor to turn off for
analogWrite(motorPin, onSpeed); // turns the motor On
delay(onTime); // waits for onTime milliseconds
analogWrite(motorPin, offSpeed); // turns the motor Off
delay(offTime); // waits for offTime milliseconds
} // void motorOnThenOffWithSpeed()
/////////////////////////////////////////////////////////////////////////////////////
void motorOnAcceleration(){ // การเร่งความเร็วมอเตอร์แบบอัตโนมัติ
int delayTime = 50 ; // milliseconds between each speed step
// Accelerates the motor เพิ่มความเร็วแบบอัตโนมัติ
for(int i = 0 ; i < 256 ; i++){ // goes through each speed from 0 to 255
analogWrite(motorPin, i); // sets the new speed
delay(delayTime); // waits for delayTime milliseconds
} // for(int i = 0 ; i < 256 ; i++)
// Decelerates the motor ลดความเร็วแบบอัตโนมัติ
for(int i = 255 ; i >= 0 ; i--){ // goes through each speed from 255 to 0
analogWrite(motorPin, i); // sets the new speed
delay(delayTime); // waits for delayTime milliseconds
} // for(int i = 255 ; i >= 0 ; i--)
} // void motorAcceleration()