Code For motor with limit switches

Hi all,

I am trying to write write a program for a project I am underaking and am new to arduino so need guidance.

When button 1 is pressed the motor will run, exending an actuator, until limit switch 1 is hit
When button 2 is pressed the motor will run until limit switch 2 is pressed. In the motion of running until limit switch 2 is hit, limit switch 1 will be hit. This needs to have no effect.
When button 3 is hit the motor will reverse to the neutral position.

Any help wtih this would be greatly appreciated

When is our assignment due?
What have you already done?

April. I need to build a rig around the board so programming is he firs step but I dont really know much about arduino.

So far I grasp that the motor will be connected to one of the output pins so i think the code starts

void setup() {
// put your setup code here, to run once:
pinMode(9,OUTPUT);

}

void loop() {

then i need to declare what pins the limit switches and the control buttons are connected to.

After that i would get into the if statements within the program?

Am i on the right track?

No, the motor will be controlled by one of the output pins, it will not be connected to it directly

sorry, connected.

Arduino pin - breadbord - transistor base pin - emitter pin - motor

jacklevins:
the motor will reverse to the neutral position.

jacklevins:
transistor base pin - emitter pin

For the motor to reverse you need an h-bridge, which is tricky to construct from discrete components.

Have a look here at Pololu for a range to suit various voltage and current combos.

Maybe have a look at Planning and Implementing a Program

...R

int motorPin=9;
int limitSwitch1=8; //bottom limit(actuator retracted)
int limitSwitch2=7; //first extension(gaa)
int limitSwitch3=6; //max height(rugby)
int soccerSwitch=5; //move to soccer
int gaaSwitch=4; //move to gaa
int rugbySwitch=3; //move to rugby
int resetSwitch=2; //reset to bottom limit
void setup()
{
//declaring pin functions
pinMode(motorPin,OUTPUT);
pinMode(limitSwitch1,INPUT);
pinMode(limitSwitch2,INPUT);
pinMode(limitSwitch3,INPUT);
pinMode(soccerSwitch,INPUT);
pinMode(gaaSwitch,INPUT);
pinMode(rugbySwitch,INPUT);
}

void loop()
{
//reset to basic configuration
if(digitalRead(resetSwitch)==HIGH);
{
do{
digitalWrite(motorPin,HIGH);
}while(limitSwitch1==LOW);
}
//press botton for needed sport

if(digitalRead(soccerSwitch)==HIGH);
{
do{
digitalWrite(motorPin,HIGH);
}while(limitSwitch1==LOW);
}

if(digitalRead(rugbySwitch)==HIGH);
{
do{
digitalWrite(motorPin,HIGH);
}while(limitSwitch3==LOW);
}

if(digitalRead(gaaSwitch)==HIGH);
{
do{
digitalWrite(motorPin,HIGH);
}while(limitSwitch2==LOW);
}

}

Any input on this?

jacklevins:
Any input on this?

Give us a hint of what you have in mind?

What does your program actually do and what do you want it to do that is different.

And please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

...R

  do{
   digitalWrite(motorPin,HIGH);
 }while(limitSwitch3==LOW);

Oops

int pwm_a = 3;                    //PWM control for motor outputs 1 and 2
int dir_a = 2;                    // direction control for motor

int resetswitch = 11;             //reset switch. will put the post back to basic
int stopbutton = 10;              //stop button for emergency

int soccerswitch = 9;             //switches for each sport
int gaaswitch = 8;
int rugbyswitch = 7;

int bottomlimit = 6;              //limit switches to tell height post is at
int middlelimit = 5;
int toplimit = 4;


void setup ()
{
 
 pinMode(pwm_a,OUTPUT);          //motor outputs
 pinMode(dir_a,OUTPUT);
 
 pinMode(resetswitch,INPUT);     //input switches
 pinMode(stopbutton,INPUT);
 pinMode(soccerswitch,INPUT);
 pinMode(gaaswitch,INPUT);
 pinMode(rugbyswitch,INPUT);
 pinMode(bottomlimit,INPUT);
 pinMode(middlelimit,INPUT);
 pinMode(toplimit,INPUT);
 
}

void loop()
{

 //reset operation
 if (digitalRead(resetswitch)==HIGH)
 {
  
   digitalWrite(dir_a,LOW);
   analogWrite(pwm_a,100);
   }
   
 
   

 //soccer operation
 if(digitalRead(soccerswitch)==HIGH)
 {
   if(digitalRead(toplimit)==HIGH)
   {
     digitalWrite(dir_a,LOW);
     analogWrite(pwm_a,100);
   }
   
   else if (digitalRead(middlelimit)==HIGH)
   {
     digitalWrite(dir_a,LOW);
     analogWrite(pwm_a,100);
   }
   
   else if (digitalRead(bottomlimit)==HIGH)
   {
     digitalWrite(dir_a,LOW);
     analogWrite(pwm_a,0);
   }
 }


 //gaa operation
 if(digitalRead(gaaswitch)==HIGH)
 {
   if(digitalRead(toplimit)==HIGH)
   {
     digitalWrite(dir_a,LOW);
     analogWrite(pwm_a,100);
   }
   else if (digitalRead(middlelimit)==HIGH)
   {
     digitalWrite(dir_a,LOW);
     analogWrite(pwm_a,0);
   }
   else if (digitalRead(bottomlimit)==HIGH)
   {
     digitalWrite(dir_a,HIGH);
     analogWrite(pwm_a,100);
   }
 }

 //rugby operation
  if(digitalRead(rugbyswitch)==HIGH)
 {
   if(digitalRead(toplimit)==HIGH)
   {
     digitalWrite(dir_a,LOW);
     analogWrite(pwm_a,0);
   }
   else if (digitalRead(middlelimit)==HIGH)
   {
     digitalWrite(dir_a,HIGH);
     analogWrite(pwm_a,100);
   }
   else if (digitalRead(bottomlimit)==HIGH)
   {
     digitalWrite(dir_a,HIGH);
     analogWrite(pwm_a,100);
   }
 }


 
 //emergency stop button
 if(digitalRead(stopbutton)==HIGH)
 {
   digitalWrite(dir_a,LOW);
   analogWrite(pwm_a,0);
 }
}

this is what i now have. I am struggling to add in he limit switches o each section so when the motor hits a limit switch it will turn off. In this code he only way the motor is turned off is by hitting emergency stop button

Nice to see the GAA getting a mention.

I think you have your logic upside down. You should not design a program so you need to repeat code in different places.

Read ALL the switches in one place and save the values - something like this

soccerState = digitalRead(soccerswitch);
gaaState = digitalRead(gaaSwitch);

topLimitState = digitalRead(toplimitswitch);

etc etc

Then it seems that the limit switches are more important than the game type so check them first

if(toplimitState)==HIGH)
   {
     digitalWrite(dir_a,LOW);
     analogWrite(pwm_a,100);
   }
   
   else if (middlelimitState)==HIGH)
   {
     digitalWrite(dir_a,LOW);
     analogWrite(pwm_a,100);
   }
   
   else if (bottomlimitState)==HIGH)
   {
     digitalWrite(dir_a,LOW);
     analogWrite(pwm_a,0);

    }
    
    else {
     // deal with the different game types when no limit switches are pressed

   }

...R