2012年1月1日日曜日

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);                
}

0 件のコメント:

コメントを投稿