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.