Thanks Thanks:  0
Needs Pictures Needs Pictures:  0
Picture(s) thanks Picture(s) thanks:  0
Page 8 of 11 FirstFirst ... 34567891011 LastLast
Results 106 to 120 of 163
  1. #106
    Join Date
    Nov 2008
    Location
    somewhere
    Posts
    152

    Default

    well, the weekend is here, and all through the workshop there came the sounds of tinkering...
    then with a bang, and a flash the lead elf cooked a precision adc
    when he hooked up the 24v power supply to the 3.3V power pin..

    whups
    moral: just because you normally use standard coloured wires from your power supply doesn't mean you shouldn't actually check you havn't changed over to an output you normally don't use and then forgotten about it.

    Anyway, the project I was working on has had the magic smoke let out of it, and while I wait for replacement parts Ken's project gets bumped up the line a little bit.
    Ken, could you please give this code below a test.
    All pins are the same as before (I think), but you might want to check that. I've simplified the code a fair bit, to make things easier to figure out whats going wrong, and I've got some output coming to the serial monitor that might help with the debugging.

    Expected behaviour is that it will sit there toggling pin13 on for 250uS every second (stick an led going to ground via a 500ohm resistor on that pin and it should pulse briefly each second) and sending "waiting on input - Current Position: 0" to the serial monitor (in the arduino software go to Tools->Serial monitor - your looking for 9600baud traffic)

    when you hit one of the movement pins it should start moving in the relevant direction - you'll see "Updating position to: <number>". You may need to hold the button down briefly, if the docamera function is in the middle of its delay (probably will be). I'll fix that later. Once it gets to the specified position it should just stop.

    Can you check that each of the buttons works as expected? A stop button or a limit switch being hit should stop the motor straight away. I'm still a bit blind as I don't have a free stepper motor to try it with, but the direction stuff seems to be working.

    Please note your speed knob and your direction distance knob will do nothing at this point.

    Code:
    /* 
    Round 2 of testing for Ken's Camera Slide project
    */
    #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
    #define STEP_TYPE SINGLE          // you can change this to DOUBLE, SINGLE, INTERLEAVE or MICROSTEP to control the step type
    
    // 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 MAX SPEED and accelleration
    #define MAX_SPEED 500
    #define MAX_ACCEL 250
    
    // define our distance to travel
    #define DISTANCE 500
    
    // define parameters for camera
    #define CAMERA_PIN 13
    #define CAMERA_PULSE 250                  // pulse time in uSeconds
    
    // Create the motor shield object with the default I2C address
    Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
    
    // Connect a stepper motor with 200 steps per revolution (1.8 degree)
    // to motor port #2 (M3 and M4)
    Adafruit_StepperMotor *motor1 = AFMS.getStepper(200, 2);
    AccelStepper stepper1(forwardstep, backwardstep); // use functions to step
    
    void setup() {
      Serial.begin(9600);           // set up Serial library at 9600 bps
      Serial.println("Stepper test for Ken");
    
      AFMS.begin();  // create with the default frequency 1.6KHz
      //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
      
      stepper1.setMaxSpeed(MAX_SPEED);
      stepper1.setAcceleration(MAX_ACCEL);
    }
    
    void loop() {
      static char dir=0;
      long newpos;
    
    
      // okay if we have no distance left to go, we should be idling.
      if (stepper1.distanceToGo() == 0)
      {
        dir=0;
        motor1->release();
        Serial.print("waiting on input -");
        Serial.print("Current position: ");
        Serial.println(stepper1.currentPosition());
        doCamera();
      }
      dir=checkInputs(dir);
      if (dir==1)
      {
        newpos=stepper1.currentPosition() + 500;
        stepper1.moveTo(newpos);
        Serial.print("Updating position to: ");
        Serial.println(newpos);
      }
      if (dir==-1)
      {
        newpos=stepper1.currentPosition()-500;
        stepper1.moveTo(newpos);
        Serial.print("Updating position to: ");
        Serial.println(newpos);
      }
      if (dir==2)  // stop button hit
      {
        stepper1.moveTo(stepper1.currentPosition());
        motor1->release();
        dir=0;
      }
      stepper1.run();
    }
    
    // this function will be called by the AccelStepper every step the motor turns forward
    void forwardstep()
    {
        motor1->onestep(FORWARD, STEP_TYPE);
    }
    
    // this function will be called by the AccelStepper every step the motor turns backward
    void backwardstep()
    {
        motor1->onestep(BACKWARD, STEP_TYPE);
    }
    
    
    // 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 dir)
    {   
        if (digitalRead(LEFT_PIN)==0 && digitalRead(LEFT_LIM_PIN)==1)
        {
            dir=1;
        }
        else if (digitalRead(RIGHT_PIN)==0 && digitalRead(RIGHT_LIM_PIN)==1)
        {
            dir=-1;
        }
        else if (digitalRead(STOP_PIN)==0 || digitalRead(RIGHT_LIM_PIN)==0 || digitalRead(STOP_PIN)==0)
        {
            dir=2;
        }
        else dir=0;
        return dir;
    }
    
    char doCamera()
    {
      // concept here is that when we stop (direction==0), we want to delay for a while, then take a shot, delay some more, then continue moving in the same direction we were before
          delay(500);                            // delay half a second after stopping
          digitalWrite(CAMERA_PIN,HIGH);         // drive the camera pin high
          delay(CAMERA_PULSE);                   // leave it high for the pulse time
          digitalWrite(CAMERA_PIN,LOW);          // drive the camera pin low
          delay(500-CAMERA_PULSE);               // snooze for the rest of the second
    }

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





     
  3. #107
    Join Date
    Nov 2006
    Location
    Heidelberg, Victoria
    Age
    79
    Posts
    2,251

    Default Camera slider update

    Hi foob,

    The dreaded smoke syndrome, it happens to all of us. I've got used to sticking my finger on the motor driver chip to test for heat.

    OK, I uploaded your sketch, and immediately the stepper rotated about half a turn and stopped. Pressing any of the push buttons had no effect.

    I reset the Arduino hoping to get some response, nothing. Looking at the serial monitor, it was scrolling down with "Waiting input-Currenttion:0"

    Pressing any of the buttons had no effect. The serial monitor just kept scrolling down with no sign of life from the motor.

    So, not able to test anything else.

    foob, again I'm happy to lend you an Arduino UNO with Adafruit motor shield on top all mounted on a small board with mini breadboard.

    Just PM me your address and it will be done.

    Ken

  4. #108
    Join Date
    Nov 2008
    Location
    somewhere
    Posts
    152

    Default

    Thanks, but I'm fine on the uno front, and i've got a motorshield v2 now as well.

    Okay, thats kind of weird. On mine the motor doesn't get any pulses until you push one of the buttons. anyway, lets start with the simple checks. Is your serial monitor set up for 9600 baud comms? And can you please check that all your switches are set up so they have a pull up resistor to +5V and the other side connected to ground. The input to the arduino pins should be from the junction between the resistor and the switch. Thats assuming the switches are normally open of course.

  5. #109
    Join Date
    Nov 2006
    Location
    Heidelberg, Victoria
    Age
    79
    Posts
    2,251

    Default Camera slider update

    Hi foob,

    Yes the baud rate is set correctly and all the switches are programmed to have pull-ups.

    I know my setup works as I use an early sketch of yours to test it.

    I've just added a couple of lines at the bottom of your sketch to pause the motor and pulse pin13, the camera pin. It's crude but it's simple and seems to work ok.

    I tried adding the "moveTo command etc", as you'll see in the sketch, commented out, but that didn't work.

    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, MICROSTEP);
      //motor1->onestep(FORWARD, MICROSTEP);
    }
    void backwardstep() { 
      motor1->onestep(BACKWARD, MICROSTEP);
      //motor1->onestep(BACKWARD, MICROSTEP);
    }
    
    
    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 and camera pin
    #define LEFT_LIM_PIN 5
    #define RIGHT_LIM_PIN 6
    #define CAMERA_PIN 13 
    
    
    
    
    // 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(500.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);
      pinMode(13,OUTPUT);  //camera shutter pin
    
    
    }
    
    
    
    
    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;
        motor1->release();
        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 = 500;  //  <-- 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);
        delay (1000);              //Stop the motor
        
        //stepper1.setMaxSpeed (3000);
        //stepper1.setAcceleration (200);
        
        //stepper1.moveTo (100);
        //while (stepper1.distanceToGo ()!=0)
        //stepper1.run ();
          
        if (sign==0) {            // If motor is stopped by stop button or limit switch
        digitalWrite (13,LOW);    // disable camera pin.
      }else digitalWrite (13,HIGH); // Activate camera pin 
      delay (250);                // Short delay
      digitalWrite (13,LOW);       // Deactivate camera pin
        
      }
    }
    Can you get hold of a stepper motor to test your sketch?

    Ken

  6. #110
    Join Date
    Nov 2008
    Location
    somewhere
    Posts
    152

    Default

    yeah, i should be able to borrow one from a friend tomorrow. On mine the comms work, and they show the movement happening. I've also used my oscilloscope and I'm seeing steps coming out of the motor port. I have to admit to being stumped
    Quote Originally Posted by neksmerj View Post
    Hi foob,

    Yes the baud rate is set correctly and all the switches are programmed to have pull-ups.

    I know my setup works as I use an early sketch of yours to test it.

    I've just added a couple of lines at the bottom of your sketch to pause the motor and pulse pin13, the camera pin. It's crude but it's simple and seems to work ok.

    I tried adding the "moveTo command etc", as you'll see in the sketch, commented out, but that didn't work.

    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, MICROSTEP);
      //motor1->onestep(FORWARD, MICROSTEP);
    }
    void backwardstep() { 
      motor1->onestep(BACKWARD, MICROSTEP);
      //motor1->onestep(BACKWARD, MICROSTEP);
    }
    
    
    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 and camera pin
    #define LEFT_LIM_PIN 5
    #define RIGHT_LIM_PIN 6
    #define CAMERA_PIN 13 
    
    
    
    
    // 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(500.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);
      pinMode(13,OUTPUT);  //camera shutter pin
    
    
    }
    
    
    
    
    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;
        motor1->release();
        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 = 500;  //  <-- 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);
        delay (1000);              //Stop the motor
        
        //stepper1.setMaxSpeed (3000);
        //stepper1.setAcceleration (200);
        
        //stepper1.moveTo (100);
        //while (stepper1.distanceToGo ()!=0)
        //stepper1.run ();
          
        if (sign==0) {            // If motor is stopped by stop button or limit switch
        digitalWrite (13,LOW);    // disable camera pin.
      }else digitalWrite (13,HIGH); // Activate camera pin 
      delay (250);                // Short delay
      digitalWrite (13,LOW);       // Deactivate camera pin
        
      }
    }
    Can you get hold of a stepper motor to test your sketch?

    Ken

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

    Default

    Okay, re-reading your post it looks like the comms is working but the buttons arn't with my most recent code? Could you try holding down one of the left or right buttons for a couple of seconds (5 max). stop if you see the text change in the comms message.

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

    Default Camera slider update

    Hi foob,

    I can hold either fwd or rev button until the cows come home, (more than 20 seconds) and no motor movement.

    The serial monitor just scrolls down and down. Pressing the stop button has no effect.

    I understand dir=1, dir=0 and dir= -1, what does dir=2 do?

    Is this explained in the AccelStepper library?

    Ken

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

    Default

    Quote Originally Posted by neksmerj View Post
    Hi foob,

    I can hold either fwd or rev button until the cows come home, (more than 20 seconds) and no motor movement.

    The serial monitor just scrolls down and down. Pressing the stop button has no effect.

    I understand dir=1, dir=0 and dir= -1, what does dir=2 do?

    Is this explained in the AccelStepper library?

    Ken
    does the text change at all with the button presses?

    The dir variable is one I'm using, not to do with the accelstepper library. I'll use it later when calculating the variables. I've forgotten why i picked 2 rather than 0, but it gets set if the stop button or limit buttons are hit, but not if the you've got a valid movement from one of the direction buttons. valid being that the limit button for that direction hasn't been hit.

    I'm at a loss at this point. It works on my end, with an arduino uno and an adafruit motorshield v2. Anybody else got any suggestions?

    *oop* just thought of something - I'm using external resistors for pullups instead of relying on the arduino's. so I havn't actually initialised the pullups in the setup function. Ken could you please either try adding 1k or 10k (either will work) pullups to your switches or try again with the code i'll post in a sec.

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

    Default

    Okay, this is the same code with the internal pullups switched on:

    Code:
    /* 
    Round 2 of testing for Ken's Camera Slide project
    */
    #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
    #define STEP_TYPE SINGLE          // you can change this to DOUBLE, SINGLE, INTERLEAVE or MICROSTEP to control the step type
    
    // 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 MAX SPEED and accelleration
    #define MAX_SPEED 500
    #define MAX_ACCEL 250
    
    // define our distance to travel
    #define DISTANCE 500
    
    // define parameters for camera
    #define CAMERA_PIN 13
    #define CAMERA_PULSE 250                  // pulse time in uSeconds
    
    // Create the motor shield object with the default I2C address
    Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
    
    // Connect a stepper motor with 200 steps per revolution (1.8 degree)
    // to motor port #2 (M3 and M4)
    Adafruit_StepperMotor *motor1 = AFMS.getStepper(200, 2);
    AccelStepper stepper1(forwardstep, backwardstep); // use functions to step
    
    void setup() {
      Serial.begin(9600);           // set up Serial library at 9600 bps
      Serial.println("Stepper test for Ken");
    
      AFMS.begin();  // create with the default frequency 1.6KHz
      //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
      
      stepper1.setMaxSpeed(MAX_SPEED);
      stepper1.setAcceleration(MAX_ACCEL);
      
      // 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);
      pinMode(CAMERA_PIN,OUTPUT);  //camera shutter pin
    
    }
    
    void loop() {
      static char dir=0;
      long newpos;
    
    
      // okay if we have no distance left to go, we should be idling.
      if (stepper1.distanceToGo() == 0)
      {
        dir=0;
        motor1->release();
        Serial.print("waiting on input - ");
        Serial.print("Current position: ");
        Serial.println(stepper1.currentPosition());
        doCamera();
      }
      dir=checkInputs(dir);
      if (dir==1)
      {
        newpos=stepper1.currentPosition() + 500;
        stepper1.moveTo(newpos);
        Serial.print("Updating position to: ");
        Serial.println(newpos);
      }
      if (dir==-1)
      {
        newpos=stepper1.currentPosition()-500;
        stepper1.moveTo(newpos);
        Serial.print("Updating position to: ");
        Serial.println(newpos);
      }
      if (dir==2)  // stop button hit
      {
        stepper1.moveTo(stepper1.currentPosition());
        motor1->release();
        dir=0;
      }
      stepper1.run();
    }
    
    // this function will be called by the AccelStepper every step the motor turns forward
    void forwardstep()
    {
        motor1->onestep(FORWARD, STEP_TYPE);
    }
    
    // this function will be called by the AccelStepper every step the motor turns backward
    void backwardstep()
    {
        motor1->onestep(BACKWARD, STEP_TYPE);
    }
    
    
    // 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 dir)
    {   
        if (digitalRead(LEFT_PIN)==0 && digitalRead(LEFT_LIM_PIN)==1)
        {
            dir=1;
        }
        else if (digitalRead(RIGHT_PIN)==0 && digitalRead(RIGHT_LIM_PIN)==1)
        {
            dir=-1;
        }
        else if (digitalRead(STOP_PIN)==0 || digitalRead(RIGHT_LIM_PIN)==0 || digitalRead(STOP_PIN)==0)
        {
            dir=2;
        }
        else dir=0;
        return dir;
    }
    
    char doCamera()
    {
      // concept here is that when we stop (direction==0), we want to delay for a while, then take a shot, delay some more, then continue moving in the same direction we were before
          delay(500);                            // delay half a second after stopping
          digitalWrite(CAMERA_PIN,HIGH);         // drive the camera pin high
          delay(CAMERA_PULSE);                   // leave it high for the pulse time
          digitalWrite(CAMERA_PIN,LOW);          // drive the camera pin low
          delay(500-CAMERA_PULSE);               // snooze for the rest of the second
    }

  11. #115
    Join Date
    Nov 2006
    Location
    Heidelberg, Victoria
    Age
    79
    Posts
    2,251

    Default Camera slider update

    Hi foob,

    Well now we have some action, although a bit erratic.

    Pushing the fwd button, the motor ramps up to speed, then ramps down and stops. It won't do anything until the fwd button is pressed again. I'm sure this will be fixed in future sketches.

    Pushing the rev button, the motor starts to ramp up, stops and vibrates back and forth quickly, (as if the phases are being swapped) then ramps down and stops.

    The stop button or either limit switch halts the motor, mean while, pin 13 (I assume it's pin 13) continuously blinks even when the motor is stopped.

    We are getting closer and I can't believe I didn't spot the lack of pull ups.

    I like the way you have re-organised the sketch, it's a pity you don't have a stepper on hand.

    Ken

    edit, I just went back to the previous sketch and it has pull-ups mentioned, so what did you change with the latest sketch?

  12. #116
    Join Date
    Nov 2008
    Location
    somewhere
    Posts
    152

    Default

    still puzzled by whats happening in the reverse direction there. *sigh* Anybody got any suggestions? Ken can you try changing to microstepping and see if its easier to identify whats going wrong? the line your looking for is near the #define STEP_TYPE SINGLE should get changed to #define STEP_TYPE MICROSTEP

    The intention was this version is for it to only move one specified distance per push of the button. You should get no activity on pin 13 while the motor is moving, but you'll get one 250uS pulse per second while its stationary. I'm not sure what your camera needs to trigger it - something to figure out later. We need to get it working properly in both directions before we move on though. Might have to try something simpler.

    The earlier version of this code that I posted didn't have the pullups in it, unless you added them yourself.

    I'm heading off for the night.

  13. #117
    Join Date
    Jun 2010
    Location
    Canberra
    Posts
    769

    Default

    If Ken wants to post me some hardware, I'm happy to help test & debug - I've got steppers lying about and can scope signals if need be. In the absence of hardware to test and diagnose on, reading code looking for bugs is pretty tedious. I'm happy to post the gear back once the code is sorted.

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

    Default Camera slider update

    Hi Rusty,

    PM sent.

    Ken

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

    Default

    YAY, it works!

    I had a 4am brainwave and I think I have some of the issues sorted. I pulled a stepper motor out of an old printer i've been meaning to strip and hooked it up. I had quite a few issues initially with getting the wires hooked up the right way around ().

    I got two things out of the testing with the motor on:
    1. a MAX_SPEED value that is too high can cause the motor to act "jittery" and miss steps and act erratically, particularly in reverse.
    2. Not having the wires the correct way around can cause the motor to act jittery, and not work in the reverse direction (or go backwards for a bit, then forwards, then backwards etc)
    3. running a stepper motor in jittery mode with a blue tape flag on it is, according to my cat, the BEST CAT TOY EVAR! and is even more fun when bits go scattering everywhich way when you pounce. just ignore the hoomans cries when you steal his toy (what a wouss).

    The code below has been modified to reduce the MAX_SPEED value. Ken could you check you have your stepper hooked up correctly? If you stick a multimeter on a low resistance reading (200Ohms works on mine) across each pair of wires you should see a small resistance on the ones which are part of a pair, and open circuit to the other wires. Each pair of wires needs to be together going into the v2 shield. I don't know if the orientation (which way round the wires in the pair go), but if you get erratic behaviour try swapping them. You may want to wire in some short wires on alligator clips to the terminal block to make this easier, or just tin the ends of your stepper motor wires.

    Ken I know this still doesn't do exactly what you want, but once we have you working properly with this basic code I'll modify it to work like you said you wanted it.

  16. #120
    Join Date
    Nov 2008
    Location
    somewhere
    Posts
    152

    Default

    whups, forgot code:
    Code:
    /* 
    Round 2 of testing for Ken's Camera Slide project
    */
    #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
    #define STEP_TYPE MICROSTEP         // you can change this to DOUBLE, SINGLE, INTERLEAVE or MICROSTEP to control the step type
    
    // 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 MAX SPEED and accelleration
    #define MAX_SPEED 250
    #define MAX_ACCEL 150
    
    // define our distance to travel
    #define DISTANCE 500
    
    // define parameters for camera
    #define CAMERA_PIN 13
    #define CAMERA_PULSE 250                  // pulse time in uSeconds
    
    // Create the motor shield object with the default I2C address
    Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
    
    // Connect a stepper motor with 200 steps per revolution (1.8 degree)
    // to motor port #2 (M3 and M4)
    Adafruit_StepperMotor *motor1 = AFMS.getStepper(200, 2);
    AccelStepper stepper1(forwardstep, backwardstep); // use functions to step
    
    void setup() {
      Serial.begin(9600);           // set up Serial library at 9600 bps
      Serial.println("Stepper test for Ken");
    
      AFMS.begin();  // create with the default frequency 1.6KHz
      //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
      
      stepper1.setMaxSpeed(MAX_SPEED);
      stepper1.setAcceleration(MAX_ACCEL);
      
      // 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);
      pinMode(CAMERA_PIN,OUTPUT);  //camera shutter pin
    
    }
    
    void loop() {
      static char dir=0;
      long newpos;
    
    
      // okay if we have no distance left to go, we should be idling.
      if (stepper1.distanceToGo() == 0)
      {
        dir=0;
        motor1->release();
        Serial.print("waiting on input - ");
        Serial.print("Current position: ");
        Serial.println(stepper1.currentPosition());
        doCamera();
      }
      dir=checkInputs(dir);
      if (dir==1)
      {
        newpos=stepper1.currentPosition() + 500;
        stepper1.moveTo(newpos);
        Serial.print("Updating position to: ");
        Serial.println(newpos);
      }
      if (dir==-1)
      {
        newpos=stepper1.currentPosition()-500;
        stepper1.moveTo(newpos);
        Serial.print("Updating position to: ");
        Serial.println(newpos);
      }
      if (dir==2)  // stop button hit
      {
        stepper1.moveTo(stepper1.currentPosition());
        motor1->release();
        dir=0;
      }
      stepper1.run();
    }
    
    // this function will be called by the AccelStepper every step the motor turns forward
    void forwardstep()
    {
        motor1->onestep(FORWARD, STEP_TYPE);
    }
    
    // this function will be called by the AccelStepper every step the motor turns backward
    void backwardstep()
    {
        motor1->onestep(BACKWARD, STEP_TYPE);
    }
    
    
    // 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 dir)
    {   
        if (digitalRead(LEFT_PIN)==0 && digitalRead(LEFT_LIM_PIN)==1)
        {
            dir=1;
        }
        else if (digitalRead(RIGHT_PIN)==0 && digitalRead(RIGHT_LIM_PIN)==1)
        {
            dir=-1;
        }
        else if (digitalRead(STOP_PIN)==0 || digitalRead(RIGHT_LIM_PIN)==0 || digitalRead(STOP_PIN)==0)
        {
            dir=2;
        }
        else dir=0;
        return dir;
    }
    
    char doCamera()
    {
    
      // concept here is that when we stop (direction==0), we want to delay for a while, then take a shot, delay some more, then continue moving in the same direction we were before
          delay(500);                            // delay half a second after stopping
          digitalWrite(CAMERA_PIN,HIGH);         // drive the camera pin high
          delay(CAMERA_PULSE);                   // leave it high for the pulse time
          digitalWrite(CAMERA_PIN,LOW);          // drive the camera pin low
          delay(500-CAMERA_PULSE);               // snooze for the rest of the second
    }

Page 8 of 11 FirstFirst ... 34567891011 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
  •