オペアンプでセンサーの出力をデジタル化できます。
この回路は赤外線距離センサーの出力を増幅し、オペアンプでコンパレータ回路を実現したものです。基準値は非反転入力で調整します。
これでアナログの束縛から解放されますね。
割り込みを使った Arduino 用のプログラムはこちらです。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <avr/sleep.h> | |
int ledPin = 13; | |
void setup() { | |
pinMode(2, INPUT_PULLUP); | |
pinMode(3, INPUT_PULLUP); | |
pinMode(ledPin, OUTPUT); | |
digitalWrite(ledPin, LOW); | |
} | |
void lightOff() | |
{ | |
digitalWrite(ledPin, LOW); | |
} | |
void lightOn() | |
{ | |
digitalWrite(ledPin, HIGH); | |
} | |
void sleepNow() | |
{ | |
set_sleep_mode(SLEEP_MODE_PWR_DOWN); | |
sleep_enable(); | |
attachInterrupt(0, lightOff, FALLING); | |
attachInterrupt(1, lightOn, RISING); | |
sleep_mode(); | |
sleep_disable(); | |
detachInterrupt(0); | |
detachInterrupt(1); | |
} | |
void loop() { | |
sleepNow(); | |
} |