Teensy 3.2 Stepper motor with push buttons

Status
Not open for further replies.

JoeG

New member
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);)
}
 
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

(...)
 
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);
  }
}
 
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)
}
 
Status
Not open for further replies.
Back
Top