Thanks Thanks:  0
Needs Pictures Needs Pictures:  0
Picture(s) thanks Picture(s) thanks:  0
Page 5 of 11 FirstFirst 12345678910 ... LastLast
Results 61 to 75 of 163
  1. #61
    Join Date
    Jun 2010
    Location
    Canberra
    Posts
    769

    Default

    Quote Originally Posted by foobillious View Post
    click once at the top, scroll down to the bottom, hold down shift and click again... that'll highlight the lot. Its not much faster, but some people find it easier.
    Thanks for that - the select and drag thing is pretty tedious. I paste the code into an editor like Notepad++ as it colour-codes everything, making it a bit easier to read.

    In the checkInputs() function, direction is always initialised to zero, so if you take your finger off the direction button, it'll get set to zero every time that function is called, stopping the motor.

    I agree about being able to do more than one distance hop in the same direction - I didn't appreciate that a change in the direction value included changing from stopped to a direction.

    As for only one direction button working, have you checked that both limit inputs are high?

  2. # ADS
    Google Adsense Advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many





     
  3. #62
    Join Date
    Nov 2008
    Location
    somewhere
    Posts
    152

    Default

    ohhh good point. I'd intended to do something else there. Thanks for picking that up rusty.

    Updated version (direction pot only testing, speed pot still to come)
    Code:
    // Ken's Arduino Controled Camera Slider
    // based on code from Example6 of Brian Schmalz's Easy Driver Example page
    // http://www.schmalzhaus.com/EasyDriver/EasyDriverExamples.html
    //
    // Designed for Arduino UNO
    
    #include <Wire.h>
    #include <Adafruit_MotorShield.h>
    #include <AccelStepper.h>
    
    // Definitions - these make it easier to change what pins we are plugging things into on our arduino, and other control options such as speed
    // First Define the 3 digital input pins for direciton control
    #define LEFT_PIN 2
    #define STOP_PIN 3
    #define RIGHT_PIN 4
    // And the 2 digital inputs to test for limits
    #define LEFT_LIM_PIN 5
    #define RIGHT_LIM_PIN 6
    // and our two analog pot input pin
    #define SPEED_PIN A0
    #define DISTANCE_PIN A1
    // and our MAX SPEED and accelleration
    #define MAX_SPEED 500
    #define MAX_ACCEL 250                                    // set this to the same as MAX_SPEED if you don't want acceleration
    #define MIN_SPEED 0.1
    // and the minimuum and maximum distance in steps
    #define MAX_DISTANCE 800                // this max distance was selected based on 1 revolution being 200 steps causing a 78mm travel.  comes out at 312mm travel
    #define MIN_DISTANCE 0
    
    // Function forward definitions
    void forwardstep();
    void backwardstep();
    char checkInputs(char direction);
    char checksetIdle(char *direction);
    void checksetDistance(char direction,char olddir);
    char checkDTG();
    float calcSpeed(int speedpot,int direction);
    float calcDistance(int destpot);
    
    // Global Variables:
    Adafruit_MotorShield AFMS = Adafruit_MotorShield();
    Adafruit_StepperMotor *motor1=AFMS.getStepper(200,2);
    AccelStepper stepper1(forwardstep,backwardstep);
    
    // initialise any global variables and tell the Arduino what to use the various pins for
    void setup()
    {
        AFMS.begin();
        
        // Tell AccelStepper what the maximum speed should be in steps per second.
        stepper1.setMaxSpeed(MAX_SPEED);
        stepper1.setAcceleration(MAX_ACCEL);
        
        // set up the 3 input button inputs and the limit inputs with pullps
        pinMode(LEFT_PIN, INPUT_PULLUP);
        pinMode(STOP_PIN, INPUT_PULLUP);
        pinMode(RIGHT_PIN, INPUT_PULLUP);
        pinMode(LEFT_LIM_PIN, INPUT_PULLUP);
        pinMode(RIGHT_LIM_PIN, INPUT_PULLUP);
    }
    
    // This is where the main body of the work gets done.  The loop function gets called repeatedly 
    void loop()
    {
        static float current_speed=0.0;                // holds the current motor speed in steps/second
        static int analog_read_counter=1000;                // counts down to 0 to fire analog read
        static char direction=0;                // holds -1, 1 or 0 to control direction
        char enabled;                        // used to keep track of if the motor has been enabled
        static int speed_value;                    // holds the raw analog value of the speed
        char olddir;                        // used to see if the direction has changed
        
        // step 1 - check inputs
        olddir=direction;
        direction=checkInputs(direction);                // get the direction we should be moving in, based on inputs from the dir switches and limits
        checksetDistance(direction,olddir);                // check and set a new distance from the current point, if necessary
        enabled=checksetIdle(&direction);                // check if the motor should be idling or not (note this may set the directuib to 0 if we have reached the specified distance)
        
        // step 2 - run the motor
    //    if (enabled==1)
    //    {
    //        stepper1.run();                    // let the motor step if it needs to before we change the speed
    //    }
    //    current_speed=calcSpeed(speed_value,direction);        // calculate what our speed should be based on last pot setting and direciton
    //    stepper1.setSpeed(current_speed);
        if (enabled==1) {
            stepper1.run();
        }
    /*    
        // step 3 - update analog values, if necessary
        if (analog_read_counter>0)
        {
            analog_read_counter--;
        }
        else
        {
            analog_read_counter=3000;                // change this to a lower number to increase pot response
            speed_value=analogRead(SPEED_PIN);        // get the speed value
                                    // distance value read could go here, but as it isn't updated all that often
                                    // its easier just to do it when necessary
        }
    */
    }
    
    // this function will be called by the AccelStepper every step the motor turns forward
    void forwardstep()
    {
            // you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
        motor1->onestep(FORWARD, SINGLE);
    }
    
    // this function will be called by the AccelStepper every step the motor turns backward
    void backwardstep()
    {
          // you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
        motor1->onestep(BACKWARD, SINGLE);
    }
    
    // this function checks our inputs and returns a number indicating if the motor will turn LEFT (1), RIGHT (-1)
    // or if it should stop (0)
    char checkInputs(char direction)
    {   
        if (digitalRead(LEFT_PIN)==0 && digitalRead(LEFT_LIM_PIN)==1)
        {
            direction=1;
        }
        else if (digitalRead(RIGHT_PIN)==0 && digitalRead(RIGHT_LIM_PIN)==1)
        {
            direction=-1;
        }
        else if (digitalRead(LEFT_LIM_PIN)==0 || digitalRead(RIGHT_LIM_PIN)==0 || digitalRead(STOP_PIN)==0)
        {
            direction=0;
        }
        return direction;
    }
    
    // this function checks if our motor should idle or not, based on the direction. it also puts the motor into the idle state
    // returns 0 if the motor is in the idle state
    char checksetIdle(char *direction)
    {
            static char lastval=0;
            
        if (*direction==0)
        {
            motor1->release();
                    lastval=0;
                    stepper1.disableOutputs();
            return 0;
        }
            if (checkDTG()==0)
            {
                    motor1->release();
                    *direction=0;
                    lastval=0;
                    stepper1.disableOutputs();
                    return 0;
            }
            if (lastval==0)
            {
                    stepper1.enableOutputs();
                    lastval=1;
            }
        return 1;
    }
    
    // this function reclculates the speed for the motor, based on the last speed potentiometer value and the direction.
    // returns a float indicating what the speed should be
    float calcSpeed(int speedpot,int direction)
    {
        return (direction * ((speedpot/1023.0) * (MAX_SPEED-MIN_SPEED)) + MIN_SPEED);
    }
    
    // this function calculates how far we should move before stopping, based on the last destination potentiometer value
    long calcDistance(int destpot, int direction)
    {
        return (direction * ((destpot/1023.0) * (MAX_DISTANCE-MIN_DISTANCE)) + MIN_DISTANCE);
    }
    
    // this function determines if a new distance to move is necessary, and provides a new distance relative to the current position to the AccelStepper library.
    void checksetDistance(char direction,char olddir)
    {
        int dist_value;                        // holds the raw analog value of the distance
        long distance;                        // holds how far we are going to move
        
        if (direction==0 || olddir==direction)                // we only update the distance if we are about to move (direction is set), and we are changing direction (olddir doesn't equal the new dir)
            return;
            
        dist_value=analogRead(DISTANCE_PIN);            // read the distance pot to figure out how far we have to move
        distance=calcDistance(dist_value,direction);        // calculate the distance
        stepper1.move(distance);                // set the distance to move from the current position
    }
    
    // check the distance to go to determine if we should idle the motor or not.
    char checkDTG()
    {
        if (stepper1.distanceToGo()>0)
        {
          return 1;
        }
        return 0;
    }

  4. #63
    Join Date
    Nov 2006
    Location
    Heidelberg, Victoria
    Age
    79
    Posts
    2,251

    Default Camera slider update

    Hi gentlemen,

    I swapped out the limit switches for a couple of push buttons, (reckon the limit switch wiring was a bit dickie), and all is well.

    The last sketch compiles ok and runs despite the error message "avrdude: st500 blah blah.

    I'm just a shade confused, is the distance pot set up but not implemented yet? Was Rusty's concerns justified?

    Ken

  5. #64
    Join Date
    Jun 2010
    Location
    Canberra
    Posts
    769

    Default

    Quote Originally Posted by neksmerj View Post
    I'm just a shade confused, is the distance pot set up but not implemented yet? Was Rusty's concerns justified?
    I haven't read through the latest code, but I believe the distance pot should work, and you now don't have to hold down the direction button - you should be able to press a direction button briefly and the stepper should run for a fixed distance and stop.

  6. #65
    Join Date
    Nov 2006
    Location
    Heidelberg, Victoria
    Age
    79
    Posts
    2,251

    Default Distance pot not working

    Mmmmm, the distance pot does not seem to function. Twiddling the pot has no effect. The voltage on pin A1 definitely swings from GND to 5V by turning the pot.

    I'm beginning to think that the new sketch is not being compiled, and that an old sketch is still in the Arduino memory.

    The reason I think this is because the little orange light on the Arduino does not blink during an upload. It usually does but now doesn't.

    Again I ask, is it possible to erase the memory of the Arduino?

    Ken

  7. #66
    Join Date
    Nov 2008
    Location
    somewhere
    Posts
    152

    Default

    yeah, i'm pretty sure your still running the old code. that avrdude error means that the upload didn't happen. The arduino wipes itself with the new upload, but you havn't got there yet unfortunately.

    If you can sort that avrdude error (avrdude is what is actually doing the upload), you might be able to progress things. try checking what com port your arduino is actually on.

    *edit* oh, and to clarify, the distance pot is set up and implemented and we are trying to test if it works. Rusty did identify a bug in one of my functions that would have meant that the button would need to be held down to get movemen. That should no longer be necessary, with the latest version of the code (I think). Your also spot on with that LED. if it doesn't blink the code doesn't update.

  8. #67
    Join Date
    Nov 2006
    Location
    Heidelberg, Victoria
    Age
    79
    Posts
    2,251

    Default Almost there.

    Hi foob.

    Just about there. Swapping ports solved the compiling issue.

    The distance pot works beautifully, however only in the fwd direction. Pressing the rev push button does nothing.

    It was fine running under an older sketch, and no wiring changes have occurred since.

    A digital probe on the appropriate pin, D4, indicates high until pressed, so the press button is fine.

    Ken

  9. #68
    Join Date
    Nov 2008
    Location
    somewhere
    Posts
    152

    Default

    great ken - I'll take a look and see if I can figure out why it doesn't work going the other way.

  10. #69
    Join Date
    Nov 2008
    Location
    somewhere
    Posts
    152

    Default

    Having a bit of trouble finding whats wrong with this.

    Ken, what happens if you hit the fwd movement button a second time? does it move the same distance again?

  11. #70
    Join Date
    Jun 2010
    Location
    Canberra
    Posts
    769

    Default

    Quote Originally Posted by neksmerj View Post
    A digital probe on the appropriate pin, D4, indicates high until pressed, so the press button is fine.
    Have you also checked that the right limit pin (D6) is also high?

    When debugging code, it's *really* important to ensure the wiring is correct, and to make sure the power supply is good, otherwise you can end up chasing a problem in code when it's actually on the breadboard.

  12. #71
    Join Date
    Nov 2006
    Location
    Heidelberg, Victoria
    Age
    79
    Posts
    2,251

    Default Camera slider update

    Gentlemen,

    I have thoroughly checked out all the switch functions with a digital probe, and all seems well.

    Rusty, to answer your question re pushing the fwd button twice, push once, the motor does a few turns then stops, press again, the motor turns then stops.

    I ran an earlier sketch and both direction buttons work including the limit switches

    That sketch is here

    Code:
    / Example6 code for Brian Schmalz's Easy Driver Example page// http://www.schmalzhaus.com/EasyDriver/EasyDriverExamples.html
    
    
    
    
    #include <AccelStepper.h>
    #include <Wire.h>
    #include <Adafruit_MotorShield.h>
    
    
    // Define the stepper and the pins it will use
    Adafruit_MotorShield AFMS = Adafruit_MotorShield();
    Adafruit_StepperMotor *motor1 = AFMS.getStepper(200, 2);
    
    
    
    
    // you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
    void forwardstep() { 
      motor1->onestep(FORWARD, DOUBLE);
    }
    void backwardstep() { 
      motor1->onestep(BACKWARD, DOUBLE);
    }
    
    
    AccelStepper stepper1(forwardstep, backwardstep); // use functions to step
    
    
    // Define our three input button pins
    #define  LEFT_PIN  2
    #define  STOP_PIN  3
    #define  RIGHT_PIN 4
    // and define our two Limit inputs
    #define LEFT_LIM_PIN 5
    #define RIGHT_LIM_PIN 6
    
    
    
    
    // Define our analog pot input pin
    #define  SPEED_PIN A0
    
    
    
    
    // Define our maximum and minimum speed in steps per second (scale pot to these)
    #define  MAX_SPEED 500
    #define  MIN_SPEED 0.1
    
    
    
    
    void setup() {
      AFMS.begin();
     
      // The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go
      stepper1.setMaxSpeed(1000.0);
     
      // Set up the three button inputs & limit inputs with pullups
      pinMode(LEFT_PIN, INPUT_PULLUP);
      pinMode(STOP_PIN, INPUT_PULLUP);
      pinMode(RIGHT_PIN, INPUT_PULLUP);
      pinMode(LEFT_LIM_PIN, INPUT_PULLUP);
      pinMode(RIGHT_LIM_PIN, INPUT_PULLUP);
    }
    
    
    
    
    void loop() {
      static float current_speed = 0.0;         // Holds current motor speed in steps/second
      static int analog_read_counter = 1000;    // Counts down to 0 to fire analog read
      static char sign = 0;                     // Holds -1, 1 or 0 to control direction
      static char enabled=0;                    // used to keep track of if the motor has been enabled (used to allow the motors to idle)
      static int analog_value = 0;              // Holds raw analog value.
      
     
      if (digitalRead(LEFT_PIN)==0 && digitalRead(LEFT_LIM_PIN)==1) {   // allow movement LEFT if the LEFT limit switch isn't hit
          sign=1;
      } else if (digitalRead(RIGHT_PIN)==0 && digitalRead(RIGHT_LIM_PIN)==1) {  // allow movement RIGHT if the RIGHT limit switch isn't hit
         sign=-1;
      } else if (digitalRead(LEFT_LIM_PIN)==0 || digitalRead(RIGHT_LIM_PIN)==0 || digitalRead(STOP_PIN)==0) { // if neither of the above is true, and a limit or stop switch has been hit then stop the motors
         sign=0;
         stepper1.setSpeed(sign);
      }
      
      if (sign!=0) {                          // motor has a valid direction 
        if (enabled==0) {                     // motor was previously disabled, re-enable it (but don't re-enable it every single time we go through the loop)
          enabled=1;
          stepper1.enableOutputs();
        }
      } else {                                  // a limit switch or the stop switch has been hit.  disable the output to the motor.
        enabled=0;
        stepper1.disableOutputs();    
      }
    
    
      // Give the stepper a chance to step if it needs to
      if (enabled==1) {
        stepper1.runSpeed();
      }
      //  And scale the pot's value from min to max speeds
      current_speed = sign * ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
      // Update the stepper to run at this new speed
      stepper1.setSpeed(current_speed);
      // This will run the stepper at a constant speed, if the motor is currently enabled.
      if (enabled==1) { 
        stepper1.runSpeed();
      }
    
    
      // We only want to read the pot every so often (because it takes a long time we don't
      // want to do it every time through the main loop). 
      if (analog_read_counter > 0) {
        analog_read_counter--;
      }
      else {
        analog_read_counter = 1000;  //  <-- change this to a lower number (i.e 500 or 1000) to increase pot response
        // Now read the pot (from 0 to 1023)
        analog_value = analogRead(SPEED_PIN);
      }
    }
    With respect to the distance control, I did not get my thoughts across properly. What I would like happen is this-

    Press the fwd button and the motor does a few turns according to the pot setting, then stops for a second while a shot is being taken. After one second the motor does a few more turns then stops etc etc automatically.

    I feel so inadequate that I can't help in writing the code, and greatly appreciate the amount of time you gentlemen have given me.

    Ken

  13. #72
    Join Date
    Nov 2008
    Location
    somewhere
    Posts
    152

    Default

    okay, thanks Ken. I've clearly made a logic error somewhere, so I'll work through and try and identify where.

    I did understand what you want to get to with the distance pot, but before getting it working with a delay in the middle we need to get it working at all, otherwise debugging it just becomes harder. Its already fairly hard, as other than checking the code compiles I don't have any way to test this is working.

    the code to go to the delay is about 4 lines long (I think). That'll be last, after we've got it travelling the set distances in both directions, and at different speeds. Speeds should be fairly straightforward too. We can't just use runSpeed() like we did before though, because it (runSpeed()) ignores the distance setting.

  14. #73
    Join Date
    Nov 2006
    Location
    Heidelberg, Victoria
    Age
    79
    Posts
    2,251

    Default Camera slider update

    Hi foob,

    mate, you are a genius, how on earth you can write all this code and see it working in your head without the actual hardware.

    I need a kick up the bum, power for the stepper stopped, so assumed the el cheapo 12V plug pack had failed. Went out and bought another one, plugged it in, and the same story, no juice.
    Turned out to be the bloody power point had gone belly up.

    Thanks for the refresher re the distance code.

    Incidently, when I press the fwd push button, the stepper rotates slightly in the reverse direction before rotating in the correct direction. Is this a clue?

    Ken

  15. #74
    Join Date
    Nov 2008
    Location
    somewhere
    Posts
    152

    Default

    Its a lots of practice thing, not a genius thing... rusty can probably tell you, but that code is a bit of a mess... I changed my mind how it was going to work about 3 times while putting it together, and it kinda shows. poor programming practice really. In theory somebody who can program could read the source code to a program and tell you exactly what its going to do. You shouldn't actually have to be able to run it to see whats going to happen.

    Unfortunately I'm having a bit of a mental block where I've got a picture of how the code works in my head and your reporting that it is doing things that it shouldn't (or not, as in this case). I then look at it and can't get past the "but it has to work!" to figure out why not. Frustrating. Fortunately she who must be obeyed has demanded some housework, so hopefully that'll shake the cobwebs loose enough. I'll take a look at it again in a couple of hours when I'm done.

    Yep, the reverse thing is definately a clue, because it shouldn't do that either. actually that gives me an idea of where to look. gah! housework.

  16. #75
    Join Date
    Nov 2006
    Location
    Heidelberg, Victoria
    Age
    79
    Posts
    2,251

    Default Camera slider update

    Hi foob,

    I'm hoping by now you will have washed and dried the dishes, swept the floor, vacuumed the carpets, done the ironing, put the bins out, cooked the tea, taken the dog for a walk and cleaned up his mess.

    Have I missed anything, oh yeh, the washing machine needs to be reloaded after you've cut the grass. I'd much rather be writing code, if I could, what about you?

    There's just one last request, and that is, when the stepper stops, and lets assume it stops for one second, half a second after stopping, a 5v pulse is output to one of the spare pins, maybe pin 13.

    This signal will be used to fire the camera shutter. Haven't figured this out yet, but I'm sure there will be something on the web.

    Ken

Page 5 of 11 FirstFirst 12345678910 ... LastLast

Similar Threads

  1. Limit/home switches
    By warrick in forum CNC Machines
    Replies: 2
    Last Post: 7th July 2012, 08:11 PM
  2. Limit switches
    By Bob Willson in forum CNC Machines
    Replies: 0
    Last Post: 24th August 2011, 12:31 PM
  3. CNC Mill Limit Switches
    By electrosteam in forum METALWORK FORUM
    Replies: 8
    Last Post: 10th September 2010, 07:31 PM
  4. How to Wire Limit switches on Xylotex board.
    By Ch4iS in forum CNC Machines
    Replies: 19
    Last Post: 19th October 2008, 09:58 PM
  5. Xylotex & limit/home switches
    By John H in forum CNC Machines
    Replies: 3
    Last Post: 31st July 2008, 08:08 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •