ジャイロセンサのデータの出力が通常シリアルモニタに表示されますが、それをSDカードに書き込みをしたいと考えております。
(Arduinoを使っており、ジャイロセンサはMPU6050,SDモジュールはAideepen Set of 5 Micro SD Storage Expansion Board Micro SD Card Memory Shield Module with Pin for ARM AVR です)
ジャイロセンサのプログラムは
#include <Wire.h>
long accelX, accelY, accelZ;
float gForceX, gForceY, gForceZ;
long gyroX, gyroY, gyroZ;
float rotX, rotY, rotZ;
void setup() {
Serial.begin(9600);
Wire.begin();
setupMPU6050();
}
void loop() {
recordAccelRegisters();
recordGyroRegisters();
printData();
delay(200);
}
void setupMPU6050() {
//MPU6050との通信を開始し、ジャイロと加速度の最大範囲を指定
Wire.beginTransmission(0b1101000); //I2C address of the MPU
Wire.write(0x6B); //Accessing the register 6B
Wire.write(0b00000000); //SLEEP register to 0
Wire.endTransmission();
Wire.beginTransmission(0b1101000);
Wire.write(0x1B); //Accessing the register 1B - Gyroscope Configuration
Wire.write(0x00000000); //gyro to full scale ± 250deg./s
Wire.endTransmission();
Wire.beginTransmission(0b1101000);
Wire.write(0x1C); //Accessing the register 1C - Acccelerometer Configuration
Wire.write(0b00000000); //accel to +/- 2g
Wire.endTransmission();
}
void recordAccelRegisters() {
//加速度読み取り
Wire.beginTransmission(0b1101000);
Wire.write(0x3B); // Accel Readings
Wire.endTransmission();
Wire.requestFrom(0b1101000, 6); //Request Accel Registers (3B - 40)
while (Wire.available() < 6);
accelX = Wire.read() << 8 | Wire.read(); //Store first two bytes into accelX
accelY = Wire.read() << 8 | Wire.read(); //Store middle two bytes into accelY
accelZ = Wire.read() << 8 | Wire.read(); //Store last two bytes into accelZ
calculateAccelData();
}
void calculateAccelData() {
//読み取った値をgに変換
gForceX = accelX / 16384.0;
gForceY = accelY / 16384.0;
gForceZ = accelZ / 16384.0;
}
void recordGyroRegisters() {
//ジャイロの値を読み取る
Wire.beginTransmission(0b1101000); //I2C address of the MPU
Wire.write(0x43); //Starting register for Gyro Readings
Wire.endTransmission();
Wire.requestFrom(0b1101000, 6); //Request Gyro Registers (43 - 48)
while (Wire.available() < 6);
gyroX = Wire.read() << 8 | Wire.read(); //Store first two bytes into accelX
gyroY = Wire.read() << 8 | Wire.read(); //Store middle two bytes into accelY
gyroZ = Wire.read() << 8 | Wire.read(); //Store last two bytes into accelZ
calculateGyroData();
}
void calculateGyroData() {
//読み取った値をdeg/secに変換
rotX = gyroX / 131.0;
rotY = gyroY / 131.0;
rotZ = gyroZ / 131.0;
}
void printData() {
//シリアルモニタに出力
Serial.print("Gyro");
Serial.print(" X=");
Serial.print(rotX);
Serial.print("deg/sec");
Serial.print(" Y=");
Serial.print(rotY);
Serial.print("deg/sec");
Serial.print(" Z=");
Serial.print(rotZ);
Serial.print("deg/sec");
Serial.print(" Accel");
Serial.print(" X=");
Serial.print(gForceX);
Serial.print('g');
Serial.print(" Y=");
Serial.print(gForceY);
Serial.print('g');
Serial.print(" Z=");
Serial.print(gForceZ);
Serial.println('g');
}
このよう0. 番号リストになり正常に作動しました。
SDカードに出力したい場合のプログラムが分かりません。
・SDカード出力するためのプログラム
を知りたいです。
追記
#include <Wire.h>
#include<SD.h>
const int chipSelect =4;
long accelX, accelY, accelZ;
float gForceX, gForceY, gForceZ;
long gyroX, gyroY, gyroZ;
float rotX, rotY, rotZ;
void setup() {
Serial.begin(9600);
Wire.begin();
setupMPU6050();
}
void loop() {
recordAccelRegisters();
recordGyroRegisters();
delay(200);
}
void setupMPU6050() {
//MPU6050との通信を開始し、ジャイロと加速度の最大範囲を指定
Wire.beginTransmission(0b1101000); //I2C address of the MPU
Wire.write(0x6B); //Accessing the register 6B
Wire.write(0b00000000); //SLEEP register to 0
Wire.endTransmission();
Wire.beginTransmission(0b1101000);
Wire.write(0x1B); //Accessing the register 1B - Gyroscope Configuration
Wire.write(0x00000000); //gyro to full scale ± 250deg./s
Wire.endTransmission();
Wire.beginTransmission(0b1101000);
Wire.write(0x1C); //Accessing the register 1C - Acccelerometer Configuration
Wire.write(0b00000000); //accel to +/- 2g
Wire.endTransmission();
}
void recordAccelRegisters() {
//加速度読み取り
Wire.beginTransmission(0b1101000);
Wire.write(0x3B); // Accel Readings
Wire.endTransmission();
Wire.requestFrom(0b1101000, 6); //Request Accel Registers (3B - 40)
while (Wire.available() < 6);
accelX = Wire.read() << 8 | Wire.read(); //Store first two bytes into accelX
accelY = Wire.read() << 8 | Wire.read(); //Store middle two bytes into accelY
accelZ = Wire.read() << 8 | Wire.read(); //Store last two bytes into accelZ
calculateAccelData();
}
void calculateAccelData() {
//読み取った値をgに変換
gForceX = accelX / 16384.0;
gForceY = accelY / 16384.0;
gForceZ = accelZ / 16384.0;
}
void recordGyroRegisters() {
//ジャイロの値を読み取る
Wire.beginTransmission(0b1101000); //I2C address of the MPU
Wire.write(0x43); //Starting register for Gyro Readings
Wire.endTransmission();
Wire.requestFrom(0b1101000, 6); //Request Gyro Registers (43 - 48)
while (Wire.available() < 6);
gyroX = Wire.read() << 8 | Wire.read(); //Store first two bytes into accelX
gyroY = Wire.read() << 8 | Wire.read(); //Store middle two bytes into accelY
gyroZ = Wire.read() << 8 | Wire.read(); //Store last two bytes into accelZ
calculateGyroData();
}
void calculateGyroData() {
//読み取った値をdeg/secに変換
rotX = gyroX / 131.0;
rotY = gyroY / 131.0;
rotZ = gyroZ / 131.0;
}
void printData() {
//シリアルモニタに出力
Serial.print("Gyro");
Serial.print(" X=");
Serial.print(rotX);
Serial.print("deg/sec");
Serial.print(" Y=");
Serial.print(rotY);
Serial.print("deg/sec");
Serial.print(" Z=");
Serial.print(rotZ);
Serial.print("deg/sec");
Serial.print(" Accel");
Serial.print(" X=");
Serial.print(gForceX);
Serial.print('g');
Serial.print(" Y=");
Serial.print(gForceY);
Serial.print('g');
Serial.print(" Z=");
Serial.print(gForceZ);
Serial.println('g');
}
File dataFile =SD.open("datalog.txt",FILE_WRITE);
if (dataFile){
dataFile.print("Gyro");
dataFile.print(" X=");
dataFile.print(rotX);
dataFile.print("deg/sec");
dataFile.print(" Y=");
dataFile.print(rotY);
dataFile.print("deg/sec");
dataFile.print(" Z=");
dataFile.print(rotZ);
dataFile.print("deg/sec");
dataFile.print(" Accel");
dataFile.print(" X=");
dataFile.print(gForceX);
dataFile.print('g');
dataFile.print(" Y=");
dataFile.print(gForceY);
dataFile.print('g');
dataFile.print(" Z=");
dataFile.print(gForceZ);
dataFile.println('g');
dataFile.close();
lcd.setCursor(0,1);
lcd.printf("CD write=");
lcd.print(sno);
lcd.setCursor(15,1);
lcd.printf("R");
}
else{
lcd.printf("error opening datalog.txt");
}
}
}
このようにプログラムしたのですが、
if (dataFile){
の行がエラー文が出てしまいます。
