// Reads analog input and sends over serial. // Reports the time the input was sampled. // Uses precise timing via interrupts. // Requires > 57600 baud serial. // The Arduino clock frequency is 1 cycle 6.25e-8 seconds. // Timer1 is 16 bits. It overflows every (2^16 - 1) * (6.25e-8 seconds). // That is, every 4.0959375 milliseconds. So that's our sampling time. // We get reliable 4 ms samples if we use > 28800 baud serial. Lower baud rate doesn't work. // avr-libc library includes #include #include void setup() { // initialize serial communication Serial.begin(57600); // Initialize Timer1 cli(); //disable global interrupts TCCR1A = 0; TCCR1B = 0; // Enable Timer1 overflow interrupt: TIMSK1 = (1 << TOIE1); // Set CS10 bit so timer runs at clock speed: TCCR1B |= (1 << CS10); sei(); // enable global interrupts } ISR(TIMER1_OVF_vect){ // Serial.println(millis()); Serial.println(analogRead(A0)); } void loop() { // do nothing since the script is interrupt driven }