Difference between revisions of "Arduino: Switch Case"

From OnnoWiki
Jump to navigation Jump to search
(Created page with "Sumber: https://docs.arduino.cc/built-in-examples/control-structures/SwitchCase An if statement allows you to choose between two discrete options, TRUE or FALSE. When there a...")
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
Sumber: https://docs.arduino.cc/built-in-examples/control-structures/SwitchCase
 
Sumber: https://docs.arduino.cc/built-in-examples/control-structures/SwitchCase
  
An if statement allows you to choose between two discrete options, TRUE or FALSE. When there are more than two options, you can use multiple if statements, or you can use the switch statement. Switch allows you to choose between several discrete options. This tutorial shows you how to use it to switch between four desired states of a photo resistor: really dark, dim, medium, and bright.
+
Pernyataan if memungkinkan kita untuk memilih di antara dua opsi terpisah, TRUE atau FALSE. Bila ada lebih dari dua opsi, kita dapat menggunakan beberapa pernyataan if, atau kita dapat menggunakan pernyataan switch. Switch memungkinkan kita untuk memilih di antara beberapa opsi diskrit. Tutorial ini menunjukkan cara menggunakannya untuk beralih di antara empat status resistor foto yang diinginkan: really dark, dim, medium, dan bright.
  
This program first reads the photoresistor. Then it uses the map() function to map its output to one of four values: 0, 1, 2, or 3. Finally, it uses the switch() statement to print one of four messages back to the computer depending on which of the four values is returned.
+
Program ini pertama membaca fotoresistor. Kemudian ia menggunakan fungsi map() untuk memetakan outputnya ke salah satu dari empat nilai: 0, 1, 2, atau 3. Terakhir, ia menggunakan pernyataan switch() untuk mencetak satu dari empat pesan kembali ke komputer tergantung pada yang mana dari empat nilai dikembalikan.
  
Hardware Required
 
Arduino Board
 
  
photoresistor, or another analog sensor
+
==Hardware Required==
  
10k ohm resistors
+
* Arduino Board
 +
* photoresistor, or another analog sensor
 +
* 10k ohm resistors
 +
* hook-up wires
 +
* breadboard
  
hook-up wires
+
==Circuit==
  
breadboard
+
Fotoresistor dihubungkan ke analog di pin 0 menggunakan rangkaian pembagi tegangan. Sebuah resistor 10K ohm membentuk sisi lain dari pembagi tegangan, berjalan dari Analog di 0 ke ground. Fungsi analogRead() mengembalikan kisaran sekitar 0 hingga 600 dari sirkuit ini di ruang dalam ruangan yang cukup terang.
  
Circuit
 
The photoresistor is connected to analog in pin 0 using a voltage divider circuit. A 10K ohm resistor makes up the other side of the voltage divider, running from Analog in 0 to ground. The analogRead() function returns a range of about 0 to 600 from this circuit in a reasonably lit indoor space.
 
  
circuit
+
[[File:Circuitswitchcase.png|center|200px|thumb]]
  
Schematic
+
==Schematic==
schematic
 
 
 
 
 
 
 
 
 
/*
 
  Switch statement
 
 
 
  Demonstrates the use of a switch statement. The switch statement allows you
 
  to choose from among a set of discrete values of a variable. It's like a
 
  series of if statements.
 
 
 
  To see this sketch in action, put the board and sensor in a well-lit room,
 
  open the Serial Monitor, and move your hand gradually down over the sensor.
 
 
 
  The circuit:
 
  - photoresistor from analog in 0 to +5V
 
  - 10K resistor from analog in 0 to ground
 
 
 
  created 1 Jul 2009
 
  modified 9 Apr 2012
 
  by Tom Igoe
 
 
 
  This example code is in the public domain.
 
 
 
  https://www.arduino.cc/en/Tutorial/BuiltInExamples/SwitchCase
 
*/
 
 
 
// these constants won't change. They are the lowest and highest readings you
 
// get from your sensor:
 
const int sensorMin = 0;      // sensor minimum, discovered through experiment
 
const int sensorMax = 600;    // sensor maximum, discovered through experiment
 
 
 
void setup() {
 
  // initialize serial communication:
 
  Serial.begin(9600);
 
}
 
 
 
void loop() {
 
  // read the sensor:
 
  int sensorReading = analogRead(A0);
 
  // map the sensor range to a range of four options:
 
  int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
 
 
 
  // do something different depending on the range value:
 
  switch (range) {
 
    case 0:    // your hand is on the sensor
 
      Serial.println("dark");
 
      break;
 
    case 1:    // your hand is close to the sensor
 
      Serial.println("dim");
 
      break;
 
    case 2:    // your hand is a few inches from the sensor
 
      Serial.println("medium");
 
      break;
 
    case 3:    // your hand is nowhere near the sensor
 
      Serial.println("bright");
 
      break;
 
  }
 
  delay(1);        // delay in between reads for stability
 
}
 
 
 
 
 
 
 
 
 
Learn more
 
You can find more basic tutorials in the built-in examples section.
 
 
 
You can also explore the language reference, a detailed collection of the Arduino programming language.
 
 
 
Last revision 2015/08/11 by SM
 
  
 +
[[File:Schematicswitchcase.png|center|200px|thumb]]
  
  
 +
==Code==
  
 +
/*
 +
  Switch statement
 +
 +
  Demonstrates the use of a switch statement. The switch statement allows you
 +
  to choose from among a set of discrete values of a variable. It's like a
 +
  series of if statements.
 +
 +
  To see this sketch in action, put the board and sensor in a well-lit room,
 +
  open the Serial Monitor, and move your hand gradually down over the sensor.
 +
 +
  The circuit:
 +
  - photoresistor from analog in 0 to +5V
 +
  - 10K resistor from analog in 0 to ground
 +
 +
  created 1 Jul 2009
 +
  modified 9 Apr 2012
 +
  by Tom Igoe
 +
 +
  This example code is in the public domain.
 +
 +
  https://www.arduino.cc/en/Tutorial/BuiltInExamples/SwitchCase
 +
*/
 +
 +
// these constants won't change. They are the lowest and highest readings you
 +
// get from your sensor:
 +
const int sensorMin = 0;      // sensor minimum, discovered through experiment
 +
const int sensorMax = 600;    // sensor maximum, discovered through experiment
 +
 +
void setup() {
 +
  // initialize serial communication:
 +
  Serial.begin(9600);
 +
}
 +
 +
void loop() {
 +
  // read the sensor:
 +
  int sensorReading = analogRead(A0);
 +
  // map the sensor range to a range of four options:
 +
  int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
 +
 +
  // do something different depending on the range value:
 +
  switch (range) {
 +
    case 0:    // your hand is on the sensor
 +
      Serial.println("dark");
 +
      break;
 +
    case 1:    // your hand is close to the sensor
 +
      Serial.println("dim");
 +
      break;
 +
    case 2:    // your hand is a few inches from the sensor
 +
      Serial.println("medium");
 +
      break;
 +
    case 3:    // your hand is nowhere near the sensor
 +
      Serial.println("bright");
 +
      break;
 +
  }
 +
  delay(1);        // delay in between reads for stability
 +
}
  
  

Latest revision as of 08:30, 3 October 2022

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

Pernyataan if memungkinkan kita untuk memilih di antara dua opsi terpisah, TRUE atau FALSE. Bila ada lebih dari dua opsi, kita dapat menggunakan beberapa pernyataan if, atau kita dapat menggunakan pernyataan switch. Switch memungkinkan kita untuk memilih di antara beberapa opsi diskrit. Tutorial ini menunjukkan cara menggunakannya untuk beralih di antara empat status resistor foto yang diinginkan: really dark, dim, medium, dan bright.

Program ini pertama membaca fotoresistor. Kemudian ia menggunakan fungsi map() untuk memetakan outputnya ke salah satu dari empat nilai: 0, 1, 2, atau 3. Terakhir, ia menggunakan pernyataan switch() untuk mencetak satu dari empat pesan kembali ke komputer tergantung pada yang mana dari empat nilai dikembalikan.


Hardware Required

  • Arduino Board
  • photoresistor, or another analog sensor
  • 10k ohm resistors
  • hook-up wires
  • breadboard

Circuit

Fotoresistor dihubungkan ke analog di pin 0 menggunakan rangkaian pembagi tegangan. Sebuah resistor 10K ohm membentuk sisi lain dari pembagi tegangan, berjalan dari Analog di 0 ke ground. Fungsi analogRead() mengembalikan kisaran sekitar 0 hingga 600 dari sirkuit ini di ruang dalam ruangan yang cukup terang.


Circuitswitchcase.png

Schematic

Schematicswitchcase.png


Code

/*
  Switch statement 

  Demonstrates the use of a switch statement. The switch statement allows you
  to choose from among a set of discrete values of a variable. It's like a
  series of if statements.

  To see this sketch in action, put the board and sensor in a well-lit room,
  open the Serial Monitor, and move your hand gradually down over the sensor.

  The circuit:
  - photoresistor from analog in 0 to +5V
  - 10K resistor from analog in 0 to ground

  created 1 Jul 2009
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/SwitchCase
*/

// these constants won't change. They are the lowest and highest readings you
// get from your sensor:
const int sensorMin = 0;      // sensor minimum, discovered through experiment
const int sensorMax = 600;    // sensor maximum, discovered through experiment 

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop() {
  // read the sensor:
  int sensorReading = analogRead(A0);
  // map the sensor range to a range of four options:
  int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

  // do something different depending on the range value:
  switch (range) {
    case 0:    // your hand is on the sensor
      Serial.println("dark");
      break;
    case 1:    // your hand is close to the sensor
      Serial.println("dim");
      break;
    case 2:    // your hand is a few inches from the sensor
      Serial.println("medium");
      break;
    case 3:    // your hand is nowhere near the sensor
      Serial.println("bright");
      break;
  }
  delay(1);        // delay in between reads for stability
} 


Referensi