Hi all, I am writing a code to control a home made electric motor, and have gotten it to run now. The problem I have is that once I add more code to the loop (for adjustable time and lcd display) the timing goes astray and motor looses rpm. I have tried a few different ways to use millis and can't finger it out. Could someone please help? How it works- There are 3 hall sensors two of them control the motor. When Trig1 is pulled LOW by the hall it writes Drive1 HIGH, this causes the circuit to fire a 2ms pulse to a transistor causing motor rotation, then Trig2/Motor2 do the same thing. Trig3 is does kind of the same thing but can only happen when Trig1 and Trig2 are HIGH. Also can one just substitute micros for the milli command, as I would think that the timing would be more precise if it was 2000 micros instead of 2 millis? Here is the code.
// constants won't change. They're used here to set pin numbers
#include <Button.h> // http://www.arduino.cc/playground/Code/Button
Button Trig1 = Button (4,PULLUP); // sets digital pin 4 as Trigger-1, also pulled up
Button Trig2 = Button (5, PULLUP); // sets digital pin 5 as Trigger-2, also pulled up
Button Trig3 = Button (6, PULLUP); // sets digital pin 6 as Trigger-3, also pulled up
const int Drive1 = 9; // sets digital pin 9 as Drive-1 output
const int Drive2 = 10; // sets digital pin 10 as Drive-2 output
const int DisC = 11; // sets digital pin 11 as CapDischarge output
void setup(){
pinMode ( 9, OUTPUT); // this is output for Drive-1
pinMode (10, OUTPUT); // this is output for Drive-2
pinMode (11, OUTPUT); // this is output for cap discharge
}
void loop()
{
if (Trig1.isPressed()){ // reads the Trigger 1 input and switches Drive-1
digitalWrite (9, HIGH);
delay (2); // this is the amount of time the Drive-1 pulse is ON
digitalWrite (9, LOW); // forces Drive-1 low...no motor pulse
}else{
digitalWrite (9,LOW); // keeps Drive-1 LOW
}
if (Trig2.isPressed()){ // reads the Trigger 2 input and switches Drive-2
//delay (); // may need for delayed pulse and tuning
digitalWrite (10, HIGH);
delay (2); // this is the amount of time the Drive-2 pulse is ON
digitalWrite (10, LOW); // forces Drive-2 low...no motor pulse
//delay (); // allows for longer trigger pulse in time
}else{
digitalWrite (10, LOW); // keeps Drive-2 LOW
}
if (Trig3.isPressed()){ // reads the Trigger 3 input and switches CapDischarge
digitalWrite (11, HIGH);
delay (2); // this is the amount of time the CapDischarge pulse is ON
digitalWrite (11, LOW); // forces CapDischarge low...back to charge
}else{
digitalWrite (11, LOW); // keeps CapDischarge LOW
}
}