Conditional Statements- While, if/else
While Loop
Description
A while loop will loop continuously, and infinitely, until the expression inside the parenthesis, () becomes false. Something must change the tested variable, or the while loop will never exit. This could be in your code, such as an incremented variable, or an external condition, such as testing a sensor.
Syntax
while(condition is true)
{ do this stuff; }
then do other stuff when the condition becomes false;
A while loop will loop continuously, and infinitely, until the expression inside the parenthesis, () becomes false. Something must change the tested variable, or the while loop will never exit. This could be in your code, such as an incremented variable, or an external condition, such as testing a sensor.
Syntax
while(condition is true)
{ do this stuff; }
then do other stuff when the condition becomes false;
Below: Partial code example- robot stops while way is blocked by object closer than 10 cm
#include <NewPing.h>
#define TRIGGER_PIN 2 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 3 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
}
void loop()
{
while (sonar.ping_cm() < 10) {
halt(); //halt while distance<10
}
forward(); //otherwise go forward
delay(100);
}
#define TRIGGER_PIN 2 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 3 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
}
void loop()
{
while (sonar.ping_cm() < 10) {
halt(); //halt while distance<10
}
forward(); //otherwise go forward
delay(100);
}
If/else
if (condition is True) {
do something; }
else {
do something else ; }
----------------------------
EXAMPLE:
if (sonar.ping_cm <10){
tone (8, 440, 500); } //beep for half a second
else {
noTone(8); } // don't beep
-----------------------------
void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);
// if the analog value is high enough, turn on the LED:
if (analogValue > threshold) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
do something; }
else {
do something else ; }
----------------------------
EXAMPLE:
if (sonar.ping_cm <10){
tone (8, 440, 500); } //beep for half a second
else {
noTone(8); } // don't beep
-----------------------------
void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);
// if the analog value is high enough, turn on the LED:
if (analogValue > threshold) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}