スイッチサイエンスでラインフォロワアレイというものが売ってます。
www.switch-science.com
赤外線LEDとフォトリフレクタが8つ付いていて、I2Cで読み取れるらしいです。
ライントレースとかを手軽にやるには便利そうなので購入しました。
Arduinoライブラリはあるんですけどmbed用のものはないので作りました。
os.mbed.com
ライブラリを使用すると以下のようになります。
i2cアドレスがセンサーに書いてあるものとは記述が異なるのでそこだけ注意ですね。
#include "mbed.h" #include "SensorBar.h" const uint8_t SX1509_ADDRESS = 0x3E<<1; // SX1509 I2C address (00) int main() { I2C i2c(p28, p27); SensorBar mySensorBar(&i2c, SX1509_ADDRESS); mySensorBar.setBarStrobe(); mySensorBar.clearInvertBits(); uint8_t returnStatus = mySensorBar.begin(); printf("next, %d\r\n", returnStatus); if(returnStatus) { printf("sx1509 IC communication OK"); } else { printf("i2c failed"); wait_ms(10); while(1); } while(1) { uint8_t rawValue = mySensorBar.getRaw(); for( int i = 7; i >= 0; i-- ) { printf("%d", (rawValue >> i) & 0x01); } printf("b, "); printf("%d\r\n", mySensorBar.getPosition()); wait_ms(10); } }