// Sampling Speed Test writing via Arduino Ethernet Shield (SPI interface) /** (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. */ #include #include int ADC_pin = A0; // Uno/Mega/101 // 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; // Enter a MAC address and IP address for your controller below. // The IP addresses will be dependent on your local network. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(10, 1, 1, 100); const int portNo = 23; IPAddress gateway(10, 1, 1, 1); IPAddress subnet(255, 255, 255, 0); EthernetServer server(portNo); EthernetClient client; 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(); // initialize the ethernet device Ethernet.begin(mac, ip, gateway, subnet); // start listening for clients server.begin(); // initialize client client = server.available(); // evaluates to false if no connection // set the timer lastMillis = millis(); } void loop(void) { while ((!client) || (!client.connected())) { client = server.available(); if (client.connected()) { Serial.println("Got connection"); lastMillis = millis(); } else { client.stop(); Serial.println("Waiting for connection"); delay(500); } } loopCounter++; if ((loopCounter % 1000) == 0) { long ms = millis(); Serial.print("Using Ethernet Shield V1"); 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 dataOut = ((float) sampledData) / 1024.0; sendData(&client); } //end main loop // change this method to send your data void sendData(Print * output) { output->print(uS); output->print(','); output->print(dataOut); output->println(); }