Alright, I have this program that takes outputs from sensors and prints it to an SD card. Because of some issues with how the SD card works I have to write all the data to it at the end of the program. They way I thought of doing this is to store the values in an array and then print that array at the end. In this example I just want it to print the first integer in the array. For some reason it writes 3 zeros instead of the integer's I want it to.
//******** Libraries ***********
#include <SparkFun_Qwiic_Relay.h>
#include <U8glib.h>
#include "SparkFun_Qwiic_Relay.h"
#include "U8glib.h"
#include <Wire.h>
#include <SoftwareSerial.h>
#define rx 2
#define tx 3
#include "TSYS01.h"
#include "MS5837.h"
#define RELAY_ADDR 0x18
#include <SPI.h>
#include <SD.h>
//*******************************
TSYS01 sensor1;
MS5837 sensor0;
SoftwareSerial myserial(rx, tx);
Qwiic_Relay relay(RELAY_ADDR);
File myFile;
int temppos = 0;
int depthpos = 0;
int phpos = 0;
int depthdata [1] = {};
int tempdata [1] = {};
int phdata [1] = {};
float relaytime = 0;
int sensortiming = 0;
String inputstring = "";
String sensorstring = "";
boolean input_string_complete = false;
boolean sensor_string_complete = false;
float pH = 0;
void setup() {
SD.remove("Output.txt");
Serial.begin(9600);
myserial.begin(9600);
inputstring.reserve(10);
sensorstring.reserve(30);
Wire.begin();
sensor1.init();
while (!sensor0.init()) {
Serial.println("Init failed!");
Serial.println("Are SDA/SCL connected correctly?");
Serial.println("Blue Robotics Bar30: White=SDA, Green=SCL");
Serial.println("\n\n\n");
delay(5000);
}
if(!relay.begin()) {
Serial.println("Check connections to Qwiic Relay.");
} else {
Serial.println("Ready to flip some switches.");
float version = relay.singleRelayVersion();
Serial.print("Firmware Version: ");
Serial.println(version);
}
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
sensor0.setModel(MS5837::MS5837_30BA);
sensor0.setFluidDensity(997); // kg/m^3 (freshwater, 1029 for seawater)
}
void serialEvent() {
inputstring = Serial.readStringUntil(13);
input_string_complete = true;
}
void loop() {
if (relaytime >= 10) {
relay.turnRelayOn();
delay(1000);
relay.turnRelayOff();
delay(1000);
relay.turnRelayOn();
delay(1000);
relay.turnRelayOff();
relaytime = -10; //The time it takes for the relay to execute again (in seconds)
myFile = SD.open("Output.txt", FILE_WRITE);
Serial.println(phdata [0]);
myFile.println(phdata [0]);
myFile.println(tempdata [0]);
Serial.println(tempdata [0]);
myFile.println(depthdata [0]);
Serial.println(depthdata [0]);
myFile.close();
}
if (sensortiming == 1000) {
sensor1.read();
sensor0.read();
Serial.print("Depth: ");
Serial.print(sensor0.depth());
Serial.println(" m");
depthdata [depthpos] = sensor0.depth();
depthpos = depthpos+1;
Serial.print("Temperature: ");
Serial.print(sensor1.temperature());
Serial.println(" deg C");
tempdata [temppos]= sensor1.temperature();
temppos = temppos+1;
Serial.println("");
sensortiming = 0;
}
if (input_string_complete == true) {
myserial.print(inputstring);
myserial.print('\r');
inputstring = "";
input_string_complete = false;
}
if (myserial.available() > 0) {
char inchar = (char)myserial.read();
sensorstring += inchar;
if (inchar == '\r') {
sensor_string_complete = true;
}
}
if (sensor_string_complete == true) {
Serial.print("pH: ");
Serial.println(sensorstring);
if (isdigit(sensorstring[0])) {
pH = sensorstring.toFloat();
}
phdata [phpos] = pH;
phpos = phpos+1;
sensorstring = "";
sensor_string_complete = false;
}
delay(1);
sensortiming = sensortiming+1;
relaytime = relaytime+0.001;
}
depthdata [depthpos] = sensor0.depth();
depthpos = depthpos+1;
Let's hope depthpos doesn't get too big. Like, 1.
int depthdata [1] = {};
An array that can only hold 1 value is useless and, as noted in reply #1, potentially dangerous to the running of the program
I originally had it so it would have an undetermined length by leaving the brackets blank, but they didn't work for some reason. I also tried to set the array length at 10 and it didn't write to the SD card.
Stockbridge_InvenTeam:
Alright, I have this program that takes outputs from sensors and prints it to an SD card. Because of some issues with how the SD card works I have to write all the data to it at the end of the program. They way I thought of doing this is to store the values in an array and then print that array at the end. In this example I just want it to print the first integer in the array. For some reason it writes 3 zeros instead of the integer's I want it to.//******** Libraries ***********#include <SparkFun_Qwiic_Relay.h>#include <U - Pastebin.com
I thought this looked familiar
I originally had it so it would have an undetermined length by leaving the brackets blank
In C/C++ an array must always be of a fixed length
I also tried to set the array length at 10 and it didn't write to the SD card.
I assume that you iterated through the array when you tried that. Please post the code that you tried.
EDIT : corrected grammar
If memory were not an issue a C++ vector would be ideal for this.