0

I'm workin on a node for blender to control an arduino. I notice it goes really slow after a while so I isolated the problem. It is good in maintaining 60 frames a second, then after around 40 seconds (pretty exact), it drops to a pretty exact 1 fps for some reason.

What could be going on?

import serial
import time

ser = serial.Serial('COM3', 115200)

c = 0

while True:
    c+=1
    print("ok ", c)
    numIn = "100n"
    ser.write(numIn.encode('ascii'))
    time.sleep(1./60)

arduino code:

// special thx to Nick Gammon

const unsigned int MAX_INPUT = 50;


unsigned long delay_time = 300;
unsigned long last_toggle_time = 0;
int current_state = LOW;

void setup () {
  Serial.begin (115200);
  pinMode(12, OUTPUT);

} 


void process_data (const char * data) {
  Serial.println (data);
  delay_time = atoi(data);
} 

void processIncomingByte (const byte inByte) {
  static char input_line [MAX_INPUT];
  static unsigned int input_pos = 0;

  switch (inByte) {

    case 'n':   // end of text
      input_line [input_pos] = 0;  // terminating null byte
      process_data (input_line);
      input_pos = 0;
      break;

    case 'r':   
      break;

    default:
      if (input_pos < (MAX_INPUT - 1))
        input_line [input_pos++] = inByte;
      break;

  } 
} 

void loop() {

  while (Serial.available () > 0) {
    processIncomingByte (Serial.read ());
  }

  unsigned long m  = millis();

  if (m > last_toggle_time + delay_time) {
      current_state = current_state == HIGH ? LOW : HIGH;
      digitalWrite(12, current_state);         
      last_toggle_time = m;
  }

}  
12
  • Hint: Check "if (m > last_toggle_time + delay_time)" and what happens when millis() as int wraps? Apr 6, 2016 at 21:55
  • I changed it to unsigned long now and it's still the case.
    – clankill3r
    Apr 6, 2016 at 22:24
  • Also, the blinking is not the problem, that keeps going well. It's the serial communication that slows down.
    – clankill3r
    Apr 6, 2016 at 22:27
  • 1
    What happens if you remove the Serial.print() from process_data()? What does your Python script do with serial input? Apr 6, 2016 at 22:41
  • Cool, if I remove the print then the problem is gone. However, eventually I want python to read the input. Now python hangs on ser.readLine(). Maybe a new topic for the hanging?
    – clankill3r
    Apr 7, 2016 at 7:28

1 Answer 1

0

If you print from arduino to serial and you don't read it then the buffer gets full. Causing it to go in a second delay. So make sure you read as well when you send data over.

A short sleep is required between the sleep and the read for some reason. Else it hangs.

import serial
from time import sleep

ser = serial.Serial('COM3', 115200)
sleep(1)

c = 0

while True:
    c+=1
    print("ok ", c)
    numIn = "100"
    ser.write((numIn + '\r\n').encode())
    sleep(1./120)

    inp = ser.readline()
    print(inp.decode("utf-8"))
    sleep(1./120)
1
  • The theory behind this answer is quite dubious, and if you have a conventional Uno type Arduino or something with a distinct USB-serial converter, impossible. Jun 6, 2016 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.