Pages: 1 [2]   Go Down
Author Topic: Could use some guidance with limit switches and a prox switch arduino mega  (Read 1475 times)
0 Members and 1 Guest are viewing this topic.
H-Town
Offline Offline
Newbie
*
Karma: 0
Posts: 11
"Never Look A Bounty Hunter In The Eyes"
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello Folks,
I am having this problem with my subroutine command not wanting to work in void loop. Could somebody further advise me to what I'm doing wrong. I can get it to work in set up, but of course it will not loop. If I take motorRight & motorLeft out of setup and place them into loop, my program does not exit setup into loop. Also, I can't get my 2nd subroutine to function at all. I must be missing something miniscule or mind is majorly malfunctioned.....  Maybe a little bit of both!
Thank You,
U_R

Code:
void setup() {
 
  // assign the pin connections for my motors
 
  motor2.set_enable_pin(7);
  motor2.set_direction_pin(6);
  motor2.set_step_pin(5);
  motor2.set_microstep_select_pins(A1);
   
  motor4.set_enable_pin(4);
  motor4.set_direction_pin(27);
  motor4.set_step_pin(11);
  motor4.set_microstep_select_pins(A2);
   
 // set the mag pin and the switch pins as inputs:
 
    pinMode (swP1, INPUT);
    pinMode (swP2, INPUT);
    pinMode (mP, INPUT);
    pinMode (enablePin, OUTPUT);

   while(digitalRead(swP1)== HIGH) {
   motor2.go(FULL,-900,10);
   if(digitalRead(swP1)==LOW) {
   goto finish;
    } 
   }
   finish:
   motor2.stall();
   delay(3000);
  motorRight();    //try code from if else program
  motorLeft();
}
 void loop() {}
 
 void motorRight ()
 {
    for(i=0; i<4; i++) {
    while (digitalRead(mP) == HIGH) {
    motor2.go(FULL, 2000, 10);
     
    if (digitalRead(mP) == LOW) { 
    motor2.stall();
    delay (3000);
   } 
  }
 } 
}
 void motorLeft ()
 {
    for(i=0; i>3; i--) {
    while (digitalRead(mP) == LOW) {
    motor2.go(FULL, -2000, 10);
     
    if (digitalRead(mP) == HIGH) { 
    motor2.stall();
    delay (3000);
   }
  } 
 }
}
Logged

U_R

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Also, I can't get my 2nd subroutine to function at all. I must be missing something miniscule or mind is majorly malfunctioned..... 
I'm voting for the latter. I've been writing code in C and C++ for almost 30 years now (it's how I make my living). !00s of thousands of lines of code, and not a single goto.

Yours is not needed, either. If the switch is low, go to a label. If it isn't, drop through to the same place. The goto is absolutely useless.

It would greatly improve the readability of your program if you put each { on a new line, and used the Tools + Auto Format menu item.

Meaningful names are important. One can't even begin to guess what mP means. Comments are important, too. We can see what the code is doing, but we have no idea what you expect it to be doing. If you have a comment like
Code:
// Run the left motor for 10 seconds
and code like
Code:
   while(millis() - then > 5000)
   {
       motorRight.go();
   }
Then we can see that there is (or is not) a discrepancy between what the comment says and what the code does.

It also helps to tell us what actually happens, along with what you expect. "It doesn't work" gives us nothing to go on.
Logged

H-Town
Offline Offline
Newbie
*
Karma: 0
Posts: 11
"Never Look A Bounty Hunter In The Eyes"
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes Sir,
  I do apologize for my incomprehensive programming skills. I am having trouble interrupting my motor with my limit switch. The program wants to complete the revolutions before stopping the motor. Programming micro controllers is a bit different than programming CNC machinery. So please, beat me up as much as it takes to get it through my thick skull.....
Thank You,
U_R

Code:
  pinMode (swP1, INPUT);  //limit switch left over travel motor #2 
  pinMode (swP2, INPUT);  //limit switch right over travel motor #4
  pinMode (mP, INPUT);     //mag sensor (proximity) switch between motors 2 & 4
  pinMode (enablePin, OUTPUT);

  while (digitalRead (swP1)==LOW)  //while limit switch 1 is OFF
{
    motor2.stall();                       //motor stop
    delay(3000);                         //3 sex
    motor2.go(FULL, -900, 10);    // motor 2 rotate CCW 4-1/2 rev's
                                              //THE MOTOR WANTS TO TURN FULL 4-1/2 REV'S BEFORE THE SWITCH STOPS IT   
    if (digitalRead (swP1)==HIGH)   //if limit switch 1 is ON
{                                             
    }
    else {
      break;
    }
  }
}
void loop()
{
  if (digitalRead(swP1) == HIGH)       //if limit switch 1 is ON
      if (digitalRead(swP1) == LOW)    //if limit switch 1 is OFF
{       
      digitalWrite(enablePin, HIGH);          // turn motor enable pin ON
      motor2.go(FULL, 2000, 10);            //THE MOTOR WANTS TO TURN FULL 10 REV'S BEFORE THE SWITCH STOPS IT
      delay (5000);                                //5 sec's   
    } 
  if (digitalRead(swP1) == HIGH)        // check if the input is HIGH
{   
    motor2.go(FULL, -2000, 10);       // step motor 2 CCW 10 rev's
                                                   //THE MOTOR WANTS TO TURN FULL 10 REV'S BEFORE THE SWITCH STOPS IT 
    digitalWrite(enablePin, HIGH);      // turn motor enable pin ON
    motor2.stall();                          //stop motor
    delay(3000);                             //3 sex
  }
}
Logged

U_R

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 108
Posts: 4014
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your incomplete code makes it very difficult to help.

Once this command is executed is there any way of stopping the motor before it has done 4.5 revs ?
Code:
    motor2.go(FULL, -900, 10);    // motor 2 rotate CCW 4-1/2 rev's

What do the parameters of the motor2.go function mean ?  You need to move the motor a little, check the switch, stop if at the limit else move the motor a little bit more and so on until the limit is reached or you have moved the complete distance.

Another question for you.  What should happen when swP1 is HIGH ?

Code:
    if (digitalRead (swP1)==HIGH)   //if limit switch 1 is ON
{                                             
    }
At the moment your code does everything between { and } above.  Is that right ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Pages: 1 [2]   Go Up
Jump to: