Difference between revisions of "Arduino: Switch Case 2"

From OnnoWiki
Jump to navigation Jump to search
(Created page with "Sumber: https://docs.arduino.cc/built-in-examples/control-structures/SwitchCase2 An if statement allows you to choose between two discrete options, TRUE or FALSE. When there...")
 
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
Sumber: https://docs.arduino.cc/built-in-examples/control-structures/SwitchCase2
 
Sumber: https://docs.arduino.cc/built-in-examples/control-structures/SwitchCase2
  
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.
+
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.
  
This tutorial shows you how to use switch to turn on one of several different LEDs based on a byte of data received serially. The sketch listens for serial input, and turns on a different LED for the characters a, b, c, d, or e.
+
Tutorial ini menunjukkan cara menggunakan switch untuk menyalakan salah satu dari beberapa LED berbeda berdasarkan satu byte data yang diterima secara serial. Program sketch mendengarkan input serial, dan menyalakan LED berbeda untuk karakter a, b, c, d, atau e.
  
Hardware Required
 
Arduino Board
 
  
5 LEDs
+
==Hardware Required==
  
5 220 ohm resistors
+
* Arduino Board
 +
* 5 LEDs
 +
* 5 220 ohm resistor
 +
* kabel jumper
 +
* breadboard
  
hook-up wires
+
==Circuit==
  
breadboard
+
Lima LED dipasang pada pin digital 2, 3, 4, 5, dan 6 secara seri melalui resistor 220 ohm.
  
Circuit
+
Untuk membuat sketch ini berfungsi, board arduino kita harus terhubung ke komputer. Di Arduino IDE buka monitor serial dan kirim karakter a, b, c, d, atau e untuk menyalakan LED yang sesuai, atau apa pun untuk mematikannya.
Five LEDs are attached to digital pins 2, 3, 4, 5, and 6 in series through 220 ohm resistors.
 
  
To make this sketch work, your board must be connected to your computer. In the Arduino IDE open the serial monitor and send the characters a, b, c, d, or e to lit up the corresponding LED, or anything else to switch them off.
 
  
circuit
 
  
Schematic
+
[[File:Circuitswitchcase2.png|center|300px|thumb]]
schematic
 
  
Code
 
COPY
 
/*
 
  
  Switch statement with serial input
+
==Schematic==
  
  Demonstrates the use of a switch statement. The switch statement allows you
+
[[File:Schematicswitchcase2.png|center|300px|tumb]]
  
  to choose from among a set of discrete values of a variable. It's like a
 
  
  series of if statements.
+
==Code==
 
 
  To see this sketch in action, open the Serial monitor and send any character.
 
 
 
  The characters a, b, c, d, and e, will turn on LEDs. Any other character will
 
 
 
  turn the LEDs off.
 
 
 
  The circuit:
 
 
 
  - five LEDs attached to digital pins 2 through 6 through 220 ohm resistors
 
 
 
  created 1 Jul 2009
 
  
 +
/*
 +
  Switch statement with serial input
 +
  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, open the Serial monitor and send any character.
 +
  The characters a, b, c, d, and e, will turn on LEDs. Any other character will
 +
  turn the LEDs off.
 +
 +
  The circuit:
 +
  - five LEDs attached to digital pins 2 through 6 through 220 ohm resistors
 +
  created 1 Jul 2009
 +
 
   by Tom Igoe
 
   by Tom Igoe
 
 
   This example code is in the public domain.
 
   This example code is in the public domain.
 
 
   http://www.arduino.cchttps://www.arduino.cc/en/Tutorial/SwitchCase2
 
   http://www.arduino.cchttps://www.arduino.cc/en/Tutorial/SwitchCase2
 
+
*/
*/
+
 
+
void setup() {
void setup() {
+
  // initialize serial communication:
 
+
  Serial.begin(9600);
  // initialize serial communication:
+
  // initialize the LED pins:
 
+
  for (int thisPin = 2; thisPin < 7; thisPin++) {
  Serial.begin(9600);
+
    pinMode(thisPin, OUTPUT);
 
+
  }
  // initialize the LED pins:
+
}
 
+
  for (int thisPin = 2; thisPin < 7; thisPin++) {
+
void loop() {
 
+
  // read the sensor:
    pinMode(thisPin, OUTPUT);
+
  if (Serial.available() > 0) {
 
+
    int inByte = Serial.read();
  }
+
    // do something different depending on the character received.
}
+
    // The switch statement expects single number values for each case; in this
 
+
    // example, though, you're using single quotes to tell the controller to get
void loop() {
+
    // the ASCII value for the character. For example 'a' = 97, 'b' = 98,
 
+
    // and so forth:
  // read the sensor:
+
    switch (inByte) {
 
+
      case 'a':
  if (Serial.available() > 0) {
+
        digitalWrite(2, HIGH);
 
+
        break;
    int inByte = Serial.read();
+
      case 'b':
 
+
        digitalWrite(3, HIGH);
    // do something different depending on the character received.
+
        break;
 
+
      case 'c':
    // The switch statement expects single number values for each case; in this
+
        digitalWrite(4, HIGH);
 
+
        break;
    // example, though, you're using single quotes to tell the controller to get
+
      case 'd':
 
+
        digitalWrite(5, HIGH);
    // the ASCII value for the character. For example 'a' = 97, 'b' = 98,
+
        break;
 
+
      case 'e':
    // and so forth:
+
        digitalWrite(6, HIGH);
 
+
        break;
    switch (inByte) {
+
      default:
 
+
        // turn all the LEDs off:
      case 'a':
+
        for (int thisPin = 2; thisPin < 7; thisPin++) {
 
+
          digitalWrite(thisPin, LOW);
        digitalWrite(2, HIGH);
+
        }
 
+
    }
        break;
+
  }
 
+
}
      case 'b':
 
 
 
        digitalWrite(3, HIGH);
 
 
 
        break;
 
 
 
      case 'c':
 
 
 
        digitalWrite(4, HIGH);
 
 
 
        break;
 
 
 
      case 'd':
 
 
 
        digitalWrite(5, HIGH);
 
 
 
        break;
 
 
 
      case 'e':
 
 
 
        digitalWrite(6, HIGH);
 
 
 
        break;
 
 
 
      default:
 
 
 
        // turn all the LEDs off:
 
 
 
        for (int thisPin = 2; thisPin < 7; thisPin++) {
 
 
 
          digitalWrite(thisPin, LOW);
 
 
 
        }
 
 
 
    }
 
 
 
  }
 
}
 
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
 
 
 
Missing something?
 
Check out our store and get what you need to follow this tutorial.
 
VISIT OUR STORE
 
Suggest Changes
 
The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.
 
EDIT THIS PAGE
 
Navigated to Switch (case) Statement, used with serial input
 
Trademark
 
Contact Us
 
Distributors
 
Careers
 
Help Center
 
NEWSLETTER
 
  
  

Latest revision as of 08:59, 3 October 2022

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

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 menggunakan switch untuk menyalakan salah satu dari beberapa LED berbeda berdasarkan satu byte data yang diterima secara serial. Program sketch mendengarkan input serial, dan menyalakan LED berbeda untuk karakter a, b, c, d, atau e.


Hardware Required

  • Arduino Board
  • 5 LEDs
  • 5 220 ohm resistor
  • kabel jumper
  • breadboard

Circuit

Lima LED dipasang pada pin digital 2, 3, 4, 5, dan 6 secara seri melalui resistor 220 ohm.

Untuk membuat sketch ini berfungsi, board arduino kita harus terhubung ke komputer. Di Arduino IDE buka monitor serial dan kirim karakter a, b, c, d, atau e untuk menyalakan LED yang sesuai, atau apa pun untuk mematikannya.


Circuitswitchcase2.png


Schematic

tumb


Code

/*
  Switch statement with serial input
  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, open the Serial monitor and send any character.
  The characters a, b, c, d, and e, will turn on LEDs. Any other character will
  turn the LEDs off.

  The circuit:
  - five LEDs attached to digital pins 2 through 6 through 220 ohm resistors
  created 1 Jul 2009

 by Tom Igoe
 This example code is in the public domain.
 http://www.arduino.cchttps://www.arduino.cc/en/Tutorial/SwitchCase2
*/

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pins:
  for (int thisPin = 2; thisPin < 7; thisPin++) {
    pinMode(thisPin, OUTPUT);
  }
}

void loop() {
  // read the sensor:
  if (Serial.available() > 0) {
    int inByte = Serial.read();
    // do something different depending on the character received.
    // The switch statement expects single number values for each case; in this
    // example, though, you're using single quotes to tell the controller to get
    // the ASCII value for the character. For example 'a' = 97, 'b' = 98,
    // and so forth:
    switch (inByte) {
      case 'a':
        digitalWrite(2, HIGH);
        break;
      case 'b':
        digitalWrite(3, HIGH);
        break;
      case 'c':
        digitalWrite(4, HIGH);
        break;
      case 'd':
        digitalWrite(5, HIGH);
        break;
      case 'e':
        digitalWrite(6, HIGH);
        break;
      default:
        // turn all the LEDs off:
        for (int thisPin = 2; thisPin < 7; thisPin++) {
          digitalWrite(thisPin, LOW);
        }
    }
  }
}


Referensi