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() {
Serial.print("Speed Motor (1-255)= ") ;
float a = InputTXT_Float() ;
Serial.println(a) ;
Serial.print("Time Motor On (sec) = ") ;
float b = InputTXT_Float() * 1000 ;
Serial.println(b/1000) ;
motorOnThenOffWithSpeed(a, b) ;
} // void loop()
/////////////////////////////////////////////////////////////////////////////////////
void motorOnThenOffWithSpeed(float s, float y) { // เปิด-ปิด มอเตอร์ให้ทำงานแบบใช้ analogWrite()
float onSpeed = s ; // a number between 0 (stopped) and full speed)
float onTime = y ; // the number of milliseconds for the motor to turn on
float offSpeed = 0 ; // a number between 0 (stopped) and 225 (full speed)
// float 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()