PC to Arduino with RS485

I finally got the PC communication with one Arduino on Max485 (RS485) working now. Right now it runs very slow. I am using Lazarus to create windows apps. The apps have six buttons for six LEDs. Arduino Nano has six LEDs. When I click the first button and the first LED comes on for about 2 sec. Then I can click the second button for the second LED to come on. I can't click the third button till the second LEDs comes on first then I can click the next button for the next LED light to come on. I want to be able to click many buttons that I don't have to wait till LEDs come on then click the next one.

I was planning to add more devices like servos, IR sensors and RFID. Before I do that I need to fix the speed first before I can add more devices. Also, I would like to add more Arduino Nano but not sure how to do this. I could not find a sample code for it. Could someone help me with the speed and code for the second and third Arduino Nano?

Here is my code for Arduino Nano.

int RE_DE = 2;

void setup()
{
  pinMode(13, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6, OUTPUT),
  pinMode(7, OUTPUT);

// Put Max485 in transmit mode so that Arduino can recieve message
  pinMode(RE_DE, OUTPUT);
  digitalWrite(RE_DE, LOW);

// Start up serial connection
  Serial.begin(9600);
}
void loop()
{
  if (Serial.available())
    {
    int state = Serial.parseInt();
    if (state == 1)
      {
      digitalWrite(13, LOW);
      Serial.println("Command completed LED 13 turned OFF");
      }
    if (state == 2)
      { 
      digitalWrite(13, HIGH);
      Serial.println("Command completed LED 13 turned ON");
      }
    if (state == 3)
      {
      digitalWrite(3, LOW);
      Serial.println("Command completed LED 3 turned OFF");
      }
    if (state == 4)
      {
      digitalWrite(3, HIGH);
      Serial.println("Command completed LED 3 turned ON");
      }
    if (state == 5)
      {
      digitalWrite(4, LOW);
      Serial.println("Command completed LED 4 turned OFF");
      }
    if (state == 6)
      {
      digitalWrite(4, HIGH);
      Serial.println("Command completed LED 4 turned ON");
      }
    if (state == 7)
      {
      digitalWrite(5, LOW);
      Serial.println("Command completed LED 5 turned OFF");
      }
    if (state == 8)
      {
      digitalWrite(5, HIGH);
      Serial.println("Command completed LED 5 turned ON");
      }
    if (state == 9)
      {
      digitalWrite(6, LOW);
      Serial.println("Command completed LED 6 turned OFF");
      }
    if (state == 10)
      {
      digitalWrite(6, HIGH);
      Serial.println("Command completed LED 6 turned ON");
      }
    if (state == 11)
      {
      digitalWrite(7, LOW);
      Serial.println("Command completed LED 7 turned OFF");
      }
    if (state == 12)
      {
      digitalWrite(7, HIGH);
      Serial.println("Command completed LED 7 turned ON");
      }
    }

}

Here is the part of code for Lazarus

procedure TForm1.Button2Click(Sender: TObject);
begin
  ser.SendString('2'); // button 2 should have 'on' here
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  ser.SendString('3'); // button 3 should have 'off' here
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  ser.SendString('4'); // button 4 should have 'on' here
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
  ser.SendString('5'); // button 5 should have 'off' here
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
  ser.SendString('6'); // button 6 should have 'on' here
end;



procedure TForm1.FormCreate(Sender: TObject);
begin
  ser := TBlockSerial.Create;
  Sleep(25); //250
  ser.Connect('COM5'); // write here Arduino COM port number (on linux it's something like '/dev/ttyUSB0')
  Sleep(25);  //250
  ser.Config(9600, 8, 'N', SB1, False, False);
  ser.RTS := false; // comment this if needed
  ser.DTR := false; // comment this if needed
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  ser.free;
end;

end.

How have you connected the MAX485 to your Arduino? Usually a software serial port is used but your code appears to be using the hardware serial port.

markd833,

I am using RS485. PC to USB to TLL convert, pin A to pin A, and pin B to pin B on Max485 from USB convert. Max485 pin RO to Nano pin 2 RX and Max485 DI to Nano pin 1 TX. Max485 RE and DE are connected to Nano pin 5 D2.

I'm not that familiar with the Nano, but a quick read of the description on the main Arduino website suggests that the Tx and Rx pins you have connected your MAX485 to are already used by the USB to Serial interface.

If that's the case, then you can use a software serial port library to drive the MAX485. AltSoftSerial seems to be pretty good. Note that with a software serial port, you won't get much faster than 19200 baud out of it.

Why aren't you using the CAN bus? 1) Speed up to 1Mbit. 2) High reliability of information transmission. 3) You can use interrupts when receiving information that data has been received and is in the buffer.

Please note that when you are using a PC exchange at 9600 baud, you cannot do anything until the exchange is done. Use a PC speed of 115200 or 250 000 baud, it is more reliable and faster than 10 times. (Read the documentation for ATMega328 (ATMega2560) on UART, what% of errors will be at different baud rates (Quartz 16MHz; speed 250 000; errors = 0). Best regards.

Hi, I'm using also a Max485 connected to a nano. My Code I use.

// RS485
#define SSerialRX   7    //Serial Receive pin
#define SSerialTX   8    //Serial Transmit pin
const byte ENABLE_PIN = 4;
SoftwareSerial rs485 (SSerialRX, SSerialTX);  // receive pin, transmit pin
...


rs485.begin (28800);
pinMode (ENABLE_PIN, OUTPUT);  // driver output enable