I want to either use a delay or preferably an interval timing, similar to the Arduino blink without delay tutorial, on a RPi board.
What function would I need to to insert into my loop to have an interval of 200 microseconds?
Code: Select all
#include <SD.h> //Includes the SD library
//=== Governing test conditions ===//
const char textFile [] = "Data.txt"; //Defines the file name for the data
const byte timeOfTest = 3; //Sets the length of time that data will be written for in minutes
const byte testDataReadingInterval = 150; //Sets interval between readings in microseconds
//=== Define variables used with the SD card ===//
const byte bodyGaugePin[] = {A0, A1, A2, A3, A4, A5, A6, A7}; //Defines the analog inputs
const byte legGaugePin[] = {A8, A9, A10, A11, A12, A13, A14, A15}; //Defines the analog inputs
int SdCsPin = 53; //SD card chip select pin
//=== Initial setup ===//
void setup()
{
Serial.begin(115200); //Initialises Serial bus connection with 115200 a Buad rate
//This tells Arduino that all input signals can move over a range of 1.1 V; this will be written
analogReference(INTERNAL1V1); //in 10-bit resolution (2^10 = 1024; [1.1 / 1024 = 1 mV per bit])
Serial.println("Initialising Card");
//Chip Select Pin is an output
pinMode(SdCsPin, OUTPUT);
if (!SD.begin(SdCsPin)) // This states that if the SD card function has not started, then print text
{
Serial.println("Card Failure");
Serial.println("Check SD card is inserted");
while (!Serial.available()) {}
}
Serial.println("Card Ready");
Serial.println();
Serial.println("Begin writing test data");
Serial.print("Data will be logged for ");
Serial.print(timeOfTest);
Serial.println(" minutes");
}
//=== Writes test data to SD card ===//
void WriteTestData() {
// Converts the digital bit reading back into mV for body gauges
int BG_V1 = ((4.96 / 1024) * (analogRead(bodyGaugePin[0]) + 1.0)) * 1000;
int BG_V8 = ... // This was previously used, but the ADC chips will write to the digital pins now
// Converts the digital bit reading back into mV for leg gauges
int LG_V1 = ((4.96 / 1024) * (analogRead(legGaugePin[0]) + 1.0)) * 1000;
int LG_V8 = ...
File testData = SD.open(textFile, FILE_WRITE); // Opens the text file to write data
if (testData) {
testData.print(abs(BG_V1)); // This determines which inputs will be logged
testData.print(" "); //
testData.println(abs(BG_V8));
testData.close();
}
else {
Serial.println("Cannot write data to SD card"); //If data is unable to write, an error will appear
while (!Serial.available()) {}
}
}
//=== Main program loop ===//
void loop() {
unsigned long time = micros();
if (time <= timeOfTest * 60000000) { // Sets how long test will run for in microseconds, 60000000 = 60 seconds
WriteTestData();
}
else {
Serial.println();
Serial.println("End of data collection");
Serial.println("Press upload to collect more data");
while (1) {}
}
delayMicroseconds(testDataReadingInterval); // time between readings
}