I try to use a python program to read the output data from an arduino nano. The data saved from the python program shows a sampling speed of about 17 ms/data point. I want to increase the speed to at least 1 ms/data point (1000/second). How should I change the code? Thank you very much.
Arduino code
const int voltagePin = A0;
const int HANDSHAKE = 0;
const int VOLTAGE_REQUEST = 1;
int inByte;
void printVoltage() {
// Read value from analog pin
float value = analogRead(voltagePin);
// Get the time point
unsigned long time_ms = millis();
// Write the result
if (Serial.availableForWrite()) {
String outstr = String(String(time_ms, DEC) + "," + String(value, DEC));
Serial.println(outstr);
}
}
void setup() {
// Initialize serial communication
Serial.begin(1000000);
}
void loop() {
// Check if data has been sent to Arduino and respond accordingly
if (Serial.available() > 0) {
// Read in request
inByte = Serial.read();
// If data is requested, fetch it and write it, or handshake
switch (inByte) {
case VOLTAGE_REQUEST:
printVoltage();
break;
//case HANDSHAKE:
//if (Serial.availableForWrite()) {
//Serial.print(""); //Please input:
}
//break;
}
}
Python code
for n in range (0, 200):
#names = ['0', 'A', 'B', 'C', 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', '1' ]
#print (names)
for name in names:
# Alphabet dictionary
alphabet_dict = {'A': '00', 'B': '01', 'C': '010', 'D': '011', 'E': '0110', 'F': '0111', 'G': '01110',
'H': '10', 'I': '11',
'J': '110', 'K': '111', 'L': '1110', 'M': '1111', 'N': '11110', 'O': '100', 'P': '101',
'Q': '1010',
'R': '1011', 'S': '10110', 'T': '10111', 'U': '1100', 'V': '1101', 'W': '11010',
'X': '11011', 'Y': '110110',
'Z': '110111'}
board = pyfirmata.Arduino ('/dev/cu.usbserial-AR0JYQ1Q')
HANDSHAKE = 0
VOLTAGE_REQUEST = 1
def find_arduino(port='/dev/cu.usbserial-AR0JYQ1Q'):
"""Get the name of the port that is connected to Arduino."""
if port is None:
ports = serial.tools.list_ports.comports ()
for p in ports:
if p.manufacturer is not None and "Arduino" in p.manufacturer:
port = p.device
return port
port = find_arduino ()
arduino = serial.Serial (port, baudrate=1000000)
# Ask Arduino for data
arduino.write (bytes ([VOLTAGE_REQUEST]))
# Receive data
raw = arduino.read_until ()
def parse_raw(raw):
"""Parse bytes output from Arduino."""
raw = raw.decode ()
if raw [-1] != "\n":
raise ValueError (
"Input must end with newline, otherwise message is incomplete."
)
t, V = raw.rstrip ().split (",")
return int (t), float (V) * 5 / 1023
def request_single_voltage(arduino):
"""Ask Arduino for a single data point"""
# Ask Arduino for data
arduino.write (bytes ([VOLTAGE_REQUEST]))
# Read in the data
raw = arduino.read_until ()
# Parse and return
return parse_raw (raw)
time_ms = []
voltage = []
for i in range (200):
# Request and append
t, V = request_single_voltage (arduino)
time_ms.append (t)
voltage.append (V)
# Wait 20 ms
# time.sleep(0)
time_ms = np.array (time_ms)
voltage = np.array (voltage)
voltage = gaussian_filter1d (voltage, sigma=2)
df = pd.DataFrame (voltage, time_ms)
df.drop(df.index[0])
df.name = name
df.to_csv (r'/Users/renzha/Documents/work/preparing papers/non-contact HCI/' + df.name + '.txt')