I have a Arduino Uno V3 and a Motor shield. I am trying to make a motor run for 3 different cases, very new to Arduino so not even sure I have the code written the best way.
Anyway I have everything functioning when I use Leds in place of the motor but when i change the code for the dc motor i have issues.
Here are the three cases I am looking to accomplish.
(1) if masterswitch is toggled high and top switch is high, toggle motor down until bottom switch is toggled high.
(2) if masterswitch is toggled high and bottom switch is high, toggle motor up until top switch is toggled high.
(3) if masterswitch is toggled high and bottom switch is Low and top switch is low, toggle motor up until top switch is toggled high.
^--Hopefully this makes sense.
Here is my code when using with LED's (This works good for me so far);
const int master_switch = 2;
const int top_switch = 3;
const int bot_switch = 4;
const int motor_up = 12;
const int motor_down = 13;
// switch states
int master_state = 0;
int top_state = 0;
int bot_state = 0;
void setup(){
pinMode(master_switch, INPUT);
pinMode(top_switch, INPUT);
pinMode(bot_switch, INPUT);
pinMode(motor_up, OUTPUT);
pinMode(motor_down, OUTPUT);
}
void loop(){
master_state = digitalRead(master_switch);
top_state = digitalRead(top_switch);
bot_state = digitalRead(bot_switch);
if(master_state == HIGH && bot_state == HIGH){
digitalWrite(motor_up, HIGH);
}
else if(top_state == HIGH){
digitalWrite(motor_up, LOW);
}
{
if(master_state == HIGH && top_state == HIGH){
digitalWrite(motor_down, HIGH);
}
else if(bot_state == HIGH){
digitalWrite(motor_down, LOW);
}
}
{
if(master_state == HIGH && bot_state == LOW && top_state == LOW){
digitalWrite(motor_up, HIGH);
}
else if(top_state == HIGH){
digitalWrite(motor_up, LOW);
}
}
}
And here is where I run into problems, the motor attempts to do something but no go.
const int master_switch = 2;
const int top_switch = 7;
const int bot_switch = 4;
const int motordir = 12;
const int motorbrake = 9;
const int motorpwm = 3;
// switch states
int master_state = 0;
int top_state = 0;
int bot_state = 0;
void setup(){
pinMode(master_switch, INPUT);
pinMode(top_switch, INPUT);
pinMode(bot_switch, INPUT);
pinMode(motordir, OUTPUT);
pinMode(motorbrake, OUTPUT);
}
void loop()
// If Master Switch is Toggled and current state is down, turn motor on up until top switch is toggled.
{
master_state = digitalRead(master_switch);
top_state = digitalRead(top_switch);
bot_state = digitalRead(bot_switch);
if(master_state == HIGH && bot_state == HIGH){
digitalWrite(motordir, HIGH);
digitalWrite(motorbrake, LOW);
analogWrite(motorpwm, 50);
}
else if(top_state == HIGH){
digitalWrite(motorbrake, HIGH);
}
//If master Switch is Toggled and current state is up, turn motor on down until bot switch toggled.
{
if(master_state == HIGH && top_state == HIGH){
digitalWrite(motordir, LOW);
digitalWrite(motorbrake, LOW);
analogWrite(motorpwm, 50);
}
else if(bot_state == HIGH){
digitalWrite(motorbrake, HIGH);
}
//Fail safe if power loss, If MasterSwitch is Toggled and Bottom and Top Switch are open, turn motor up on until top switch is toggled.
{
if(master_state == HIGH && bot_state == LOW && top_state == LOW){
digitalWrite(motordir, HIGH);
digitalWrite(motorbrake, LOW);
analogWrite(motorpwm, 50);
}
else if(top_state == HIGH){
digitalWrite(motorbrake, HIGH);
}
}
}
}
Thanks in advance for any help, if you have a better way to go about this I am all ears.