/* PythonSerialTest1 Reads an analog input on pin 0, prints the result along with sample time to the serial monitor. Serial data can also be sent to the Arduino to change the sample time Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground. This example code is in the public domain. Modified by Liam Maskey for Fab Academy 2017 python serial tutorial */ int sampleNumber = 0; int sampleTime = 0; // time in seconds sent by user int delayValue = 1000; // defualt delay value in milliseconds String readString; const int milli = 1000; // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } // the loop routine runs over and over again forever: void loop() { while (Serial.available()) { char c = Serial.read(); //gets one byte from serial buffer readString += c; //makes the string readString delay(2); //slow looping to allow buffer to fill with next character } if (readString.length() >0) { sampleTime = readString.toInt(); //convert readString into a number if((sampleTime <1) || (sampleTime > 1000)) // put a limit to the sample time sampleTime=1; delayValue=sampleTime*milli; // convert to milliseconds } if(sampleTime >0){ int sensorValue = analogRead(A0); sampleNumber++; // print out the value you read: Serial.print(sampleNumber*sampleTime); //Serial.print(sampleNumber); Serial.print(','); Serial.println(sensorValue); } readString=""; // reset string value delay(delayValue); // delay in between reads for stability }