arduino-sketch.ino 785 B

12345678910111213141516171819202122
  1. // https://www.arduino.cc/reference/en/language/functions/time/micros/
  2. const unsigned long SAMPLE_INTERVAL_MICROSECONDS = 2500L;
  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. Serial.write(reading >> 8);
  18. Serial.write(reading & 0xff);
  19. }