Switch and speed issues

Hello.

I am currently working on a project where I need a sort of sensor system that, when tripped, reverses the motion of the motor. To this end, I am using a stepper motor and two switches as a prototype.

Currently, when a switch is tripped, it does cause the motor to reverse, but only if the switch is left on. What I need is for the motor to run after the switch is tripped but then returns to the off position.

Also, side question: I have been experiencing a lot of issues with modulating the speed. I am using a v2 Adafruit Motor Shield with the Arduino Uno. I have used the .setSpeed command but it does not yield the correct rpm (i.e. I would set it to 10, and instead the motor would move much faster). Why is this? I have also used the AccelStepper library to similarly unfruitful results.

Thank you to all that respond.

[code]
#include <Wire.h>
#include <Stepper.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"

const int buttonPin1 = 2;
const int buttonPin2 = 3;

const int sensorMin = 0;
const int sensorMax = 1;

int counter;

Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myMotor1 = AFMS.getStepper(200, 1);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  pinMode (buttonPin1, INPUT);
  pinMode (buttonPin2, INPUT);

  AFMS.begin();

  myMotor1->setSpeed(10);
}

void loop() {
  // put your main code here, to run repeatedly:
  int reading = digitalRead(buttonPin1);
  if (reading == HIGH) {
    Serial.println("right");
    myMotor1->step(25, FORWARD, SINGLE);
    ++counter;
  }
  int reading1 = digitalRead(buttonPin2);
  if (reading1 == HIGH) {
    Serial.println("left");
    myMotor1->step(25, BACKWARD, SINGLE);
    --counter;
  }
}

[/code]

Re your switch problem. Take a look at the state change detection example in the IDE examples (File, Examples, Digital). Using state change detection you can act on the transition not the level.

Use a variable to keep track of which direction to travel.
Use the switch to change the direction variable.
Use the direction variable to set the direction of the motor.

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(LeftLimitPin)) {
    Serial.println("right");
    Direction = FORWARD;  // Go right
  }
  if (digitalRead(RightLimitPin)) {
    Serial.println("left");
    Direction = BACKWARD;  // Go left
  }

  myMotor1->step(25, Direction, SINGLE);
  counter += (Direction == FORWARD) ? +1 : -1;
}

Update: Thank you all, I have taken your feedback into consideration. All of the issues involving the code proper have been fixed, but I'm still having issues modulating the speed. Any ideas on what might be going on with that would be much appreciated.

Post your code as it is now.

This is the finished code

[code]
#include <Wire.h>
#include <Stepper.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"

const int buttonPin1 = 2;
const int buttonPin2 = 3;

const int sensorMin = 0;
const int sensorMax = 1;

int buttonState;
int counter;

Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myMotor1 = AFMS.getStepper(200, 1);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  pinMode (buttonPin1, INPUT);
  pinMode (buttonPin2, INPUT);

  AFMS.begin();

  myMotor1->setSpeed(20);
}

void loop() {
  // put your main code here, to run repeatedly:
  buttonState = digitalRead(buttonPin1);
  //  int reading = digitalRead(buttonPin1);
  if (buttonState == HIGH) {
    Serial.println("right");
    ++counter;
    delay(500);
  }
  buttonState = digitalRead(buttonPin2);
  if (buttonState == HIGH) {
    --counter;
    delay(500);
  }
  if (counter % 2 == 0) {
    Serial.println("right");
    myMotor1->step(25, FORWARD, SINGLE);
  }
  else {
    Serial.println("left");
    myMotor1->step(25, BACKWARD, DOUBLE);
    }
  }

[/code]

What is supposed to "modulate the speed" in that sketch?

anniej:
but it does not yield the correct rpm (i.e. I would set it to 10, and instead the motor would move much faster

If you want that question answered, you need to post the particular code you used in that case (well, cases,
as you mention AccelStepper and Adafruit libraries). And tell us exactly which motor you use.

aarg:
What is supposed to “modulate the speed” in that sketch?

The “myMotor1->setSpeed(20)” command should change the speed by changing the number within the parentheses, no?

MarkT:
you need to post the particular code you used in that case

I have used both the code displayed here (i.e. the myMotor1->setSpeed(20) line with changes to the number within the parentheses) and a slightly modified version of an example provided by the AccelStepper library.

#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"

Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myMotor1 = AFMS.getStepper(200, 1);

void forwardstep1() {
  myMotor1->onestep(FORWARD, SINGLE);
}
void backwardstep1() {
  myMotor1->onestep(BACKWARD, SINGLE);
}
AccelStepper Astepper1(forwardstep1, backwardstep1);

void setup() {
  // put your setup code here, to run once:
  AFMS.begin();
  Astepper1.setSpeed(200);
}

void loop() {
  // put your main code here, to run repeatedly:
  Astepper1.runSpeed();
}

Note, this is only the code for changing speed; it doesn’t contain any of the switch commands.

MarkT:
And tell us exactly which motor you use.

I am not certain what stepper motor I am using (what specific brand, name, whatever.). All I know is that it is a bipolar, 200 step motor.

Hmm, the AccelStepper library handles all its parameters in terms of steps, so that the setSpeed(200)
there means 200 steps/second.

The AdaFruit call is in RPM, not steps per second - could this difference be confusing you?

The Adafruit code probably doesn't do ramping (I've not checked the source), which will affect
things too - AccelStepper has an acceleration in steps/s/s that you have to configure.

Modulation means dynamically changing the speed, have you tried to do that and its failed?

MarkT:
Hmm, the AccelStepper library handles all its parameters in terms of steps, so that the setSpeed(200)
there means 200 steps/second.

The AdaFruit call is in RPM, not steps per second - could this difference be confusing you?

No, I was aware that AccelStepper and Adafruit use different means of counting speed. The issue is that I would set the speed at, for example, 10 (on either program) and then set it to 20 or 100 and the speed of the motor's rotation would not change significantly or at all proportional to what their change numerically would have indicated.

MarkT:
The Adafruit code probably doesn't do ramping

I'm not sure what you mean by "ramping." Could you please clarify?

MarkT:
Modulation means dynamically changing the speed, have you tried to do that and its failed?

I may have used the word "modulation" incorrectly in this context. What I have done is change the number within the parentheses of the setSpeed commands. This has failed, in that the motor does not rotate in proportion to the speed entered, as I have explained above.

"Ramping" = acceleration. Like your car can't instantly go from stationary to 60kph; you have to accelerate. A simple constant acceleration produces a straight-line speed change, which looks a little like a ramp. (Boat ramp, disabled access ramp...)

If the motor speed isn't changing when you change those numbers then there's something else wrong.

anniej:
The "myMotor1->setSpeed(20)" command should change the speed by changing the number within the parentheses, no?

Editing, compiling and uploading a sketch every time you change a parameter, isn't modulation. It's difficult to help if you don't use standard terminology.

aarg:
Editing, compiling and uploading a sketch every time you change a parameter, isn't modulation. It's difficult to help if you don't use standard terminology.

My apologies? I did not do so to purposely mislead you. I hope everything is now satisfactorily explained.

Given what I have further elucidated, do you have any ideas as to why the motor is not changing speed (at the very least, proportionally to the setSpeed command)?

The "AFMotor_ConstantSpeed" example has:

    stepper.setMaxSpeed(50);
    stepper.setSpeed(50);

where you have only:

      Astepper1.setSpeed(200);

Perhaps you should add the call to Astepper1.setMaxSpeed(200) before that line.

It also doesn't have a call to AFMS.begin() in setup(). Maybe starting the AFMS library causes it to conflict with the AccelStepper library.