The arduino will wait till you have entered a value.
This serial monitor is in TinkerCad.
Example 1
// variables
int myNumber;
String msg="Please Enter Your Number:";
String msg2="Your Number is: ";
void setup()
{
Serial.begin(9600);
}
void loop()
{
// 3 things we do: ask wait read
Serial.println(msg); //ask
while (Serial.available()==0){
// while there is no data
//on the serial loop do nothing ..wait
}
//read
myNumber=Serial.parseInt();
Serial.print(msg2);
Serial.println(myNumber);
}
-----------------------------------------------------
// variables
int numBlinks;
String msg="How many blinks do you want?:";
int j;
int bt=100;
int redPin=12;
void setup()
{
Serial.begin(9600);
pinMode(redPin,OUTPUT);
}
void loop()
{
// 3 things we do: ask wait read
Serial.println(msg); //ask
while (Serial.available()==0){
// while there is no data
//on the serial loop do nothing ..wait
}
//read
numBlinks=Serial.parseInt();
for (j=1;j<=numBlinks;j=j+1){
digitalWrite(redPin,HIGH);
delay(bt);
digitalWrite(redPin,LOW);
delay(bt);
}
}
-----------------------------------------------------------------------------------
// variables
String msg="What is the radius of your circle ?:";
float radius;
float area;
String msg2="Your Circle Has an Area of : ";
float pi=3.14;
void setup()
{
Serial.begin(9600);
}
void loop()
{
// 3 things we do: ask wait read
Serial.println(msg); //ask
{
while (Serial.available()==0);
// while there is no data
//on the serial loop do nothing ..wait
}
//read
radius=Serial.parseFloat(); // parseFloat not parseInt
area=pi*radius*radius;
Serial.print(msg2);
Serial.println(area);
}
----------------------------------------------
Links
No comments:
Post a Comment