2 analog input loops, problem with time

I’m trying to write a code that takes in two different analog input signals. I would like to have the code output 3 columns of data; the two signals and then time. I need to know at what time each analog signal comes into the Arduino for data acquisition purposes. The dream was to then have digital outputs to a rasberry pi, but due to practically no knowledge of the Raspi, me and my team are abandoning that idea and are settling with putting the data on an sd card on the Uno.

I’m fairly new to Arduino coding, but this is what I have so far…

#include <SD.h>

File file;

int val1;
int val2;
int volts1;
int volts2;
int sdcard = 4;

void setup (){
pinMode (0,INPUT);
pinMode (1,INPUT);
pinMode (sdcard,OUTPUT);
Time= millis();
!SD.begin (sdcard);

void loop (){
file=SD.open (“file.txt” , FILE_WRITE);


if (volts1 > 2.5)


if (volts2 > 2.5)
file.println (0)
file.println (1)

file.println (Time);
file.Close ();


Will this code produce three columns of data on the sd card? The first two columns should be zeros and ones, depending on if the arduino sensed the signal( around 3 volts) Also, will the two analog read and division lines take more than a millisecond? Thanks for the help

No. Data doesn't get stored in a file in rows and columns. It goes in typically as sequential binary data or delimited ASCII. How it gets printed/displayed is up to you or the program that retrieves it.

With regard to the speed, I've read that a write cycle takes 20ms, but the data is written in blocks so if you wait until you have a block full of data it may not be an issue.

ok, thanks. Did you see any major hickups in my code? I was wonder if it was possible to move the following line to the void setup, to save processing time each time the loop is run:

"file=SD.open ("file.txt" , FILE_WRITE);"

If that is possible, what would I do about the line that closes the sd card? One of the inputs is an analog signal coming from a inductive spark plug sensor with a max frequency of 60 cycles per second. The other analog input is about the same frequency. Do you think the Uno can process my void loop in right around one millisecond?

Yes, move your SD open to setup. You will need some test condition that closes the file. Before to many more questions are asked, please post your code according to the guidelines, it makes life easier.

You declare digital pins 0 and 1 as inputs but you never use them. Those pins are always more useful as Serial in 99% of Arduino projects.

analogRead() will make any analog pin an input when it is called but usually you would put pinMode(PIN_SENSOR_NAME,INPUT); in setup() to make it clear.

Practise by printing the data to both Serial and the SD card. When it looks good to the Serial Monitor it will be good on the card.

Next time use code tags. The forum software eats some of your code if you don't.


if (volts1 > 2.5)

The value in val1 will range from 0 to 1023. Dividing that by 1040 will result in a range from 0 to 0, because val1 is in int. volts1 is an int, too, so comparing the value in it to 2.5 does not make sense.

Even if you divide by 1040.0, so you get a fractional part, the result will still be in the range 0.0 to 0.98365, which is not greater than 2.5.

Put out whatever you are smoking, and explain why you wrote the code that way.