/* -*- c-mode -*- */ /* * Print the output of a sonar rangefinder on analog input 1. * A machine on the other end can read the serial output * and do something with it. * * by Akkana Peck * This is Free Software: share and enjoy under the terms of the * GNU Public License. */ #include int sensorPin = 1; void setup() { pinMode(sensorPin, INPUT); Serial.begin(9600); } void loop() { while (1) { /* Read 8 values and average them, in case of spurious readings */ float val = 0; for (int i=0; i<8; i++) { val += analogRead(sensorPin); delay(50); } val /= 8; Serial.println(val); delay(1000); } } /* Various sites say to add this to get around the missing symbol when * including Serial.println. No idea why it's an infinite loop. * The error otherwise is applet/core.a(Print.o):(.data+0x6): undefined reference to `__cxa_pure_virtual' * and comes from Print.h: virtual void write(uint8_t) = 0; */ extern "C" void __cxa_pure_virtual() { while (1) ; }