Sketch
/*
pushbuttonをおした回数をカウントする
counter
The circuit:
* +5Vと2番ピンの間にpushbuttonを接続する
* 抵抗10Kを2番ピンとグランドの間に接続する
*
This example code is in the public domain.
http://arduino.cc/en/Tutorial/ButtonStateChange
4回ごとにLEDが点灯する
*/
int buttonPin = 2; // 押しボタン
int ledPin = 13; // 内蔵LED
int buttonPushCounter = 0; // カウンター
int buttonState = 0; // 押しボタンの状態
int lastButtonState = 0; // 前回の押しボタンの状態
void setup() {
// 入力に設定
pinMode(buttonPin, INPUT);
// LEDを出力に設定
pinMode(ledPin, OUTPUT);
// シリアルを設定
Serial.begin(9600);
}
void loop() {
// 押しボタンを読取
buttonState = digitalRead(buttonPin);
// 前回の状態と比較
if (buttonState != lastButtonState) {
//状態が変化していたらカウンター数を加数
if (buttonState == HIGH) {
// 現在の状態が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 {
// 現在の状態がLOWならif the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
}
// 次回のために現在の状態を最後の状態と差し替えるsave the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
// 4回ごとにLEDを点灯させるtturns 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);
}
}
2012年1月3日火曜日
2012年1月1日日曜日
割り込みの状態が変化したらLEDを反転させる
Sketch
//デジタルピン2(割り込みピン)の状態が変化したらLEDを反転させる
int ledPin=13;
volatile int state = LOW;
void setup() {
pinMode(ledPin,OUTPUT);
//attachInterrupt(0, blink, CHANGE); //0->デジタルピン2 1->デジタルピン3
attachInterrupt(0, blink, RISING); //デジタルピン2 LOW-->HIGH
//attachInterrupt(0, blink, FALLING); //デジタルピン2
}
void loop() {
digitalWrite(ledPin,state);
}
void blink() {
state= !state;
}
//デジタルピン2(割り込みピン)の状態が変化したらLEDを反転させる
int ledPin=13;
volatile int state = LOW;
void setup() {
pinMode(ledPin,OUTPUT);
//attachInterrupt(0, blink, CHANGE); //0->デジタルピン2 1->デジタルピン3
attachInterrupt(0, blink, RISING); //デジタルピン2 LOW-->HIGH
//attachInterrupt(0, blink, FALLING); //デジタルピン2
}
void loop() {
digitalWrite(ledPin,state);
}
void blink() {
state= !state;
}
LEDの光量を制御する。
Sketch
/*
LEDの光量を制御する。
The circuit:
* Potentiometer 10kΩを使用する。
中央のピンをアナログピン0に接続
右のピンをグランドに接続
残りのピンを +5Vに接続
* LED をデジタルピン11に接続(PWM)
* LED カソード (short leg)をグランドに接続
http://arduino.cc/en/Tutorial/AnalogInput
*圧電ブザー をデジタルピン10に接続(PWM)
*/
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 11; // select the pin for the LED PWM
int buzzerPin = 10; // select the pin for the Buzzer PWM
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600); // 9600bpsでポートを開く
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
analogWrite(ledPin, sensorValue/4);
analogWrite(buzzerPin, sensorValue/4);
Serial.println(sensorValue/4);
// stop the program for <sensorValue> milliseconds:
delay(500);
// turn the ledPin off:
analogWrite(ledPin, 0);
analogWrite(buzzerPin, 0);
// stop the program for for <sensorValue> milliseconds:
delay(100);
}
/*
LEDの光量を制御する。
The circuit:
* Potentiometer 10kΩを使用する。
中央のピンをアナログピン0に接続
右のピンをグランドに接続
残りのピンを +5Vに接続
* LED をデジタルピン11に接続(PWM)
* LED カソード (short leg)をグランドに接続
http://arduino.cc/en/Tutorial/AnalogInput
*圧電ブザー をデジタルピン10に接続(PWM)
*/
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 11; // select the pin for the LED PWM
int buzzerPin = 10; // select the pin for the Buzzer PWM
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600); // 9600bpsでポートを開く
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
analogWrite(ledPin, sensorValue/4);
analogWrite(buzzerPin, sensorValue/4);
Serial.println(sensorValue/4);
// stop the program for <sensorValue> milliseconds:
delay(500);
// turn the ledPin off:
analogWrite(ledPin, 0);
analogWrite(buzzerPin, 0);
// stop the program for for <sensorValue> milliseconds:
delay(100);
}
登録:
投稿 (Atom)