i am a noob at programming but i just wanted to share this one possible good reason to use goto, i wanted the data logger to cease measurements after a few minutes because i have written a template in excel formulas, graphs etc that take far too long to load if the text file i import data from is large. anyway i have to give others the opportunity to laugh about it will prob be even funnier when i get more experience with C:
#include <SD.h>
File myFile;
const int chipSelect = 4;
void setup()
{
Serial.begin(9600);
while (!Serial) {
}
Serial.print("Initializing SD card...");
pinMode(53, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}else{ Serial.println("card initialized.");}
Serial.println("Creating data01.txt...");
myFile = SD.open("data01.txt", FILE_WRITE);
myFile.close();
}
void loop()
{
String dataString = "";
for (int analogPin = 2; analogPin < 8; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 7) {
dataString += " ";
}
}
File dataFile = SD.open("data01.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}
long t1 = long(millis());
if(t1>long(240000)){dataFile.close(); goto FINISH;}
else{return;}
FINISH:
{ Serial.println("DATA COLLECTION COMPLETE");
goto START;
}
START:
{
goto FINISH;}
}
system
August 10, 2014, 6:27pm
2
Laugh?
I thought I'd never start.
I took the liberty of applying the auto-format tool to your code - maybe it'll help you visualise the program flow.
#include <SD.h>
File myFile;
const int chipSelect = 4;
void setup()
{
Serial.begin(9600);
while (!Serial) {
}
Serial.print("Initializing SD card...");
pinMode(53, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
else{
Serial.println("card initialized.");
}
Serial.println("Creating data01.txt...");
myFile = SD.open("data01.txt", FILE_WRITE);
myFile.close();
}
void loop()
{
String dataString = "";
for (int analogPin = 2; analogPin < 8; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 7) {
dataString += " ";
}
}
File dataFile = SD.open("data01.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}
long t1 = long(millis());
if(t1>long(240000)){
dataFile.close();
goto FINISH;
}
else{
return;
}
FINISH:
{
Serial.println("DATA COLLECTION COMPLETE");
goto START;
}
START:
{
goto FINISH;
}
}
The goto-less version is even shorter.
cool but im reading the same code i think you accidently pasted my original code
system
August 10, 2014, 7:18pm
4
im reading the same code i think you accidently pasted my original code
No, I very deliberately pasted your code, but after formatting it to highlight the program flow.
Nothing at all accidental.
but in reference to the goto-less version, ie the code absent of the goto function i can use for future reference to cease the SD output
system
August 10, 2014, 7:32pm
6
I don't understand your last post.
The goto-less version is even shorter.
i am asking what that version is
system
August 10, 2014, 7:35pm
8
It's almost the same as your code, but it is shorter, and it doesn't have any "goto"s or labels in it.
Or explicit "return"s
pfff fine ill do it tommorow