前提・実現したいこと
センサで測定した値を8pino(Arduino互換のマイコン)にI2C通信して送り、その値を8pinoから送信モジュールに送るプログラムを作りました。
最終的には、受信モジュールで受け取った値をArduinoUNOを介してパソコンで表示します。
このプログラムを書き込んだ8pinoとセンサと送信モジュールをはんだ付けでくっつけたもの(簡単するためこれをセンサ粒子と呼びます)が3つあり、すべて機能しておりました。このセンサ粒子以外にブレッドボード上で回路を作っており、それも以前まではちゃんと動いていました。そのあと期間が1か月ほど空いてしまいましたが、ブレッドボードで作った回路の方でプログラムを書き込みし作動させたところ機能しなくなりました(パソコンに表示されなくなった)。
発生している問題・エラーメッセージ
エラーメッセージなし
該当のソースコード
#include <TinyWireM.h> //#include <TimerOne.h> #include <VirtualWireM.h> const int transmit_pin = 3; const int receive_pin = 2; #define MPU9250_ADDRESS 0x68 #define MAG_ADDRESS 0x0C #define GYRO_FULL_SCALE_250_DPS 0x00 #define GYRO_FULL_SCALE_500_DPS 0x08 #define GYRO_FULL_SCALE_1000_DPS 0x10 #define GYRO_FULL_SCALE_2000_DPS 0x18 #define ACC_FULL_SCALE_2_G 0x00 #define ACC_FULL_SCALE_4_G 0x08 #define ACC_FULL_SCALE_8_G 0x10 #define ACC_FULL_SCALE_16_G 0x18 // This function read Nbytes bytes from I2C device at address Address. // Put read bytes starting at register Register in the Data array. void I2Cread(uint8_t Address, uint8_t Register, uint8_t Nbytes, uint8_t* Data) { // Set register address TinyWireM.beginTransmission(Address); TinyWireM.write(Register); TinyWireM.endTransmission(); // Read Nbytes TinyWireM.requestFrom(Address, Nbytes); uint8_t index=0; while (TinyWireM.available()) Data[index++]=TinyWireM.read(); } // Write a byte (Data) in device (Address) at register (Register) void I2CwriteByte(uint8_t Address, uint8_t Register, uint8_t Data) { // Set register address TinyWireM.beginTransmission(Address); TinyWireM.write(Register); TinyWireM.write(Data); TinyWireM.endTransmission(); } // Initial time long int ti; // Initializations void setup() { // Arduino initializations TinyWireM.begin(); // Set accelerometers low pass filter at 5Hz I2CwriteByte(MPU9250_ADDRESS,29,0x06); // Set gyroscope low pass filter at 5Hz I2CwriteByte(MPU9250_ADDRESS,26,0x06); // Configure gyroscope range I2CwriteByte(MPU9250_ADDRESS,27,GYRO_FULL_SCALE_1000_DPS); // Configure accelerometers range I2CwriteByte(MPU9250_ADDRESS,28,ACC_FULL_SCALE_4_G); // Set by pass mode for the magnetometers I2CwriteByte(MPU9250_ADDRESS,0x37,0x02); // Request continuous magnetometer measurements in 16 bits I2CwriteByte(MAG_ADDRESS,0x0A,0x16); // Store initial time ti=millis(); // Initialise the IO and ISR vw_set_tx_pin(transmit_pin); vw_set_rx_pin(receive_pin); vw_set_ptt_inverted(true); // Required for DR3100 vw_setup(7500); // Bits per sec } byte count = 1; // Main loop, read and display data void loop() { // ____________________________________ // ::: accelerometer and gyroscope ::: // Read accelerometer and gyroscope uint8_t Buf[21]; I2Cread(MPU9250_ADDRESS,0x3B,14,Buf); // Read register Status 1 and wait for the DRDY: Data Ready uint8_t ST1; do { I2Cread(MAG_ADDRESS, 0x02, 1, &ST1); } while (!(ST1 & 0x01)); // Read magnetometer data uint8_t Mag[7]; I2Cread(MAG_ADDRESS, 0x03, 7, Mag); int i=0; do { Buf[14 + i] = Mag[i]; i++; }while(i<8); vw_send(Buf, 21); vw_wait_tx(); // Wait until the whole message is gone // delay(1000); count = count + 1; }
試したこと
以前まで動いていたセンサ粒子に上記のプログラムを書き込まず、作動させたところPCに表示されたので、受信側のプログラムは間違えていないと判断し、送信側のプログラムに問題があることが分かりました。
プログラムのコンパイル時にエラーはありません。
部品や配線はすべて確認し、また8pinoから送信モジュールも簡単なプログラムを書き込みすると機能しました。
故障する前後ではArduinoの更新、ファイルの移動をしていました。ファイルを移動したことが原因なのか、ライブラリが一度消えており、
再インストールしています。
補足情報(FW/ツールのバージョンなど)
Arduinoのバージョンは1.8.2です。
ボードはArduino Gemma、書込装置はUSBtinyISPを使用しています。