質問編集履歴

1

誤字、プログラムの更新

2022/11/24 06:31

投稿

takerussatto
takerussatto

スコア0

test CHANGED
File without changes
test CHANGED
@@ -105,8 +105,154 @@
105
105
  Serial.println('g');
106
106
  }
107
107
 
108
- このようになり正常に作動しました。
108
+ このよう0. 番号リストになり正常に作動しました。
109
109
  SDカードに出力したい場合のプログラムが分かりません。
110
110
 
111
111
  ・SDカード出力するためのプログラム
112
112
  を知りたいです。
113
+
114
+ 追記 
115
+ #include <Wire.h>
116
+ #include<SD.h>
117
+ const int chipSelect =4;
118
+
119
+ long accelX, accelY, accelZ;
120
+ float gForceX, gForceY, gForceZ;
121
+
122
+ long gyroX, gyroY, gyroZ;
123
+ float rotX, rotY, rotZ;
124
+
125
+ void setup() {
126
+ Serial.begin(9600);
127
+ Wire.begin();
128
+ setupMPU6050();
129
+ }
130
+
131
+
132
+ void loop() {
133
+ recordAccelRegisters();
134
+ recordGyroRegisters();
135
+
136
+ delay(200);
137
+ }
138
+
139
+ void setupMPU6050() {
140
+ //MPU6050との通信を開始し、ジャイロと加速度の最大範囲を指定
141
+ Wire.beginTransmission(0b1101000); //I2C address of the MPU
142
+ Wire.write(0x6B); //Accessing the register 6B
143
+ Wire.write(0b00000000); //SLEEP register to 0
144
+ Wire.endTransmission();
145
+ Wire.beginTransmission(0b1101000);
146
+ Wire.write(0x1B); //Accessing the register 1B - Gyroscope Configuration
147
+ Wire.write(0x00000000); //gyro to full scale ± 250deg./s
148
+ Wire.endTransmission();
149
+ Wire.beginTransmission(0b1101000);
150
+ Wire.write(0x1C); //Accessing the register 1C - Acccelerometer Configuration
151
+ Wire.write(0b00000000); //accel to +/- 2g
152
+ Wire.endTransmission();
153
+ }
154
+
155
+ void recordAccelRegisters() {
156
+ //加速度読み取り
157
+ Wire.beginTransmission(0b1101000);
158
+ Wire.write(0x3B); // Accel Readings
159
+ Wire.endTransmission();
160
+ Wire.requestFrom(0b1101000, 6); //Request Accel Registers (3B - 40)
161
+ while (Wire.available() < 6);
162
+ accelX = Wire.read() << 8 | Wire.read(); //Store first two bytes into accelX
163
+ accelY = Wire.read() << 8 | Wire.read(); //Store middle two bytes into accelY
164
+ accelZ = Wire.read() << 8 | Wire.read(); //Store last two bytes into accelZ
165
+ calculateAccelData();
166
+ }
167
+
168
+ void calculateAccelData() {
169
+ //読み取った値をgに変換
170
+ gForceX = accelX / 16384.0;
171
+ gForceY = accelY / 16384.0;
172
+ gForceZ = accelZ / 16384.0;
173
+ }
174
+
175
+ void recordGyroRegisters() {
176
+ //ジャイロの値を読み取る
177
+ Wire.beginTransmission(0b1101000); //I2C address of the MPU
178
+ Wire.write(0x43); //Starting register for Gyro Readings
179
+ Wire.endTransmission();
180
+ Wire.requestFrom(0b1101000, 6); //Request Gyro Registers (43 - 48)
181
+ while (Wire.available() < 6);
182
+ gyroX = Wire.read() << 8 | Wire.read(); //Store first two bytes into accelX
183
+ gyroY = Wire.read() << 8 | Wire.read(); //Store middle two bytes into accelY
184
+ gyroZ = Wire.read() << 8 | Wire.read(); //Store last two bytes into accelZ
185
+ calculateGyroData();
186
+ }
187
+
188
+ void calculateGyroData() {
189
+ //読み取った値をdeg/secに変換
190
+ rotX = gyroX / 131.0;
191
+ rotY = gyroY / 131.0;
192
+ rotZ = gyroZ / 131.0;
193
+ }
194
+ void printData() {
195
+ //シリアルモニタに出力
196
+ Serial.print("Gyro");
197
+ Serial.print(" X=");
198
+ Serial.print(rotX);
199
+ Serial.print("deg/sec");
200
+ Serial.print(" Y=");
201
+ Serial.print(rotY);
202
+ Serial.print("deg/sec");
203
+ Serial.print(" Z=");
204
+ Serial.print(rotZ);
205
+ Serial.print("deg/sec");
206
+ Serial.print(" Accel");
207
+ Serial.print(" X=");
208
+ Serial.print(gForceX);
209
+ Serial.print('g');
210
+ Serial.print(" Y=");
211
+ Serial.print(gForceY);
212
+ Serial.print('g');
213
+ Serial.print(" Z=");
214
+ Serial.print(gForceZ);
215
+ Serial.println('g');
216
+ }
217
+
218
+ File dataFile =SD.open("datalog.txt",FILE_WRITE);
219
+ if (dataFile){
220
+
221
+ dataFile.print("Gyro");
222
+ dataFile.print(" X=");
223
+ dataFile.print(rotX);
224
+ dataFile.print("deg/sec");
225
+ dataFile.print(" Y=");
226
+ dataFile.print(rotY);
227
+ dataFile.print("deg/sec");
228
+ dataFile.print(" Z=");
229
+ dataFile.print(rotZ);
230
+ dataFile.print("deg/sec");
231
+ dataFile.print(" Accel");
232
+ dataFile.print(" X=");
233
+ dataFile.print(gForceX);
234
+ dataFile.print('g');
235
+ dataFile.print(" Y=");
236
+ dataFile.print(gForceY);
237
+ dataFile.print('g');
238
+ dataFile.print(" Z=");
239
+ dataFile.print(gForceZ);
240
+ dataFile.println('g');
241
+ dataFile.close();
242
+ lcd.setCursor(0,1);
243
+ lcd.printf("CD write=");
244
+ lcd.print(sno);
245
+ lcd.setCursor(15,1);
246
+ lcd.printf("R");
247
+ }
248
+ else{
249
+ lcd.printf("error opening datalog.txt");
250
+ }
251
+ }
252
+
253
+
254
+ }
255
+
256
+ このようにプログラムしたのですが、
257
+ if (dataFile){
258
+ の行がエラー文が出てしまいます。