Hi, quick post. I'm trying to log data from two analog inputs called with analogRead and their inputs printed every millisecond. I also am printing the time in millis besides them for a time reference. I'm getting data on the sd card but its every 11 milliseconds. Anyhelp would much be appreciated. Here's my code:
#include <SD.h> #include <SPI.h>
const int chipselect = 4;
File mysd;
int val;
int val2;
int volts;
int volts2;
unsigned long Time;
if (mysd){
val = analogRead(A0);
volts = (val/1040);
val2 = analogRead(A1);
volts2 = (val2/1040);
if (volts > 2.5){
mysd.print("1,");}
else{
mysd.print("0,");}
if (volts2 > 2.5){
mysd.print("1,");}
else{
mysd.print("0,");}
mysd.println(Time);
mysd.close();
}
}
How do I speed it up to every millisecond. I actually really need it at .5 millisecond speed but I'll settle at 1 millisecond. Btw, I'm using a sd shield that goes ontop of my Uno. Thanks!
Mm, I don't know if you can. analogRead() already takes some time. You might want to look into that. Same for SPI speed of the SD library. But the biggest thing, why do you keep opening and closing the damn file if you want to write fast? If you want to make a grocery list fast, do you put the note book back into the drawer after every letter?
Couple of remakes:
-Please use code tags next time
if (volts2 > 2.5){
You do know volts2 is an int and int's don't have decimals. Aka, that line is the same as:
ok ok lol. I was wondering that about putting opening the sd in the void setup. Ill try that. I made a simpler code to test the speed that just had a single println. in the void loop and it got the speed down to 6 millis but not 1 milli. Is it the sd card that is my problem?
jremington:
What is your thinking here? Do you not realize that the integer divide will always give 0 as a result?
val = analogRead(A0);
volts = (val/1040);
Missed that! :o
Mixing float in is not going to make it faster. Don't let the Arduino do math just because you need understandable units. Of at least do it the other way around so the compiler can do the heavy calculation:
if(analogRead(TestPin) >= (1024 * 2.5 / 5)){
But if you just want to detect >2,5V the analog comparator is faster.