arduino-sketch.ino 852 B

1234567891011121314151617181920212223
  1. // https://www.arduino.cc/reference/en/language/functions/time/micros/
  2. const unsigned long SAMPLE_INTERVAL_MICROSECONDS = 2000L;
  3. void setup() {
  4. // $ stty -F /dev/ttyUSB0 | grep speed
  5. Serial.begin(115200);
  6. }
  7. unsigned long last_sample_micros = 0;
  8. void loop() {
  9. while (micros() - last_sample_micros < SAMPLE_INTERVAL_MICROSECONDS)
  10. ;
  11. last_sample_micros = micros();
  12. // > On ATmega based boards [...], it takes about 100 microseconds
  13. // > (0.0001 s) > to read an analog input, [...]
  14. // https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/
  15. unsigned short reading = analogRead(A0);
  16. // $ tail --follow /dev/ttyUSB0 | od --format=u2 --endian=big --width=2
  17. // 16 bits/sample / 115200 Baud * 1e6 μs/sec = 139 μs/sample
  18. Serial.write(reading >> 8);
  19. Serial.write(reading & 0xff);
  20. }