Forum: Mikrocontroller und Digitale Elektronik Speichern von verschiedenen Messwerten auf SD-Karte


von Andreas G. (gandy)


Lesenswert?

Hallo Community,

bin Arduinoneuling und hab ein Problem beim Speichern meiner Daten auf 
die SD-Karte. Ich benutze einen Arduino Uno, ADXL345 und Ultimate GPS 
Datenlogger Shield (beide Adafruit)

Obwohl ich schon viel im Internet gesucht und gefunden habe, konnte ich 
mein Problem nicht lösen.

in meinem Seriellen Monitor erhalte ich die gewünschten Daten von meinen 
Shield und B.Sensor.

Nun würde ich diese gerne auf die SD-Karte speichern. Dazu verwende ich 
die SD-lib. von Adafruit.

Mein Code:
1
 
2
3
#include <SPI.h>
4
#include <SD.h>
5
6
#include <Wire.h>
7
#include <Adafruit_Sensor.h>
8
#include <Adafruit_ADXL345_U.h>
9
10
#include <Adafruit_GPS.h>
11
#include <SoftwareSerial.h>
12
13
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);
14
15
// If you're using a GPS module:
16
// Connect the GPS Power pin to 5V
17
// Connect the GPS Ground pin to ground
18
// If using software serial (sketch example default):
19
//   Connect the GPS TX (transmit) pin to Digital 3
20
//   Connect the GPS RX (receive) pin to Digital 2
21
// If using hardware serial (e.g. Arduino Mega):
22
//   Connect the GPS TX (transmit) pin to Arduino RX1, RX2 or RX3
23
//   Connect the GPS RX (receive) pin to matching TX1, TX2 or TX3
24
25
// If you're using the Adafruit GPS shield, change
26
// SoftwareSerial mySerial(3, 2); -> SoftwareSerial mySerial(8, 7);
27
// and make sure the switch is set to SoftSerial
28
29
// If using software serial, keep this line enabled
30
// (you can change the pin numbers to match your wiring):
31
SoftwareSerial mySerial(8, 7);
32
33
// If using hardware serial (e.g. Arduino Mega), comment out the
34
// above SoftwareSerial line, and enable this line instead
35
// (you can change the Serial number to match your wiring):
36
37
//HardwareSerial mySerial = Serial1;
38
39
const int chipSelect = 10;
40
File dataFile;
41
42
Adafruit_GPS GPS(&mySerial);
43
44
45
// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
46
// Set to 'true' if you want to debug and listen to the raw GPS sentences.
47
#define GPSECHO  true
48
49
// this keeps track of whether we're using the interrupt
50
// off by default!
51
boolean usingInterrupt = false;
52
void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy
53
54
void setup()
55
{
56
57
  // connect at 115200 so we can read the GPS fast enough and echo without dropping chars
58
  // also spit it out
59
  Serial.begin(115200);
60
61
62
63
64
65
  // SD CARD
66
  
67
  // Open serial communications and wait for port to open:
68
  //Serial.begin(9600);
69
   while (!Serial) {
70
    ; // wait for serial port to connect. Needed for Leonardo only
71
  }
72
   // SD CARD
73
  Serial.print("Initializing SD card...");
74
  // make sure that the default chip select pin is set to
75
  // output, even if you don't use it:
76
  pinMode(SS, OUTPUT);
77
  
78
  // see if the card is present and can be initialized:
79
  if (!SD.begin(chipSelect)) {
80
    Serial.println("Card failed, or not present");
81
    // don't do anything more:
82
    while (1) ;
83
  }
84
  Serial.println("card initialized.");
85
  
86
  // Open up the file we're going to log to!
87
  dataFile = SD.open("datalog.txt", FILE_WRITE);
88
  if (! dataFile) {
89
    Serial.println("error opening datalog.txt");
90
    // Wait forever since we cant write data
91
    while (1) ;
92
  }
93
94
95
96
97
  // BESCHLEUNIGUNGSSENSOR
98
99
  /* Initialise the sensor */
100
  if (!accel.begin())
101
  {
102
    /* There was a problem detecting the ADXL345 ... check your connections */
103
    Serial.println("Ooops, no ADXL345 detected ... Check your wiring!");
104
    while (1);
105
  }
106
107
108
109
110
  // GPS
111
  // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
112
  GPS.begin(9600);
113
114
  // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
115
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
116
  // uncomment this line to turn on only the "minimum recommended" data
117
  // GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
118
  // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
119
  // the parser doesn't care about other sentences at this time
120
121
  // Set the update rate
122
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   // 1 Hz update rate
123
  // For the parsing code to work nicely and have time to sort thru the data, and
124
  // print it out we don't suggest using anything higher than 1 Hz
125
126
  // Request updates on antenna status, comment out to keep quiet
127
  GPS.sendCommand(PGCMD_ANTENNA);
128
129
  // the nice thing about this code is you can have a timer0 interrupt go off
130
  // every 1 millisecond, and read data from the GPS for you. that makes the
131
  // loop code a heck of a lot easier!
132
  useInterrupt(true);
133
134
  delay(1000);
135
  // Ask for firmware version
136
  mySerial.println(PMTK_Q_RELEASE);
137
}
138
139
140
// Interrupt is called once a millisecond, looks for any new GPS data, and stores it
141
SIGNAL(TIMER0_COMPA_vect) {
142
  char c = GPS.read();
143
  // if you want to debug, this is a good time to do it!
144
#ifdef UDR0
145
  if (GPSECHO)
146
    if (c) UDR0 = c;
147
  // writing direct to UDR0 is much much faster than Serial.print
148
  // but only one character can be written at a time.
149
#endif
150
}
151
152
void useInterrupt(boolean v) {
153
  if (v) {
154
    // Timer0 is already used for millis() - we'll just interrupt somewhere
155
    // in the middle and call the "Compare A" function above
156
    OCR0A = 0xAF;
157
    TIMSK0 |= _BV(OCIE0A);
158
    usingInterrupt = true;
159
  } else {
160
    // do not call the interrupt function COMPA anymore
161
    TIMSK0 &= ~_BV(OCIE0A);
162
    usingInterrupt = false;
163
  }
164
}
165
166
uint32_t timer = millis();
167
void loop()                     // run over and over again
168
{
169
170
  /* Get a new sensor event */
171
  sensors_event_t event;
172
  accel.getEvent(&event);
173
174
175
  // SD CARD - ABSPEICHERN
176
177
  // make a string for assembling the data to log:
178
179
  String dataString = "";
180
  int x =(event.acceleration.x);
181
  int y =(event.acceleration.y);
182
  int z =(event.acceleration.z);
183
184
//  dataString += String(x, y, z); funktioniert nicht! 
185
186
187
  
188
189
  dataFile.println(dataString);
190
191
  // print to the serial port too:
192
  Serial.println(dataString);
193
  
194
  // The following line will 'save' the file to the SD card after every
195
  // line of data - this will use more power and slow down how much data
196
  // you can read but it's safer! 
197
  // If you want to speed up the system, remove the call to flush() and it
198
  // will save the file only every 512 bytes - every time a sector on the 
199
  // SD card is filled with data.
200
  dataFile.flush();
201
202
203
  
204
  // BESCHLEUNIGUNGSSENSOR 
205
206
  // if a sentence is received, we can check the checksum, parse it...
207
  if (GPS.newNMEAreceived()) {
208
    
209
  //  if (GPS.fix) {
210
211
      /* Display the results (acceleration is measured in m/s^2)  
212
       */
213
      Serial.print(event.acceleration.x); Serial.print("; ");
214
      Serial.print(event.acceleration.y); Serial.print("; ");
215
      Serial.print(event.acceleration.z); Serial.print("; "); Serial.println(" ");
216
      delay(1000);
217
218
      
219
220
   }   
221
  }

Jedoch ohne Erfolg, ich hoffe ihr könnt mir Tipps geben

Vielen Dank

Gandy

von Alex R. (itaxel)


Lesenswert?

Andreas G. schrieb:
> dataString += String(x, y, z);

????????
Schau dir mal die Referenz zum String Objekt an.

Probiers mal so:
dataString = String(x + ',' + y + ',' + z);

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.