I'm very new to Arduino, having previously had a dabble with PIC microcontrollers. I have put together some code to read the Arduino input pins and send the value to a PC via the serial communications as a string of 1 and 0 dependent on the value whenever the Arduino receives a "READ" command.
However, no matter how fast I send the read command, the maximum speed I can get the reply from the Arduino seems to be about once a second. Ideally, I want to read an encoder and require a speed of at least 100 hZ.
Is there any way of getting the Arduino to send the input values at this speed via the serial communications?
SoundsDigital:
Is there any way of getting the Arduino to send the input values at this speed via the serial communications?
I normally communicate at a speed of 11,520 characters a second - using 115200 bps serial. Fastest I think is about 8x that. So unless your lines are ridiculously long, the answer to your question would be "yes".
SoundsDigital:
OK, so it IS possible, just needed to set the Serial.setTimeout
No have you read what the timeout does and where you can use it? https://www.arduino.cc/en/Serial/SetTimeout
Your code should not be using any of the functions where the timeout is applicable.
But as you keep on refusing to post your code I assume you are not interested in learning the right thing to do.
String incoming = ""; // for incoming serial string data
String inputs = ""; //for input string
int I1 = 2;
int I2 = 3;
int I3 = 4;
int I4 = 5;
int I5 = 6;
int I6 = 7;
int I7 = 8;
int I8 = 9;
int I9 = 10;
int I10 = 11;
int I11 = 12;
int I12 = 14;
int I13 = 15;
int I14 = 16;
int I15 = 17;
int I16 = 18;
int I17 = 19;
int I18 = 21;
Please post your program using the code button </> so it looks like this. See How to use the Forum.
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with ‘\0’ (NULL).
That's your real speed problem. You can set that to a much higher number - the Serial monitor of the IDE supports up to 2000000 (2 million), check the documentation to see what your specific Arduino (most importantly: the microcontroller) can do.
I have put together some code to read the Arduino input pins and send the value to a PC via the serial communications as a string of 1 and 0 dependent on the value whenever the Arduino receives a “READ” command.
That is not what that code does. It adds up all the binary bits on the inputs and gives you the total number of inputs with a logic High on them. Is that what you want to do? If so it seems a singular useless thing to do.
The speed of output is limited by the number of times you can send the string “READ” and how fast the Serial.readString() function takes. It is this last point that limits the speed because that function is really really slow.
To speed things up reduce the read command to a single character and use the Serial.read function to just get one byte from the input stream to trigger the writing.
Grumpy_Mike:
That is not what that code does. It adds up all the binary bits on the inputs and gives you the total number of inputs with a logic High on them. Is that what you want to do? If so it seems a singular useless thing to do.
That's what I thought too, until I noticed that 'inputs' is a String object.
johnwasser:
That's what I thought too, until I noticed that 'inputs' is a String object.
I'm using a string as this is intended as a replacement for a 3rd party pic microcontroller USB i/o device which has proven expensive, unreliable and inefficent (not as many ports as the arduino)
It is connecting to a PC running a VB program which will need a little bit of editing, but hoping to keep that to a minimum. The program in only capable of accessing the serial port a maximum of once per millisecond, which is plenty fast enough for what it is doing.
So while I freely acknowledge this is probably not the best way of doing the job, the plan is to use it as a replacement for something that was doing the job far worse.
I am more than happy to admit my experience is very limited with the Arduino and writing code in C. I came up with this in just a few hours, starting from scratch and it actually works, after spending weeks trying to code a pic 16F887 to do the same thing and failing miserably.
SoundsDigital:
I’m using a string as this is intended as a replacement for a 3rd party pic microcontroller
That is an illogical statement
You need to distinguish between using the String class (which is a bad idea if you want reliability) and cstrings which can do the same job reliably, but the programming is a little less convenient. (See my earlier comment).