the code is missing a } before the serial_communication function definition ?
you should ensure you don't try to receive more than 31 characters - if you have a buffer overflow then you are in trouble;
how did you connect your power supply for the motor? any risk of interference with the Serial lines?
larrychance:
Sorry for my English, I'm French
There is a french forum (but your english is totally fine)
paulvha:
Why do you attach the interrupt handler to pin 0 ? isn't that your serial communication ?
No that's correct. The 0 there is the interrupt number (for pin #2 - his pin_encoder_I )
that being said the recommended way to write this isattachInterrupt(digitalPinToInterrupt(pin), ISR, mode); // recommendedwhere you indeed pass the pin number
J-M-L:
the code is missing a } before the serial_communication function definition ?
you should ensure you don't try to receive more than 31 characters - if you have a buffer overflow then you are in trouble;
how did you connect your power supply for the motor? any risk of interference with the Serial lines?
There is a french forum (but your english is totally fine)
I already asked the french forum but did'nt get any accurate answer
i use a l293d chip and a simple switched-mode power supply of 12v (the nominal voltage is 23.5) and I don't think there can be any interferences.
for the buffer i'm supposed to transmit 5 char so a buffer of 31 should be sufficiant
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.
If the encoder is producing a lot of pulses per second it may use up so much of the Arduino's CPU cycles that it is unable to keep up with serial input. Serial requires interrupts to be enabled and they are disabled in an ISR. Even though your ISR code is commendably short it may be enough to screw things up if there are a lot of encoder pulses.
larrychance:
i use a l293d chip and a simple switched-mode power supply of 12v (the nominal voltage is 23.5) and I don't think there can be any interferences.
can you attach a diagram / describe your wiring? could you try powering the motor separately from the arduino?
larrychance:
for the buffer i'm supposed to transmit 5 char so a buffer of 31 should be sufficient
are you typing that in the Serial Console yourself or it comes from somewhere else?
Robin2:
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.
If the encoder is producing a lot of pulses per second it may use up so much of the Arduino's CPU cycles that it is unable to keep up with serial input. Serial requires interrupts to be enabled and they are disabled in an ISR. Even though your ISR code is commendably short it may be enough to screw things up if there are a lot of encoder pulses.
...R
indeed my encoder (i get it from a ink printer) produces a lot of impulses, but when i received a number ,the serial isr should be disabled or i missed something ?
J-M-L:
can you attach a diagram / describe your wiring? could you try powering the motor separately from the arduino?
are you typing that in the Serial Console yourself or it comes from somewhere else?
i've just done a fritzing one
i'm doing tests with the serial monitor but the aim is to send the setpoint with python (where i have the image processing code)
since you read french, have a look at Le moteur à courant continu and check the part discussing the L293D
Are things working when the motor spins slowly? what is "a lot"?
my motor works well, in fact my problem is when I use the serial (without the serial transmission the servo is very reactive and goes to the right place (even if it is a P corrector))
when I put a small proportional gain: yes it works more or less but my goal is to put a strong gain for the speed and accuracy of the system
for the number of encoder values it depends on the baudrate but for a displacement of 20 ms I must have about 100 ms, there are 2000 positions on the encoder strip so it scrolls quickly on the serial monitor
larrychance:
indeed my encoder (i get it from a ink printer) produces a lot of impulses, but when i received a number ,the serial isr should be disabled or i missed something ?
If the Arduino is busy checking the encoder it can miss incoming Serial data. There isn't any simple solution to that. Maybe get an encoder with fewer pulses per revolution.
How many pulses per second is the encoder generating?
You are using CHANGE for you encoder interrupts. Is that essential? if you use RISING or FALLING the number of interrupts will be halved.
Have you the option to pause the encoder and then ask for and receive the serial data while it is paused?
Robin2:
If the Arduino is busy checking the encoder it can miss incoming Serial data. There isn't any simple solution to that. Maybe get an encoder with fewer pulses per revolution.
How many pulses per second is the encoder generating?
You are using CHANGE for you encoder interrupts. Is that essential? if you use RISING or FALLING the number of interrupts will be halved.
Have you the option to pause the encoder and then ask for and receive the serial data while it is paused?
i dont know how fast is the encoder but i think it generates a lot of impulsions (maybe 500 per sec)
i can change to RSISNG or FALLING but the precision will be also halved (and my project need a good precision). At the reception of data yes i can pasue the encoder.
i havent the system at home but next week i'll try all your solutions
larrychance:
At the reception of data yes i can pasue the encoder.
That is probably the simplest solution. Be aware, however, that what I have in mind is that the sending of Serial data will only start after the encoder is paused.
Today i have tried your solutions but i've found a very simple one (I never thought about it)
I just moved my transfert_function out of the if isr and i lowered the baudrate to 9600.
Now the servo control is fully operational
Thank you for your answers, i set the thread to solved