1

I have Arduino Leonardo board operating lots of AC relays in quite noisy environment. USB works unstable, which sometimes causes the board to hang, so I decided to use secondary serial connection as "debug console":

  Serial1.begin(115200);   .....  .....  if (Serial1.available()) {    char ch = Serial1.read();    if ( isalnum(ch) ) {      switch (ch) {        case 'M': {          turnOn_pin_Motor();          break;        }        case 'm': {          turnOff_pin_Motor();          break;        }        .......

My problem is that noise affects Serial1 the same way it messes USB (but at least it doesn't cause it to freeze). I'd like to use some sort of error checking to prevent Leonardo from acting on "phantom" commands. I could, for example, prefix each keyboard command my Arduino should receive with special symbol, maybe "!".

So I must be reading one char, checking if it is "!" and reading second character.

Unfortunately, I can't modify my code so it would use 2-character buffer and shift its contents as characters arrive, without locking the main loop while waiting for the next char. Any hints on what should I do in this case?

askedMar 15, 2014 at 22:38
Alexander's user avatar
9
  • Serial1 on a Leonardo isnot “Software Serial”, it’s arguably more “hardware” thanSerial. How does the noise on your serial port manifest? Are the characters injected truly random?CommentedMar 15, 2014 at 23:52
  • Yes, they are totally random: here is tiny example from my debug log (notice the garbage as the 'Inlet' valve turns off and the current inducted by AC coil messes with power supply):[3819448 void turnOff_pin_Inlet()] WкК•╧╠•я² [3819570 void checkTempSensors()] DS18B20 on Door failed!CommentedMar 16, 2014 at 7:33
  • 1
    Providing a schematic of your circuit would help. The best way to solve your problem is to first try to reduce the noise, this has to be done electronically.CommentedMar 16, 2014 at 8:19
  • 1
    As @jfpoilpret mentions, investigate the cause of the noise. It is usually easier to fix the cause than to remedy the data corruption.CommentedMar 16, 2014 at 9:09
  • 1
    Noise bad enough to cause Serial to corrupt is likely to cause other problems on I/O. Fixing the commands won't fix other issues, and it's likely you system would fail in another way.CommentedMar 16, 2014 at 11:38

1 Answer1

3

Serial1 on the Arduino Leonardo is hardware serial, not software.

There are several ways to read in multiple bytes from Serial.

Instead of detectingSerial.available() (i.e. non-zero), change it toSerial.available() >= 2

if (Serial.available() >= 2) {  for (int i=0; i<2; i++) {    buffer[i] = Serial.read();  }}

Or you can leave it as it is, and read into the buffer one at a time.

static int bufferIndex;if (Serial.available()){    buffer[bufferIndex++] = Serial.read()}

You'll need to detect the contents of the buffer in anotherif statement, and also deal with the bounds of the index.

Honestly though, noise is your issue here - this is an example of anXY problem. There's nothing inherent in using a microcontroller with relays that should mean that serial won't work.

answeredMar 16, 2014 at 11:45
Cybergibbons's user avatar

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.