ATmega328P-PU と ブレッドボードを使って タクトスイッチに連動した出力信号 実験 2

実験2.タクトスイッチの押す回数で状態変化検出 / エッジ検出


今回は、4番ピン(digital pin 2) タクトスイッチを5回押すと19番ピン(digital pin13) LEDが点灯します。

*** タクトスイッチを押す回数は任意の数字で設定できます ***


ATmega328P-PU IC Chip 1個
16MHz 発信器 1個
22pF セラミックコンデンサー 2個
10KΩ 抵抗器 1個
333Ω 抵抗器 LEDと同数
ブレッドボード 1個
タクトスイッチ 1個


スケッチ 実験2:(下記のスケッチ例をカットアンドペーストしてください)

// 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:
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 went from off to on:
Serial.print(“number of button pushes: “);
} else {
// if the current state is LOW then the button went from on to off:
// Delay a little bit to avoid bouncing
// 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 % 6 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);



const int  buttonPin = 2;    // the pin that the pushbutton is attached to  タクトスイッチを繋ぐ場所(ピン番号)

const int ledPin = 13;       // the pin that the LED is attached to  LEDを繋ぐ場所(ピン番号)

if (buttonPushCounter % 6 == 0) {  状態の変化をさせるタクトスイッチの押す回数







スケッチ例 実験3:(下記のスケッチをカットアンドペーストしてください)

// 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 unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 500;    // 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;





unsigned long debounceDelay = 500;    // the debounce time; increase if the output flickers