Arduino: While Statement Conditional

From OnnoWiki
Jump to navigation Jump to search

Sumber: https://docs.arduino.cc/built-in-examples/control-structures/WhileStatementConditional


Terkadang kita ingin semua yang ada di program berhenti saat kondisi tertentu benar. Kita dapat melakukan ini menggunakan while loop. Contoh ini menunjukkan cara menggunakan while loop untuk mengkalibrasi nilai sensor analog.

Pada main loop, sketch di bawah ini membaca nilai fotoresistor pada pin analog 0 dan menggunakannya untuk memudarkan LED pada pin 9. Tetapi saat tombol yang terpasang pada pin digital 2 ditekan, program menjalankan metode yang disebut kalibrasi() yang mencari nilai tertinggi dan terendah dari sensor analog. Saat Anda melepaskan tombol, sketch berlanjut dengan main loop.

Teknik ini memungkinkan kita memperbarui nilai maksimum dan minimum untuk fotoresistor saat kondisi pencahayaan berubah.


Hardware Required

  • Arduino Board
  • pushbutton / switch
  • photoresistor atau analog sensor lainnya
  • 2 10k ohm resistor
  • breadboard

Circuit

Hubungkan sensor analog (misalnya potensiometer, sensor cahaya) pada input analog 2 dengan resistor 10K ohm ke ground. Hubungkan tombol ke pin digital, sekali lagi dengan resistor 10K ohm ke ground. Hubungkan LED ke pin digital 9, dengan resistor 220 ohm secara seri.


Circuitwhile.png


Schematic

Schematicwhile.png

Code

/*
  Conditionals - while statement
  This example demonstrates the use of  while() statements.
  While the pushbutton is pressed, the sketch runs the calibration routine.
  The sensor readings during the while loop define the minimum and maximum of
  expected values from the photoresistor.

  This is a variation on the calibrate example.
  The circuit:
  - photoresistor connected from +5V to analog in pin 0
  - 10 kilohm resistor connected from ground to analog in pin 0
  - LED connected from digital pin 9 to ground through 220 ohm resistor
  - pushbutton attached from pin 2 to +5V
  - 10 kilohm resistor attached from pin 2 to ground

  created 17 Jan 2009
  modified 30 Aug 2011
  by Tom Igoe
  modified 20 Jan 2017
  by Arturo Guadalupi
  This example code is in the public domain.
  http://www.arduino.cchttps://www.arduino.cc/en/Tutorial/WhileLoop
*/

// These constants won't change:
const int sensorPin = A0;       // pin that the sensor is attached to
const int ledPin = 9;           // pin that the LED is attached to
const int indicatorLedPin = 13; // pin that the built-in LED is attached to
const int buttonPin = 2;        // pin that the button is attached to

// These variables will change:
int sensorMin = 1023;  // minimum sensor value
int sensorMax = 0;     // maximum sensor value
int sensorValue = 0;         // the sensor value

void setup() {
  // set the LED pins as outputs and the switch pin as input:
  pinMode(indicatorLedPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop() {
  // while the button is pressed, take calibration readings:
  while (digitalRead(buttonPin) == HIGH) {
    calibrate();
  }
  // signal the end of the calibration period
  digitalWrite(indicatorLedPin, LOW);
  // read the sensor:
  sensorValue = analogRead(sensorPin);
  // apply the calibration to the sensor reading
  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
  // in case the sensor value is outside the range seen during calibration
  sensorValue = constrain(sensorValue, 0, 255);
  // fade the LED using the calibrated value:
  analogWrite(ledPin, sensorValue);
}

void calibrate() {
  // turn on the indicator LED to indicate that calibration is happening:
  digitalWrite(indicatorLedPin, HIGH);
  // read the sensor:
  sensorValue = analogRead(sensorPin);
  // record the maximum sensor value
  if (sensorValue > sensorMax) {
    sensorMax = sensorValue;
  }
  // record the minimum sensor value
  if (sensorValue < sensorMin) {
    sensorMin = sensorValue;
  }
}


Referensi