Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: Teensy 3.2 Stepper motor with push buttons

  1. #1
    Junior Member
    Join Date
    Oct 2018
    Posts
    2

    Teensy 3.2 Stepper motor with push buttons

    Being very new to programing I am trying to drive a 12v stepper motor with a Teensy 3.2 and an H-Bridge.

    I want the program to run a stepper clockwise when pushing button A, until you release the switch or it hits the limit switch. A button B, and limit switch, run it counterclockwise.

    I found a example sketch that I'm wanting to learn from but get a compile error - "default arguments are only permitted for function parameters" when it reaches "int a = analogRead(A0); immediately after "void loop()".
    I think it has to do with declaring the input pins A0 and A1, or lack thereof. I think I have a grasp on hookup of the teensy, bridge and switches but can't go forward until I can get the program to compile.

    Any help would be appreciated.

    Here is the code -

    #include <Stepper.h>

    const int stepsPerRevolution = 200;

    Stepper myStepper(stepsPerRevolution, 4, 6, 5, 7);

    void setup() {
    myStepper.setSpeed(200);
    int a = 0;
    int b = 0;
    }

    void loop() (
    int a = analogRead(A0);
    int b = analogRead(A1);
    if (a==0){
    myStepper.step(stepsPerRevolution);
    }
    else if (b==0){
    myStepper.step(-stepsPerRevolution)
    }

  2. #2
    Senior Member
    Join Date
    Oct 2015
    Location
    Roma (IT, EU)
    Posts
    102
    Hello Joe,

    I think the problem is, "A0" is not a valid pin number (nor is "A1").
    analogRead() wants an int (so, a pin number) as an argument.

    It should work if you declare A0 and A1 appropriately, for example, just after #includes:

    Code:
    #include <Stepper.h>
    
    #define A0 14
    #define A1 15
    
    (...)

  3. #3
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    280
    Quote Originally Posted by XFer View Post
    I think the problem is, "A0" is not a valid pin number (nor is "A1").
    analogRead() wants an int (so, a pin number) as an argument.
    No, A0, A1... are defined in the core library and can be used without problem. The code you posted simply has syntax errors.

    1. The braket after loop() ( should be a brace {
    2. You forgot the last closing brace }
    3. I assume the smiley at the end of the last statement originally was a semicolon? Better to use code tags to post code to avoid such things.


    Here a version which compiles

    Code:
    #include <Stepper.h>
    
    const int stepsPerRevolution = 200;
    
    Stepper myStepper(stepsPerRevolution, 4, 6, 5, 7);
    
    void setup() {
      myStepper.setSpeed(200);
      int a = 0;
      int b = 0;
    }
    
    void loop() {
      int a = analogRead(A0);
      int b = analogRead(A1);
      if (a == 0) {
        myStepper.step(stepsPerRevolution);
      }
      else if (b == 0) {
        myStepper.step(-stepsPerRevolution);
      }
    }

  4. #4
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    6,986
    When in the Ardiuno IDE as the editor key combo of :: Ctrl+T

    Will uniformly format the code to some accepted standard - when there are no typos as noted by luni. It will also show matching braces [ and parens etc. ] - when present clicking at/after one will indicate the other if found.

    When there are such typos present - missing or ill formed bracket pairs - the formatting will typically FAIL to follow the standard and make it somewhat apparent where the error is.

    When that is done with the code in OP it looks like this - where the end of loop() fails to indent without an open brace as luni shows above:
    Code:
    #include <Stepper.h>
    
    const int stepsPerRevolution = 200;
    
    Stepper myStepper(stepsPerRevolution, 4, 6, 5, 7);
    
    void setup() {
      myStepper.setSpeed(200);
      int a = 0;
      int b = 0;
    }
    
    void loop() (
      int a = analogRead(A0);
      int b = analogRead(A1);
    if (a == 0) {
    myStepper.step(stepsPerRevolution);
    }
    else if (b == 0) {
    myStepper.step(-stepsPerRevolution)
    }

  5. #5
    Junior Member
    Join Date
    Oct 2018
    Posts
    2
    It was the typo - thanks for the assist.

Posting Permissions

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