Make Sure Digital pin is high before Serial.write() I have the following code for a TTL to RS-485 Adapter Module 

[...code]
digitalWrite(DE_PIN, HIGH); //directional control for adapter 
Serial2.write(Message, Size);
Serial2.flush();
digitalWrite(DE_PIN, LOW);  // Set To Recieve
Serial2_reading_foo();
[code...]


I've noticed that DE_PIN does not seem to go High fast enough before Serial2.write() executes I confirmed this by adding a small delay(ms) which made the code function perfectly.  

digitalWrite(DE_PIN, HIGH);
delay(100);
Serial2.write(Message, Size);
Serial2.flush();
digitalWrite(DE_PIN, LOW);  // Set To Recieve
delay(100);
Serial2_reading_foo();


Unfortunately, there's pretty much universal agreement that you shouldn't ever use delay() in programming. Plus doing any kind of wait function here seems to be insensitive to the real problem. Is there a better way to make sure that DE_PIN==HIGH before the Serial2.write() Function begins?
The MAX485 requires 2V on the DE pin to register a HIGH. It takes no more than an absolute maximum of 50ns for the DE pin to switch the buffers. The Arduino takes 10.4ns for an IO pin to rise to 2V.

That's an absolute maximum of 60.4ns.

One clock cycle of a 16MHz Arduino is 63ns.

That is longer than it takes for the IO pin to tell the MAX485 to switch the buffers.

You can increase that rise time by taking in to account the input capacitance of the MAX485 and the resistance of the connection between the Arduino and the DE pin, but it will still be microscopically small compared to the time between the pin going HIGH and the serial transmission starting.

So there must be something fundamentally wrong with your setup.  Two possibilities spring to mind:


You aren't controlling RE, only DE, so RE is floating and is being capacitively linked to DE through the parasitic capacitances of the board and circuitry, making it rise and fall very slowly.
You haven't set the IO pin on the Arduino to OUTPUT, so it switches between floating and HIGH through a very high resistance, causing slow rise to HIGH and maybe never getting to LOW at all.


Or a combination of the two.