4章:Arduino(アルドゥイーノ)演習(Digital-01編)

    作成2015.08.16

  1. Arduino(アルドゥイーノ)演習参照アドレス
     Arduino(アルドゥイーノ)演習は下記のアドレスを参照します。
    https://www.arduino.cc/en/Tutorial/HomePage


  2. Digital/Blink Without Delayの回路図
     Digital/Blink Without Delayの回路図はBasics/Blinkの回路図と同じとなります。
     LEDの電流制限用として220〜1kΩの抵抗を直列に挿入します。





  3. Digital/Blink Without Delayのスケッチ
    (1)メニューの「ファイル」_「スケッチの例」_「Digital」_「Blink Without Delay」で以下のスケッチが設定されます。
    /* Blink without Delay
     Turns on and off a light emitting diode (LED) connected to a digital
     pin, without using the delay() function.  This means that other code
     can run at the same time without being interrupted by the LED code.
    
     The circuit:
     * LED attached from pin 13 to ground.
     * Note: on most Arduinos, there is already an LED on the board
     that's attached to pin 13, so no hardware is needed for this example.
     
     created 2005
     by David A. Mellis
     modified 8 Feb 2010
     by Paul Stoffregen
     modified 11 Nov 2013
     by Scott Fitzgerald
     
     This example code is in the public domain.
     
     http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
     */
    
    // constants won't change. Used here to set a pin number :
    const int ledPin =  13;      // the number of the LED pin
    
    // Variables will change :
    int ledState = LOW;             // ledState used to set the LED
    
    // Generally, you should use "unsigned long" for variables that hold time
    // The value will quickly become too large for an int to store
    unsigned long previousMillis = 0;        // will store last time LED was updated
    
    // constants won't change :
    const long interval = 1000;           // interval at which to blink (milliseconds)
    
    void setup() {
      // set the digital pin as output:
      pinMode(ledPin, OUTPUT);
    }
    
    void loop()
    {
      // here is where you'd put code that needs to be running all the time.
    
      // check to see if it's time to blink the LED; that is, if the
      // difference between the current time and last time you blinked
      // the LED is bigger than the interval at which you want to
      // blink the LED.
    //プログラムの実行を開始した時から現在までの時間をミル秒単位で返します。
    //約50日でオーバーフローしゼロに戻ります。
      unsigned long currentMillis = millis();
     
      if(currentMillis - previousMillis >= interval) {
        // save the last time you blinked the LED 
        previousMillis = currentMillis;   
    
        // if the LED is off turn it on and vice-versa:
        if (ledState == LOW)
          ledState = HIGH;
        else
          ledState = LOW;
    
        // set the LED with the ledState of the variable:
        digitalWrite(ledPin, ledState);
      }
    }
    


  4. Digital/Blink Without Delayの実行
    (1)メニューの「スケッチ」_「マイコンボードに書き込む」で書込みされ、実行されます。
    (2)LEDが1秒毎に点滅します。


  5. Digital/Buttonの回路図
     Digital/Buttonの回路図はBasics/Digital Read Serialの回路図と同じとなります。





  6. Digital/Buttonのスケッチ
    (1)メニューの「ファイル」_「スケッチの例」_Digital」_「Button」でスケッチが設定されます。
    /*
      Button
    
     Turns on and off a light emitting diode(LED) connected to digital
     pin 13, when pressing a pushbutton attached to pin 2.
    
     The circuit:
     * LED attached from pin 13 to ground
     * pushbutton attached to pin 2 from +5V
     * 10K resistor attached to pin 2 from ground
    
     * Note: on most Arduinos there is already an LED on the board
     attached to pin 13.
    
     created 2005
     by DojoDave 
     modified 30 Aug 2011
     by Tom Igoe
    
     This example code is in the public domain.
     http://www.arduino.cc/en/Tutorial/Button
     */
    
    // constants won't change. They're used here to
    // set pin numbers:
    const int buttonPin = 2;     // the number of the pushbutton pin
    const int ledPin =  13;      // the number of the LED pin
    
    // variables will change:
    int buttonState = 0;         // variable for reading the pushbutton status
    
    void setup() {
      // initialize the LED pin as an output:
      pinMode(ledPin, OUTPUT);
      // initialize the pushbutton pin as an input:
      pinMode(buttonPin, INPUT);
    }
    
    void loop() {
      // read the state of the pushbutton value:
      buttonState = digitalRead(buttonPin);
    
      // check if the pushbutton is pressed.
      // if it is, the buttonState is HIGH:
      if (buttonState == HIGH) {
        // turn LED on:
        digitalWrite(ledPin, HIGH);
      }
      else {
        // turn LED off:
        digitalWrite(ledPin, LOW);
      }
    }
    


  7. Digital/Buttonの実行
    (1)メニューの「スケッチ」_「マイコンボードに書き込む」で書込みされ、実行されます。
    (2)スイッチを押すとLED(L)オレンジが点灯し、離すと消灯します。


  8. Digital/Debounceの回路図(跳ね返り制御)
     Digital/Debounceの回路図の回路図はBasics/Digital Read Serialの回路図と同じとなります。。





  9. Digital/Debounceのスケッチ
    (1)(1)メニューの「ファイル」_「スケッチの例」_Digital」_「Debounce」でスケッチが設定されます。
    /*
     Debounce
    
     Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
     press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
     a minimum delay between toggles to debounce the circuit (i.e. to ignore
     noise).
    
     The circuit:
     * LED attached from pin 13 to ground
     * pushbutton attached from pin 2 to +5V
     * 10K resistor attached from pin 2 to ground
    
     * Note: On most Arduino boards, there is already an LED on the board
     connected to pin 13, so you don't need any extra components for this example.
    
    
     created 21 November 2006
     by David A. Mellis
     modified 30 Aug 2011
     by Limor Fried
     modified 28 Dec 2012
     by Mike Walters
    
     This example code is in the public domain.
    
     http://www.arduino.cc/en/Tutorial/Debounce
     */
    
    // constants won't change. They're used here to
    // set pin numbers:
    const int buttonPin = 2;    // the number of the pushbutton pin
    const int ledPin = 13;      // the number of the LED pin
    
    // Variables will change:
    int ledState = HIGH;         // the current state of the output pin
    int buttonState;             // the current reading from the input pin
    int lastButtonState = LOW;   // the previous reading from the input pin
    
    // the following variables are long's because the time, measured in miliseconds,
    // will quickly become a bigger number than can be stored in an int.
    long lastDebounceTime = 0;  // the last time the output pin was toggled
    long debounceDelay = 50;    // the debounce time; increase if the output flickers
    
    void setup() {
      pinMode(buttonPin, INPUT);
      pinMode(ledPin, OUTPUT);
    
      // set initial LED state
      digitalWrite(ledPin, ledState);
    }
    
    void loop() {
      // read the state of the switch into a local variable:
      int reading = digitalRead(buttonPin);
    
      // check to see if you just pressed the button
      // (i.e. the input went from LOW to HIGH),  and you've waited
      // long enough since the last press to ignore any noise:
    
      // If the switch changed, due to noise or pressing:
      if (reading != lastButtonState) {
        // reset the debouncing timer
        lastDebounceTime = millis();
      }
    
      if ((millis() - lastDebounceTime) > debounceDelay) {
        // whatever the reading is at, it's been there for longer
        // than the debounce delay, so take it as the actual current state:
    
        // if the button state has changed:
        if (reading != buttonState) {
          buttonState = reading;
    
          // only toggle the LED if the new button state is HIGH
          if (buttonState == HIGH) {
            ledState = !ledState;
          }
        }
      }
    
      // set the LED:
      digitalWrite(ledPin, ledState);
    
      // save the reading.  Next time through the loop,
      // it'll be the lastButtonState:
      lastButtonState = reading;
    }
    


  10. Digital/Debounceの実行
    (1)メニューの「スケッチ」_「マイコンボードに書き込む」で書込みされ、実行されます。
    (2)スイッチを押すとLED(L)オレンジが点灯し、離しても点灯のままとなります。
    (3)もう一度スイッチを押すと消灯します。


  11. Digital/State change detection (edge detection)の回路図
     Digital/State change detectionの回路図の回路図はBasics/Digital Read Serialの回路図と同じとなります。





  12. Digital/Button State Change Detectionのスケッチ
    (1)メニューの「ファイル」_「スケッチの例」_「Digital」_「Button State Change Detection」で以下のスケッチが設定されます。
    /*
      State change detection (edge detection)
    
     Often, you don't need to know the state of a digital input all the time,
     but you just need to know when the input changes from one state to another.
     For example, you want to know when a button goes from OFF to ON.  This is called
     state change detection, or edge detection.
    
     This example shows how to detect when a button or button changes from off to on
     and on to off.
    
     The circuit:
     * pushbutton attached to pin 2 from +5V
     * 10K resistor attached to pin 2 from ground
     * LED attached from pin 13 to ground (or use the built-in LED on
       most Arduino boards)
    
     created  27 Sep 2005
     modified 30 Aug 2011
     by Tom Igoe
    
    This example code is in the public domain.
    
     http://www.arduino.cc/en/Tutorial/ButtonStateChange
    
     */
    
    // this constant won't change:
    const int  buttonPin = 2;    // the pin that the pushbutton is attached to
    const int ledPin = 13;       // the pin that the LED is attached to
    
    // Variables will change:
    int buttonPushCounter = 0;   // counter for the number of button presses
    int buttonState = 0;         // current state of the button
    int lastButtonState = 0;     // previous state of the button
    
    void setup() {
      // initialize the button pin as a input:
      pinMode(buttonPin, INPUT);
      // initialize the LED as an output:
      pinMode(ledPin, OUTPUT);
      // initialize serial communication:
      Serial.begin(9600);
    }
    
    
    void loop() {
      // read the pushbutton input pin:
      buttonState = digitalRead(buttonPin);
    
      // compare the buttonState to its previous state
      if (buttonState != lastButtonState) {
        // if the state has changed, increment the counter
        if (buttonState == HIGH) {
          // if the current state is HIGH then the button
          // wend from off to on:
          buttonPushCounter++;
          Serial.println("on");
          Serial.print("number of button pushes:  ");
          Serial.println(buttonPushCounter);
        }
        else {
          // if the current state is LOW then the button
          // wend from on to off:
          Serial.println("off");
        }
        // Delay a little bit to avoid bouncing
        delay(50);
      }
      // save the current state as the last state,
      //for next time through the loop
      lastButtonState = buttonState;
    
    
      // turns on the LED every four button pushes by
      // checking the modulo of the button push counter.
      // the modulo function gives you the remainder of
      // the division of two numbers:
      if (buttonPushCounter % 4 == 0) {
        digitalWrite(ledPin, HIGH);
      } else {
        digitalWrite(ledPin, LOW);
      }
    }
    


  13. Digital/State change detectionの実行
    (1)メニューの「スケッチ」_「マイコンボードに書き込む」で書込みされ、実行されます。
    (2)メニューの「ツール」_「シリアルモニタ」を選択するとシリアルモニタが表示されます。
    (3)スイッチの押された回数がシリアルモニタに表示されます。
    (4)実行結果を以下に示します。


  14. Digital/Input Pullup Serialの回路図
     Digital/Input Pullup Serialの回路図を以下に示します。





  15. Digital/Input Pullup Serialのスケッチ
    (1)メニューの「ファイル」_「スケッチの例」_「Digital」_「Input Pullup Serial」で以下のスケッチが設定されます。
    /*
     Input Pullup Serial
     This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a
     digital input on pin 2 and prints the results to the serial monitor.
    
     The circuit:
     * Momentary switch attached from pin 2 to ground
     * Built-in LED on pin 13
    
     Unlike pinMode(INPUT), there is no pull-down resistor necessary. An internal
     20K-ohm resistor is pulled to 5V. This configuration causes the input to
     read HIGH when the switch is open, and LOW when it is closed.
    
     created 14 March 2012
     by Scott Fitzgerald
     http://www.arduino.cc/en/Tutorial/InputPullupSerial
     This example code is in the public domain
     */
    void setup() {
      //start serial connection
      Serial.begin(9600);
      //configure pin2 as an input and enable the internal pull-up resistor
      pinMode(2, INPUT_PULLUP);//**注(1)ピン2をプルアップ設定
      pinMode(13, OUTPUT);
    
    }
    
    void loop() {
      //read the pushbutton value into a variable
      int sensorVal = digitalRead(2);
      //print out the value of the pushbutton
      Serial.println(sensorVal);
    
      // Keep in mind the pullup means the pushbutton's
      // logic is inverted. It goes HIGH when it's open,
      // and LOW when it's pressed. Turn on pin 13 when the
      // button's pressed, and off when it's not:
      if (sensorVal == HIGH) {
        digitalWrite(13, LOW);
      }
      else {
        digitalWrite(13, HIGH);
      }
    }
    


  16. Digital/Input Pullup Serialの実行
    (1)メニューの「スケッチ」_「マイコンボードに書き込む」で書込みされ、実行されます。
    (2)メニューの「ツール」_「シリアルモニタ」を選択するとシリアルモニタが表示されます。
    (3)スイッチを押すとLED(L)オレンジが点灯し、モニタに1が表示されます。
    (4)スイッチを離すとLED(L)オレンジが消灯し、モニタに0が表示されます。


  17. Arduino(アルドゥイーノ)演習(Digital-01編)まとめ
    (1)Digital/Blink Without Delayではmillis()関数の使用方法が理解できます。
    (2)Digital/Debounceは標準的なスイッチ回路です。
    (3)Digital/Debounceは、スイッチを押すと状態が変化します。
    (4)Digital/State change detection (edge detection)はスイッチの押された回数をカウントします。
    (5)Digital/Input Pullup SerialはpinMode(2, INPUT_PULLUP)を使用するとスイッチ回路を簡略できることを示しています。

    **millis();関数とpinMode(2, INPUT_PULLUP)関数がポイント!!




5章:Arduino(アルドゥイーノ)演習(Digital-02編)に行く。

トップページに戻る。