How long it takes for the effects of a drug to wear off is a complicated matter that depends on many factors, including what substance you used, its strength, how much you used, your tolerance, how the drug was taken (ingested, smoked, injected, etc.), your weight, and more. Researchers estimate drivers should wait anywhere from three to ten hours after smoking marijuana. There is little research on how long drivers should wait before driving under the influence of other controlled substances.
The bottom line is that if you still feel the effects of any drug, including legal medications, you should avoid driving.