New to Arduino. Is this possible

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?

Is there any way of getting the Arduino to send the input values at this speed via the serial communications?

Is there any way for us to know what your program is doing without see it ?

OK, so it IS possible, just needed to set the Serial.setTimeout

can I communicate more than one line of serial data per second, regardless of the code.

The code is important, don't you think ?

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.

Well setting the timeout did work for me.

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;

void setup() {
// put your setup code here, to run once:
pinMode(I1, INPUT_PULLUP);
pinMode(I2, INPUT_PULLUP);
pinMode(I3, INPUT_PULLUP);
pinMode(I4, INPUT_PULLUP);
pinMode(I5, INPUT_PULLUP);
pinMode(I6, INPUT_PULLUP);
pinMode(I7, INPUT_PULLUP);
pinMode(I8, INPUT_PULLUP);
pinMode(I9, INPUT_PULLUP);
pinMode(I10, INPUT_PULLUP);
pinMode(I11, INPUT_PULLUP);
pinMode(I12, INPUT_PULLUP);
pinMode(I13, INPUT_PULLUP);
pinMode(I14, INPUT_PULLUP);
pinMode(I15, INPUT_PULLUP);
pinMode(I16, INPUT_PULLUP);
pinMode(I17, INPUT_PULLUP);
pinMode(I18, INPUT_PULLUP);
Serial.setTimeout(5);
Serial.begin(19200);
}

void loop() {

// send data only when you receive data:
// if (Serial.available() > 0) {
// read the incoming:
incoming = Serial.readString();
if (incoming == "READ") {
//send input string
inputs = digitalRead(I1);
inputs = inputs + digitalRead(I2) + digitalRead(I3) + digitalRead(I4) + digitalRead(I5) + digitalRead(I6) + digitalRead(I7) + digitalRead(I8) + digitalRead(I9);
inputs = inputs + digitalRead(I10) + digitalRead(I11) + digitalRead(I12) + digitalRead(I13) + digitalRead(I14) + digitalRead(I15) + digitalRead(I16) + digitalRead(I17);
inputs = inputs + digitalRead(I18) ;
Serial.println(inputs);

}

// }

// incoming = "";

Serial.flush();

}

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).

Serial Input Basics - simple reliable ways to receive data.

…R

 Serial.begin(19200);

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.

Try this, compiles but not tested:-

// define input pins in the order you output them
byte inputPin [] = {21,19,18,17,16,15,14,12,11,10,9,8,7,6,5,4,3,2}; 

void setup() {
  for(int i=0; i< sizeof(inputPin) ;i++){
    pinMode(inputPin[i], INPUT_PULLUP); // enable pull ups
  }
  Serial.begin(19200);  // or a higher speed if you want
}

void loop() {
  if (Serial.available() > 0) {
    char incoming = Serial.read();
    if(incoming == 'R'){ // only need to look at one char
      sendData();
    }
  } 
}

void sendData(){
    for(int i=0; i< sizeof(inputPin) ;i++){
    if(digitalRead(inputPin[i]) == 0) Serial.print('0');
    else Serial.print('1');
  }
  Serial.println();
}

byte inputPin = {21,19,18,17,16,15,14...

Which Arduino is this?

An Uno has 14-19 (A0-A5), but doesn't have pin 21.

A Mega does, but this is a hardware I2C pin, and a Mega AFAIK doesn't have 14-19.
Leo..

SoundsDigital:
I’m using a string as this is intended as a replacement for a 3rd party pic microcontroller

That is an illogical statement :slight_smile:

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).

…R