Go Down

Topic: Stop watch for multiple triggers writing to SD card on Arduino UNO (Read 1 time) previous topic - next topic

jphigby


I have searched the internet, this forum and Jeromy Blums tutorials and have compiled a sketch that will  display elapsed time in total minutes and total seconds from the time the Arduino Uno is powered up, until pin 3 is connected to 5 v. I am using a SD card shield and all is saved on the SD card. I know the sketch is not very good and much does not need to be in the sketch, but it does what I need. This is the information I get on the SD card if I connect pin 3 to 5 volt after 5 minutes and 10 seconds for example.

Start test
Pressure drop detected at min. and sec.
5
310

I would like to be able to time also when pin 4, 5, and 6 are connected to 5 v. and I have tried to dublicate the loop to incorporate more pins but I cannot get it to work. I get all sorts of different results e.g. triggering timer at  0 sec. although it should not have triggered at all. I would be very greatful for all help in doing this and helping me with the sketch.  Johan, Stockholm Sweden

The complete sketch is included below:

/*
Stopwatch using millis();
Based on sketch by Tom Igoe andJohn Boxall.
This sketch is in the public domain.

The program starts at startup and displays elapsed
time when D3 is connected to 5 v. Pull-down resistor 10 k
between D3 and ground.
LED between D7 and GND will light
up as D3 is connected to 5v.

*/

#include<SD.h>
//SD library comes included with the Arduino IDE

//Set by default for the SD Card Library
//MOSI = Pin 11
//MISO = Pin 12
//SCLK = PIN 13
//We always need to set the CS Pin
int CS_pin = 10;
int pow_pin = 8;
const int ledPin =  7;      // the number of the LED pin

unsigned long start, finished, elapsed;

void setup()
{
Serial.begin(9600);

pinMode(3, INPUT); // stop function
Serial.println("Initializing Card");
     

//CS Pin is an output
pinMode(CS_pin, OUTPUT);
 
//Card will Draw Power from Pin 8, so set it high
pinMode(pow_pin, OUTPUT); 
digitalWrite(pow_pin, HIGH);
 
if (!SD.begin(CS_pin))
{
Serial.println("Card Failure");
return;
}
Serial.println("Card ready");
Serial.println("Start test");
File dataFile = SD.open("log.txt", FILE_WRITE);
if (dataFile)
{
String dataString = "Start test";
 
dataFile.println(dataString);
dataFile.close();
}
 


 
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);     
}
void displayResult()
{
float h,m,s,ms;
unsigned long over;
elapsed=finished-start;
h=int(elapsed/3600000);
over=elapsed%3600000;
m=int(over/60000);
over=over%60000;
s=int(over/1000);
ms=over%1000;

Serial.print("End test: ");
Serial.print(h,0);
Serial.print("h ");
Serial.print(m,0);
Serial.print("m ");
Serial.print(s,0);
Serial.print("s ");
Serial.print(ms,0);
Serial.println("ms");
Serial.println();
 
{
start=millis();
delay(200); // for debounce
   
}
 
}

void loop()
{

 
if (digitalRead(3)==HIGH)

{
finished=millis();
delay(200); // for debounce
displayResult();
   
File dataFile = SD.open("log.txt", FILE_WRITE);
String dataString = "Pressure drop detected at min. and sec.";
 
dataFile.println(dataString);

dataFile.println(finished/1000/60);
   
dataFile.println(finished/1000);
   
dataFile.close();
   


//turn LED on:
digitalWrite(ledPin,HIGH);
{
delay(800000);
}
}

}


PaulS

Code: [Select]
if (dataFile)
{
String dataString = "Start test";

dataFile.println(dataString);
dataFile.close();
}

A huge waste of resources here. Wrap some data in a String. Pass that String to a function that then needs to unwrap the data to print it. dataFile.println("Start test"); would be far simpler, faster, AND result in a smaller sketch. dataFile.println(F("Start test")); would be even better.

Code: [Select]
String dataString = "Pressure drop detected at min. and sec.";

dataFile.println(dataString);

More resources pissed away uselessly.

Code: [Select]
{
delay(800000);
}

There goes any responsiveness you might be hoping for. {Do} {those} {curly} {braces} {serve} {some} {purpose} {for} {you}?

There is nothing in that incorrectly posted code that concerns itself with other pins. Seeing what you tried is the only way to determine what you did wrong.

Go Up