Please Help, I'm a rookie :-/

I am a complete novice at writing my own sketches, although I have modified some in the past. I am simply wanting to start a stepper motor with a push button, have it run at a designated speed until it reaches an optical end stop, and then run at a slower designated speed until it reaches a second optical end stop. at which point the program stops/resets, until I pushed the button again. this is for a balance beam scale.

Thanks in advance for anyone who can help me, or point me in the right direction for something to reference.

There are three parts to the project - getting the right hardware, wiring it up correctly, and writing the code.

What part(s) are you stuck on?

Writing the code involves reading a switch, and determining that it has BECOME pressed. If it has, start the sequence of events.

Moving the stepper motor is easy. Step. Step. Step. ...

Detecting that an optical sensor is, or is not, blocked is easy.

This would be some good code. I didn’t debug it so I don’t know how good it is but as long as you have it wired right this should work. I am currently working with something similar.

int button = 0; //Sets pin 0 to be called button.
int motor = 10; //Sets pin 10 to be called motor.

void setup() {
pinMode(button, INPUT);
pinMode(motor, OUTPUT);
}

void loop() {
if (digitalRead(button) == HIGH) {
digitalWrite(motor,HIGH);
}
else if (digitalRead(button) == LOW) {
digitalWrite(motor, LOW);
}

}

int button = 0; //Sets pin 0 to be called button.

Using the serial pins for switches makes debugging all that more difficult.

And, if you've read the pin once, and it isn't HIGH, there's no point in reading it again to see if it is LOW

Thanks guys. I will try some of these tips as soon as I get time. You’re help is very much appreciated!

where in the sketch would I specify the speeds that I would like to run? until first optical switch is made, and then until the second is made?

thanks again for your help everyone.

mmk8283: where in the sketch would I specify the speeds that I would like to run? until first optical switch is made, and then until the second is made?

Can you explain what you mean, please? First? Second?

So my design is to run a mini auger attached to a stepper motor...

  1. I'd like the motor to start at a high speed (a variable I can set), when I press a button.
  2. I'd like the motor to run at that given speed until a switch (optical) is made, and then continue at a slower speed (another variable I can set)
  3. The motor continues to run at the slower speed until a second switch (optical) is made, which then stops the motor.
  4. Pressing the button again, after the switches are reset, starts this process over again.

as the second switch is made, the first will continue to be blocked.

so…this is my first attempt, what do you guys think?? Am I way off?

#include <Stepper.h>

const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
int sensorPin1 = A0;
int sensorPin2 = A1;

if (sensorPin1 == HIGH && sensorPin2 == HIGH) {
myStepper.setSpeed(100);
}
else if (sensorPin1 == LOW && sensorPin2 == HIGH) {
myStepper.setSpeed(10);
}
else (sensorPin1 == LOW && sensorPin2 == LOW); {
myStepper.setSpeed(0);
}

}

void loop() {
// put your main code here, to run repeatedly:

}

  int sensorPin1 = A0;
  int sensorPin2 = A1;
  
  if (sensorPin1 == HIGH

A0 typically has the value 14.
What do you think the chances are that it will equal HIGH, which always has the value 1?

you lost me there... should i use A1 and A2 for the optical switches instead?

am I on the right track with the rest of the sketch? I'm self teaching on all of this... its my first attempt

mmk8283: you lost me there... should i use A1 and A2 for the optical switches instead?

You could, but you need to be sure you read what is on the pins, not use the pin number itself.