I'm trying to control a DC motor via a push button which will make the DC motor go in the forward, reverse or stop depending on the number of pushes. Here's a picture of the schematic of the setup. My question is what is the code or function that I need to put in in order to make it go forward, reverse, and stop? And no involvement of a h bridge.
Here's a sample of what I "think" would make the DC motor stop, go forward, and reverse based on reading through numerous websites. This is just part of my whole code.
if (MotorMode == 0) { // initial mode
digitalWrite(relayPin, LOW); // Motor in initial state
}
if (MotorMode == 1) { // Motor in the forward direction
digitalWrite(relayPin, HIGH);
}
if (MotorMode == 2) { // Motor stops
digitalWrite(relayPin, LOW);
}
if (MotorMode == 3) { // Motor goes in the reverse direction
digitalWrite(relayPin, HIGH);
}
}
In your drawing pin 1 controls motor on or off. Pin 2 controls forware or reverse. So:
if (MotorMode == 0) { // initial mode
digitalWrite(1, LOW); // Motor in initial state = off
}
if (MotorMode == 1) { // Motor in the forward direction
digitalWrite(2, HIGH); // turn direction relay on for forward direction
digitalWrite(1, HIGH); // turn motor voltage on
}
if (MotorMode == 2) { // Motor stops
digitalWrite(1, LOW); //turn motor voltage off
}
if (MotorMode == 3) { // Motor goes in the reverse direction
digitalWrite(2, LOW); //turn direction relay off for reverse direction
digitalWrite(1, HIGH); // turn motor voltage on
}
}
Be sure you have a wire from the 9 volt supply negative terminal to the arduino ground pin, it's required.
retrolefty:
In your drawing pin 1 controls motor on or off. Pin 2 controls forware or reverse. So:
Be sure you have a wire from the 9 volt supply negative terminal to the arduino ground pin, it's required.
Lefty
Thank you so much! I was so confused on why there was a pin 2 schematic on the bottom right not attached to the motor transistor / relay schematic in the top left.
retrolefty:
In your drawing pin 1 controls motor on or off. Pin 2 controls forware or reverse. So:
Lefty
I would need to define pin 2 at the beginning of my code though right or else the code will not recognize what pin 2 is?
Like this:
int buttonPin = 6; // pushbutton switch is connected to pin 6
int MotorMode = 0; // Motor position
int MotorPin = 1; // motor is connected to pin 1
int RelayPin = 2; // Relay connected to pin 2
void setup() {
pinMode(buttonPin, INPUT); // Declare pushbutton as input
pinMode(relayPin, OUTPUT); // Declare relay as output
pinMode(MotorPin, OUTPUT): // Declare motor as output
retrolefty:
In your drawing pin 1 controls motor on or off. Pin 2 controls forware or reverse. So:
Lefty
I would need to define pin 2 at the beginning of my code though right or else the code will not recognize what pin 2 is?
Like this:
int ForwardOrReversePin = 2; // connected to pin 2
If you wish. However digitalWrite commands will accept using the actual digital pin number in the command. It is however considered good paractice to utilize names for your constants rather then just using 'magic numbers' that later may not be as easily understood when reading the code.
retrolefty:
In your drawing pin 1 controls motor on or off. Pin 2 controls forware or reverse. So:
Lefty
I would need to define pin 2 at the beginning of my code though right or else the code will not recognize what pin 2 is?
Like this:
int ForwardOrReversePin = 2; // connected to pin 2
If you wish. However digitalWrite commands will accept using the actual digital pin number in the command. It is however considered good paractice to utilize names for your constants rather then just using 'magic numbers' that later may not be as easily understood when reading the code.
Good luck;
Lefty
I am a noob and super weak at programming so I like to name my variables or whatever they're called so I don't get confused XD
I am a noob and super weak at programming so I like to name my variables or whatever they're called so I don't get confused
As I said that is a good practice to follow beginner or not. Back to constants Vs variables, one uses a variable where the value of the variable can or does change as the program runs. Constants are used where the value needs to stay the same at all times (read only value) when the program runs. Either will work for the digitalWrite command but it's a good habit to understand and learn the difference and utilize them properly.