Go Down

Topic: code not using limit switch input (Read 103 times) previous topic - next topic

isaacs77

Hi guys, in the code below the stepper motor is being stuck in the first loop of the function regardless of the value of the limit switch. The limit switch is working because I put an led across one side and the other to ground and it lit up but for some reason the Arduino isn't recognising that the limit switch has been hit and continuing the stay in the first while loop in the goHome() function.

pls help

Code: [Select]

  #include <Stepper.h>

const int stepRate = 400;

Stepper myStepperX(stepRate,  8, 9, 10, 11);
Stepper myStepperY(stepRate,  4, 5, 6, 7);

const int systemOn = 14;
const int startButton = 15;
//const int returnHome =31;
const int led =  16;
const int limitY=31;
const int limitX=30;

int startButtonState = 0;
//int returnHomeState = 0;           
int systemOnState =  0;
 int imHome= 0;
 int xState =0;
 int yState=0;

 void goHome()
{
   while (digitalRead(limitX)== HIGH)
         {
            myStepperX.step(-stepRate);
            delay(500);
         }
 
   while (digitalRead(limitY)==HIGH)
          {
             myStepperY.step(-stepRate);
             delay(500);
          }
}

void setup()
{
  myStepperY.setSpeed(100);
  myStepperX.setSpeed(100);// set the speed at 100 rpm
 
  Serial.begin(9600);
 
  pinMode(led, OUTPUT);
  pinMode(startButton,  INPUT);
//  pinMode(returnHome,  INPUT);
  pinMode(limitX, INPUT_PULLUP);
  pinMode(limitY, INPUT_PULLUP);
  pinMode(systemOn, INPUT);
           

}

void loop() {

int stepCount = 0;

systemOnState= digitalRead(systemOn);

if (systemOnState==1)
{

xState=digitalRead(limitX);
yState=digitalRead(limitY);

goHome();
 
startButtonState=digitalRead(startButton);
 
  if (startButtonState==1)
 {
   
    for ( int stepYCount =0; stepYCount <33; stepYCount++)
    {
      digitalWrite(led, 1);
      myStepperY.step(stepRate);
      delay(500);
    }
    myStepperX.step(stepRate);
   
   for ( int stepYCount2 =0; stepYCount2 <33; stepYCount2++)
    {
      digitalWrite(led, 1);
      myStepperY.step(-stepRate);
      delay(500);
    }
    myStepperX.step(stepRate);
   
for ( int stepYCount3 =0; stepYCount3 <33; stepYCount3++)
    {
      digitalWrite(led, 1);
      myStepperY.step(stepRate);
      delay(500);
    }
    myStepperX.step(stepRate);
   
    for ( int stepYCount4 =0; stepYCount4 <33; stepYCount4++)
    {
      digitalWrite(led, 1);
      myStepperY.step(-stepRate);
      delay(500);
    }
    myStepperX.step(stepRate);
   
    for ( int stepYCount5 =0; stepYCount5 <33; stepYCount5++)
    {
      digitalWrite(led, 1);
      myStepperY.step(stepRate);
      delay(500);
    }
    myStepperX.step(stepRate);

    for ( int stepYCount6 =0; stepYCount6 <33; stepYCount6++)
    {
      digitalWrite(led, 1);
      myStepperY.step(-stepRate);
      delay(500);
    }
    myStepperX.step(stepRate);
   
    for ( int stepYCount7 =0; stepYCount7 <33; stepYCount7++)
    {
      digitalWrite(led, 1);
      myStepperY.step(stepRate);
      delay(500);
    }
    myStepperX.step(stepRate);
    for ( int stepYCount8 =0; stepYCount8 <33; stepYCount8++)
    {
      digitalWrite(led, 1);
      myStepperY.step(-stepRate);
      delay(500);
    }
    myStepperX.step(stepRate);
    for ( int stepYCount9 =0; stepYCount9 <33; stepYCount9++)
    {
      digitalWrite(led, 1);
      myStepperY.step(stepRate);
      delay(500);
    }
    myStepperX.step(stepRate);
    for ( int stepYCount10 =0; stepYCount10 <33; stepYCount10++)
    {
      digitalWrite(led, 1);
      myStepperY.step(-stepRate);
      delay(500);
    }
    myStepperX.step(stepRate);
    for ( int stepYCount11 =0; stepYCount11 <33; stepYCount11++)
    {
      digitalWrite(led, 1);
      myStepperY.step(stepRate);
      delay(500);
    }
    myStepperX.step(stepRate);
    for ( int stepYCount12 =0; stepYCount12 <33; stepYCount12++)
    {
      digitalWrite(led, 1);
      myStepperY.step(-stepRate);
      delay(500);
    }
    myStepperX.step(stepRate);
myStepperX.step(stepRate);
    for ( int stepYCount13 =0; stepYCount13 <33; stepYCount13++)
    {
      digitalWrite(led, 1);
      myStepperY.step(stepRate);
      delay(500);
    }
    myStepperX.step(stepRate);
   
myStepperX.step(stepRate);
   
    for ( int stepXHome =0; stepXHome <13; stepXHome++)
    {
      myStepperX.step(-stepRate);
    }
  }
}
}




MorganS

How are your switches wired?

Do you know if it's getting stuck in the X or Y section? How have you verified that? Can you be sure that you didn't swap the X or Y switches?
"The problem is in the code you didn't post."

Go Up