Results 1 to 7 of 7
  1. #1
    10+ Posting Member
    Join Date
    Apr 2014
    Location
    United States
    Posts
    15
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    help with stepper code

    Hi, I have posted about this project before but with life and all I've had to leave it for awhile, but now I'm back to it and I'm having a problem.

    The basic concept is that I want to use a stepper motor to tighten a bit of surgical tubing on my yoke the faster I fly, thereby increasing the tension on the yoke.

    I've tried a servo motor but found that the 180 degree travel wasn't enough, so I've gotten a couple of 12 volt 200 steps per rotation steppers that I believe will do what I want.

    The problems I'm having is with the code below

    Code:
    #include 
    #include 
    #include "utility/Adafruit_PWMServoDriver.h"
    
    Adafruit_MotorShield AFMS = Adafruit_MotorShield();
    
    Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
    
    int CodeIn;
    int ASI;
    int val;
    
    String KoldpinStateSTR, KpinStateSTR, Kstringnewstate,Kstringoldstate;
    
    void setup() {
      Serial.begin(115200);
      Serial.println("ASI Stepper test!");
      AFMS.begin();
      myMotor->setSpeed(250);  // 10 rpm
    }
    
    void loop() {
     
      if (Serial.available()) {
        CodeIn = getChar();
        if (CodeIn == '<') {LESSTHAN();}// The first identifier is "<"
         }
    
    }
    
    char getChar()// Get a character from the serial buffer
    {
      while(Serial.available() == 0);// wait for data
      return((char)Serial.read());// Thanks Doug
    }
    
    void LESSTHAN(){ // The first identifier was "<"
    CodeIn = getChar(); // Get another character
      switch(CodeIn) {// Now lets find what to do with it
      case 'P'://Found the second identifier Turn coordinator
      
      {
        ASI == '20';
        Serial.println("Interleave coil steps");
      if (ASI > 20)
      myMotor->step(10, FORWARD, INTERLEAVE);
      if (ASI < 20) 
      myMotor->step(10, BACKWARD, INTERLEAVE);
        }
        break;}
    }
    I can't seem to get the stepper to turn but one direction. It doesn't matter if the airspeed is going up or down the stepper won't turn but counterclockwise.

    Could someone help me with this?

    Thanks
    Mark

  2. #2
    75+ Posting Member vikingsraven's Avatar
    Join Date
    Dec 2012
    Location
    UK
    Posts
    89
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: help with stepper code

    hi mark,
    first swap your wiring round to see if it will run in reverse.
    Not used this card, but using pololu boards.
    also slow the pulse speed down, you've got it at setSpeed(250) which is 250rpm as far as i can tell from the adafriuit test code. it maybe be stalling.
    also try changing the second if to an else.(if (ASI<20))
    hope this helps. ill look into the adafruit card more.
    Mark

  3. #3
    10+ Posting Member
    Join Date
    Apr 2014
    Location
    United States
    Posts
    15
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: help with stepper code

    Mark, Thanks for the reply. As for swapping the wiring, I have a test code that will run the motor in forward and reverse and at different speeds, so I know that's not it. I did slow it down some and changed the second if to a else but it is still doing the same thing.

    I did put in two separate println for each direction and in the Link2fs monitor it is showing the forward for just a split second and then the reverse, or Backward in this case, but the motor will only turn Backward, never Forward. I think it has something to do with the timing but I've yet to find a solution.
    Mark

  4. #4
    10+ Posting Member
    Join Date
    Apr 2014
    Location
    United States
    Posts
    15
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: help with stepper code

    Mark, Thanks for the reply. As for swapping the wiring, I have a test code that will run the motor in forward and reverse and at different speeds, so I know that's not it. I did slow it down some and changed the second if to a else but it is still doing the same thing.

    I did put in two separate println for each direction and in the Link2fs monitor it is showing the forward for just a split second and then the reverse, or Backward in this case, but the motor will only turn Backward, never Forward. I think it has something to do with the timing but I've yet to find a solution.
    Mark

  5. #5
    75+ Posting Member vikingsraven's Avatar
    Join Date
    Dec 2012
    Location
    UK
    Posts
    89
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: help with stepper code

    ping bright flash of light above head!
    it because your saying ASI == '20' at the begining!
    putting it in quotes makes it a string i believe so your saying 'hex 32,hex30'.
    its only ever going to be above 20 so go forwards!

  6. #6
    10+ Posting Member
    Join Date
    Apr 2014
    Location
    United States
    Posts
    15
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: help with stepper code

    Ok, Mark,
    This is what I'm trying now.

    Code:
    #include 
    
    #include 
    #include 
    #include "utility/Adafruit_PWMServoDriver.h"
    
    Adafruit_MotorShield AFMS = Adafruit_MotorShield();
    
    Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
    
    long CodeIn;
    long asi;
    long asil;
    
    String KoldpinStateSTR, KpinStateSTR, Kstringnewstate,Kstringoldstate;
    
    void setup() {
      Serial.begin(115200);
      Serial.println("ASI Stepper test!");
      AFMS.begin();
      myMotor->setSpeed(1000);  // 10 rpm
    }
    
    void loop() {
     
      if (Serial.available()) {
        CodeIn = getChar();
        if (CodeIn == '<') {LESSTHAN();}// The first identifier is "<"
         }
    
    }
    
    char getChar()// Get a character from the serial buffer
    {
      while(Serial.available() == 0);// wait for data
      return((char)Serial.read());// Thanks Doug
    }
    
    void LESSTHAN(){ // The first identifier was "<"
    CodeIn = getChar(); // Get another character
      switch(CodeIn) {// Now lets find what to do with it
      case 'P'://Found the second identifier Turn coordinator
    
      if (asi < asil); //if i put a ; after ) the motor just jumps back and forth monitor shows forward backward real fast without the ; the monitor just shows BACKWARD
      {  
      myMotor->step(10, FORWARD, SINGLE);
      Serial.println("FORWARD");
      }
    
      if (asi > asil);  // if I remove this ; then it only shows forward with both ; it just jumps back and forth
      { 
      myMotor->step(10, BACKWARD, SINGLE);
      Serial.println("BACKWARD");
      }
      
      
        
      }
      (asil=asi);
    }
    As you can see from my comments I can get it to rotate one way or the other but not both. If it is setup correctly the motor just jumps back and forth real fast and it doesn't matter if the airspeed is increasing or decreasing.

    I think I need some way to separate the two if statements. I tried a separate case statement reading off of the ground speed instead of airspeed but that didn't work when they were the same value.

    Any ideas?

    Mark

  7. #7
    75+ Posting Member vikingsraven's Avatar
    Join Date
    Dec 2012
    Location
    UK
    Posts
    89
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: help with stepper code

    are you converting the data recieved to a number. When it come in its a char so 2 will be 50 (dec) 32 Hex.
    youll have to change it to a number.
    try something like
    after< P detected

    ASI = ""; // clears ASI value so you can extract number
    ASI +=getChar(); //Gets char and appends to ASI eg 0
    ASI +=getChar();// Gets char appends to ASI eg 3 so ASI = 30 this is not a number its a ascii string
    ASI +=getChar();// ditto

    int ASII = ASI.toInt(); /converts the string to an integer number ASII is the value you then check
    if (ASII<20 .......