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.
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
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
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
}