// Sampling Speed Test writing via Serial or NullPrint /** (c)2016 Forward Computing and Control Pty. Ltd. www.forward.com.au This code may be freely used for both private and commercial use. Provide this copyright is maintained. */ // uncomment this line if using Adafruit WICED //#define USE_WICED #ifdef USE_WICED int ADC_pin = PC3; // WICED, Adafruit WICED use PC3, WICED has odd pin names #else int ADC_pin = A0; // Uno/Mega/101 #endif // set to true to just throw output away, set to false to send to Serial boolean useNullPrint = true; // try 115200 versus 9600 when sending to Serial const unsigned long baudRate = 115200; // note this is an unsigned long value unsigned long uS = 0; long loopCounter = 0; long lastMillis = 0; long dataCounter = 0; long loop1000time = 0; int sampledData = 0; float dataOut = 0.0; int testCounter = 0; // Create dummy Print class for printing the results to nothing // This removes transfer delays to the communication shield from the measurement // class NullPrint: public Print { size_t write(uint8_t); }; size_t NullPrint::write(uint8_t b) { return 1; } NullPrint nullOutput; void setup(void) { Serial.begin(baudRate); for (int i = 10; i > 0; i--) { // add a little delay, to give you time to open the serial monitor delay(1000); Serial.print(" "); Serial.print(i); } Serial.println(); #ifdef USE_WICED pinMode(PC3, INPUT_ANALOG); // special pinMode for WICED #endif // set the timer lastMillis = millis(); } void loop(void) { loopCounter++; if ((loopCounter % 1000) == 0) { long ms = millis(); if (useNullPrint) { Serial.print("Using NullPrint"); } else { Serial.print("Using Serial at "); Serial.print(baudRate); } Serial.print(" 1000 loops in:"); loop1000time = ms - lastMillis; Serial.print(loop1000time); Serial.print("mS"); Serial.print(" -> Sample rate:"); Serial.print((1000.0 / loop1000time)); Serial.println("k samples/sec"); Serial.println("Example data record:"); // print a sample of the data record sendData(&Serial); lastMillis = millis(); // restart time after printing testCounter++; if (testCounter > 5) { // stop here to view results while (1) {}; } } // get the data and the time // replace these lines with your own measurement code sampledData = analogRead(ADC_pin); uS = micros(); // prepare and print the data record time , data // do any data scaling and timestamp preperation here #ifdef USE_WICED dataOut = ((float) sampledData) / 4096.0; #else dataOut = ((float) sampledData) / 1024.0; #endif if (useNullPrint) { sendData(&nullOutput); } else { sendData(&Serial); } } //end main loop // change this method to send your data void sendData(Print * output) { output->print(uS); output->print(','); output->print(dataOut); output->println(); }