Hello Folks!
So I writing a sketch to control a winch with a dual channel motor shield remapped into a single channel for more current. The forward and reverse controlled from a switch that flicks up for forward and down for reverse. the middle position is off. Vin to the switch is either going to be 12v, if that is a usable logic signal in to the Arduino, or I can do 5v with a 12 to 5 converter. There are a couple other inputs to my shield that aren't usable with my remap so I haven't bothered to change the pinMode on those. Here is the shield i'm using: Pololu Dual VNH5019 Motor Driver Shield for Arduino . I haven't had much luck with their library and I decided to write one myself for the practice. Here is my sketch.
/*
Pololu VNH5019 Dual channel remapped into single channel mode to control SS Mast.
We are only using one input channel to Driver Shield because the shield is remapped to each VNH5019.
pins 2 & 4 control OUTA
pins 7 & 8 control OUTB
pins 9 & 10 control PWM
Pololu VNH5019 dual channel motor driver shield resource:
User's Guide: http://www.pololu.com/docs/pdf/0J49/dual_vnh5019_motor_driver_shield.pdf
*/
int keyUp =5; //Input from key to raise mast.
int keyDown =6; //Input from key to lower mast.
int FWD =2; //Raises mast.
int REV =7; //Lowers mast.
int PWM= 9; //Speed control of raise and lower.
void setup()
{
pinMode (keyUp, INPUT_PULLUP); //This sets Arduino pin 5 as input from key switch (HIGH in off state).
pinMode (keyDown, INPUT_PULLUP); //This sets Arduino pin 6 as input from key switch (HIGH in off state).
pinMode (FWD, OUTPUT); //This sets the Arduino pin 2 as output to Pololu pin 2 and 4.
pinMode (REV, OUTPUT); //This sets the Arduino pin 7 as output to Pololu pin 7 and 8.
pinMode (PWM, OUTPUT); //This sets the Arduino pin 9 as output to Pololu pin 9 and 10.
}
void loop()
{
analogWrite (PWM, 128); //This sets the speed between 1-255. 127/128 = 50% duty cycle.
if (digitalRead(keyUp) == LOW) //This reads the state of key switch and controls movement forward.
{
digitalWrite(FWD, HIGH); //This sets the FWD pin HIGH if the keyUp state is LOW.
digitalWrite(REV, LOW); //This sets the REV pin LOW if the KeyUp state is LOW.
}
else; //If keyUp is HIGH then FWD and REV are off.(; closes if)
{
digitalWrite(FWD, LOW);
digitalWrite(REV, LOW);
}
if (digitalRead(keyDown) == LOW) //This reads the state of key switch and controls movement reverse.
{
digitalWrite(REV, HIGH); //This sets the REV pin HIGH if the keyDown state is HIGH.
digitalWrite(FWD, LOW); //This sets the FWD pin LOW if the KeyDown state is HIGH.
}
else; //If keyDown is HIGH then FWD and REV are off.(; closes if)
{
digitalWrite(REV, LOW);
digitalWrite(FWD, LOW);
}
}
I believe this will work as I want it to but I do have a couple questions.
- In the loop do I have to write an if statement for the off position of my switch?
if(digitalRead(keyUp)==HIGH && digitalRead(keyDown)==HIGH){
digitalWrite(FWD, LOW);
digitalWrite(RVS, LOW);
}
(Can i put the ; at the end of the "};" or put it after my if(); - In the else statement if I put the ";" after the else does it mean the same thing if i put the ; after the ending of what I want the else to do?
else; //If keyDown is HIGH then FWD and REV are off.(; closes if)
{
digitalWrite(REV, LOW);
digitalWrite(FWD, LOW);
}
the same as
else //If keyDown is HIGH then FWD and REV are off.(; closes if)
{
digitalWrite(REV, LOW);
digitalWrite(FWD, LOW);
};