in random values to stepper motor, limit switch wont work plz help

hi,

i am new to arduino and i m working on a project in which am trying to rotate a stepper motor on random values (delay as well as off time). i have managed to write the code for random rotation and its working fine. but i also want a limit switch to have a function. when limit switch is pressed i want the motor to rotate for 20 seconds... but it wont work.. as much as i could figure out, when i lower the value of

randOn = random (100, 7200); // clockwise rotation time

to say 50,100 limit switch works fine but motors rotation time is reduced to a very minute time.

i have no idea how to go through this... could anyone please help

Regards

stepper_motor_limitswtich.ino (1.64 KB)

What is really connected to pin 13? A stepper or an LED?

int stepperpin1 = 13;                  // LED connected to digital pin 13

NOTE - I've never used a stepper motor...

What happens when you simplify your code and simply run the motor? i.e. Take out the randomness, the direction changes, and the limit sensing.

That's how you "develop" a program... Start with something simple... When that simple thing works, add another feature/function, and when that works, add some more... That way, when there's a problem, you know exactly where the problem is. If you are a beginning programmer, just add one or two lines of code at at time, always test-compiling and test-running before continuing (even if those one or two lines of code don't do anything yet).

The other major "trick" is to use the serial monitor so you can "see" variable values and what's going-on. For example, you can monitor your randOn variable, or see the state of your limit switch, or send-out a message that says "running clockwise", etc.

It doesn't look like you are using the stepper library. That's OK, but you are including it and setting-up some variables.

I may not be understanding your code, but it looks like you take one step clockwise and then take a step counterclockwise (with some random timing between each half-step).

i want the motor to rotate for 20 seconds..

I see a delay of 20 seconds, but your program can't do anything during the delay. ...You are pausing for 20 seconds, not running the motor for 20 seconds. The stepper library might run in the background during a delay, I don't know... But, that would be using the step() and setSpeed() functions instead of "manually" writing high & low to the stepper pins.

What kind of limit switch do you have? Usually a limit switch is only triggered temporarily. So if you only run clockwise while the switch is low and always run counterclockwise when it's high, you'll only run counterclockwise for a moment while the limit switch is high, and then it will run clockwise again... And the stepper will sit at the limit "chattering" back-and-forth...

const int step_360 = 12;// 360 number of steps per/rev                               
const int step_360_r = -60;// 360 number of steps per/rev

That doesn't look right to me. Most stepper motors are 200 steps per revolution. (But as far as I can tell, you never use those variables, so it's probably not hurting anything.)

@moonis, you need to post a link to the datasheet for your stepper motor and tell us what stepper motor driver you are using.

If you want a limit switch to be detected you need to move the motor one step at a time, checking the switch after each step.

In general never use delay() in anything other than a quick-and-dirty test program. Use millis() to manage timing as illustrated in Several Things at a Time.

I agree completely with @DVDdoug's advice for stepwise development of your program.

Random numbers make programs very hard to debug. Get the code working with fixed or known values to start with. You could simulate a succession of different values with an array. For testing each successive number would always be known. When it works you would probably have no problem with random values.

Another thing to watch for with random numbers is the upper and lower limits - can your code deal with them. They may not occur often enough to demonstrate a problem - until you want to show off your pride and joy.

...R
Stepper Motor Basics
Simple Stepper Code

thank u all for your replies. actually i am a visual arts student making an xy plotter which draws random abstract lines. i dont have any electronic or programming background (bit of programming i know since i have used flash and html) but i completely agree to the step wise development advice. infact thats how i got this far. i first tried to blink an led then add delays, then come to random numbers (l.e.d. traces can still be found in the code :frowning: ..

i did similar thing to motor as well. first i tried clock wise anti clock wise rotation, then adding delays, then adding randon randoff. the problem actually i am facing is after a while the x axis hits the side of the structure since it keeps on covering distance. so i want a limit switch to reverse the motion at that point of time. stepper motor datasheet unfortunately i dont have. since i bought this motor from some who was already using it and he didnt know about any datasheet the only information i cud find is mentioned on ebay as well... http://www.ebay.com/itm/Xerox-Phaser-7760-Takeaway-Motor-127K38881-/151788047070 .

i need the random values to the motor because if i put my own values the lines generated by the plotter would not be random.

also when i use the limit switch with no random values or less upper and lower limit, then the limit switch works fine but as soon as the value gap is increased it doesnt work. is there any other way of solving the problem.

thank u so much again

moonis:
i need the random values to the motor because if i put my own values the lines generated by the plotter would not be random.

But you don't want to use random values while you are developing the program

also when i use the limit switch with no random values or less upper and lower limit, then the limit switch works fine but as soon as the value gap is increased it doesnt work. is there any other way of solving the problem.

If the limit switch code works properly it will work with any value. The whole purpose of a limit switch is to prevent strange values from causing a problem.

Without seeing your latest code I can't comment further - I am assuming you have evolved it since your Original Post based on the advice so far.

...R

so i tried to simpify it as much as i cud. i added switch pin code on every pin low and high change with no random values at all... so when delay value is lower the switch pin works fine but when i increase it the pin doesnt work immediately it works after it accomplishes it on going task. which is not desirable. coz if it continues to do its task it will strike the ends of x axis...

int Pin = 13;                  //steppermotor  connected to digital pin 13
int Pin2 = 12;                  // steppermotor connected to digital pin 12
int switchPin = 5;




#include <Stepper.h>

const int step_360 = 12;// 360 number of steps per/rev                               
const int step_360_r = -60;// 360 number of steps per/rev 
// initialize the stepper library on pins 13-12 n 7-6
Stepper myStepper1(step_360,13,12);

void setup() 
{
  pinMode (switchPin, INPUT);
  myStepper1.setSpeed(1000);//left
  // initialize the serial port:
 Serial.begin(9600);
}
void loop() 
{
   if (digitalRead(switchPin) == LOW) {
     
   digitalWrite(Pin, HIGH);   // sets the motor on 13  on
   delay(100);
   }

   else {
      digitalWrite(Pin, HIGH); 
      digitalWrite(Pin2, LOW); 
      delay (20000);
      }

   if (digitalRead(switchPin) == LOW) {
    
   digitalWrite(Pin, LOW);    // sets the motor off
   delay(140);
   }

   else {
     digitalWrite(Pin, HIGH); // set pin high
     digitalWrite(Pin2, LOW); // set pin2 low
     delay (20000);
      }
      
  
   if (digitalRead(switchPin) == LOW) {
    
     digitalWrite(Pin2, HIGH);   // sets the motor on
     delay(100);
   }

    else {
     digitalWrite(Pin, HIGH); // set pin  high
     digitalWrite(Pin2, LOW); // set pin2 low
     delay (20000);
      }
  


    if (digitalRead(switchPin) == LOW) {
      
   digitalWrite(Pin2, LOW);    // sets the motor off
   delay(310);
    }
    
    else {
     digitalWrite(Pin, HIGH); // set pin high
     digitalWrite(Pin2, LOW); // set pin2 low
     delay (20000);
      }


}

If you are worried about missing an opportunity to read a limit switch, you would NOT be stuffing your head in the sand using delay().

what is that supposed to mean?

If you want to check a limit switch you should not have the delay() function anywhere in your program. The Arduino can do nothing during a delay(). You need to organize your code so that loop() repeats hundreds or thousands of times per second.

That also means you only need to read the switch position once in very interation of loop() - which greatly reduces the risk of silly errors.

Did you study the code in the link I gave you earlier Several Things at a Time

...R

@robin...

i did study it... i tried to rewrite the code according to it but i cudnt get it to work... can u please edit my code please since i am a beginner.. please help me with this.. i am not able to get it further..

moonis:
i tried to rewrite the code according to it but i cudnt get it to work...

Post you best attempt and we will try to help.

...R

thank u so much robin.... below is my code.. i tried to make the motor move simple and get the limit switch working so then i progress further to feed the motor with random values and if it hits the limit switch is hit then it rotates counter clockwise for 20 seconds. but i failed at the first step using millis method.

const int Pin = 13;                 // stepper motor 1 //pin1
const int Pin2 = 12;                // stepper motor 1 //pin 2
const int switchPin = 5;
const int buttonInterval = 300; // number of millisecs between button readings

const int motorDuration = 500; // number of millisecs that Led's are on - all three leds use this

unsigned long currentMillis = 0;
unsigned long previousPinMillis = 0;
unsigned long previousPin2Millis = 0;
unsigned long previousSwitchPinMillis = 0;


byte PinState = LOW;             // used to record whether the LEDs are on or off
byte Pin2State = LOW;           //   LOW = off
byte Pin_State = LOW;



void setup() 
{
  Serial.begin(9600);
  pinMode (switchPin, INPUT);
  pinMode (Pin, OUTPUT);
  pinMode (Pin2, OUTPUT);
  
  
 
}
void loop() 
{

   currentMillis = millis();   // capture the latest value of millis()


   readButton();
   updatePinState();
   updatePin2State();
   updateswitchPinState();

}
   
   void updatePinState(){
    if (PinState ==LOW) {
      if (currentMillis - previousPinMillis >= PinInterval) {

        PinState = HIGH;

        previousPinMillis += PinInterval;

        
      }
    }

  
    else {  
  
          
    if (currentMillis - previousPinMillis >= motorDuration) {
          // time is up, so change the state to LOW
      PinState = LOW;
          // and save the time when we made the change
       previousPinMillis += motorDuration;
    } 
  }
}


void switchPin() {
      

  digitalWrite(Pin, PinState);
  digitalWrite(Pin2, Pin2State);
}


void readButton() {

  
  if (millis() - previousSwitchPinMillis >= buttonInterval) {

    if (digitalRead(switchPin) == LOW) {
      Pin_State = ! Pin_State;
                                        
      previousSwitchPinMillis += buttonInterval;
    }
  }

}

Failed how? What is wrong?

I can understand why you are using millis() to control how often the button is read in the function readButton().

But I don't understand why you have a time control in the function updatePinState() - don't you want the LED ti change as soon as the button is pressed?

Also you seem to call some functions that don't exist - that won't get past the compiler. It is perfectly normal to create an empty function to keep the compiler happy while developing a program.

...R

cant u edit the code for me :-|... my mind is totally blank now.. plz

We usually look for signs of effort here before writing code for free.

moonis:
cant u edit the code for me :-|... my mind is totally blank now.. plz

You do not seem to have read my Reply #14 - you certainly have not replied to it.

...R