Can I please get help with adding two buttons??

914 views
Skip to first unread message

Grant Hobgood

unread,
Jan 11, 2014, 2:59:02 PM1/11/14
to accels...@googlegroups.com
I am trying to add two SPST momentary buttons to my stepper motor and arduino setup using the arduino motor shield. Can anyone please help me do this please? I am brand new to microcontrollers over all so I am a bit taken back and overwhelmed. I am hoping to have it ramp up pretty fast and stay at a constant speed then decelerate to zero, or simply cut off when I release the button. One button would do forward, and one reverse. Would you please help me with the coding at least? What I have already is below!

#include <AccelStepper.h>

AccelStepper stepper(2,12,13);

const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 8;
const int brakeB = 9;


void setup()
{  
  
  pinMode(pwmA, OUTPUT);
  pinMode(pwmB, OUTPUT);
  pinMode(brakeA, OUTPUT);
  pinMode(brakeB, OUTPUT);
  
  digitalWrite(pwmA, HIGH);
  digitalWrite(pwmB, HIGH);
  digitalWrite(brakeA, LOW);
  digitalWrite(brakeB, LOW);
  
  stepper.setMaxSpeed(1500);
  stepper.setSpeed(800);
  
  stepper.setAcceleration(200);
  stepper.moveTo(1500);
  
  
}

void loop(){  

  if (stepper.distanceToGo() == 0) {
    stepper.run();                   // let the AccelStepper to disable motor current after stop
    delay(2000);                     // wait 2 sec in final position 
    stepper.moveTo(-stepper.currentPosition());
  }
  stepper.run();

}

Sandy Noble

unread,
Jan 11, 2014, 3:39:39 PM1/11/14
to accels...@googlegroups.com
HI Grant, how are you registering the button presses?


Grant Hobgood

unread,
Jan 11, 2014, 3:45:27 PM1/11/14
to accels...@googlegroups.com
I've actually just switched over to the AccelStepper library from the stock Stepper library and haven't had much luck doing anything in Accel other than movement being way smoother. I have never programmed anything before, just kind of jumped in. I've not been able to find what I'm looking to do, so I would say I haven't registered since switching libraries. Sorry I can't help you help me haha.

Sandy Noble

unread,
Jan 11, 2014, 5:51:22 PM1/11/14
to accels...@googlegroups.com
Fair enough - the program you've got does something at least, but yes you've got to figure out how you're going to deal with the button presses. Now there's tonnes of ways of doing that, from simple and limited (http://arduino.cc/en/tutorial/button) to more complete (http://www.dave-auld.net/?option=com_content&view=article&id=107:arduino-interrupts&catid=53:arduino-input-output-basics&Itemid=107), to super advanced and extensible.

Simple - In your loop, you do a digitalRead() on the pin that you have your switch wired to, and make a decision based on if it is high or low. This has the benefit of being well, simple, but the problem of filling your main loop with digitalRead, which is slow, and takes time even if nothing has changed (ie the button hasn't been touched). You could probably get away with this method if you don't mind your button-controlled movements being slow.

Complete - Attach an Interrupt to the pin that your button is wired to. This way, when you press the button, then a particular method/procedure gets triggered. No need to keep sampling the pin. Advantage is that your main loop is cleaner and faster. Disadvantage is a little more complexity, the necessity of knowing about interrupts, and also dealing with debouncing (the short period of oscillation that happens when you press a button mechanical, before it settles down in either on or off). Interrupts are not complicated, just a little "magical".

Really, IO is not really anything to do with accelstepper though, but if you have a google around for something like "button arduino circuit", and look at the arduino examples (examples->digital->button and examples->digital->debounce I think) you should get an idea of what it requires, and you'll be able to write another sketch that lets you get a handle on that. Once that's sorted out, then you can combine the two.

A very simple version with some significant problems could be something like

void loop() {
    // check button status
    boolean leftButtonWasPressed = digitalRead(leftButtonPin);
    boolean rightButtonWasPressed = digitalRead(rightButtonPin);

    // make a decision based on your input
    if (leftButtonWasPressed == true) {
        stepper.move(-50);
    }
    else if (rightButtonWasPressed == true) {
        stepper.move(50);
    }

    // step
    stepper.run();
}


sn


--
You received this message because you are subscribed to the Google Groups "accelstepper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to accelstepper...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Sandy Noble

Grant Hobgood

unread,
Jan 11, 2014, 8:45:50 PM1/11/14
to accels...@googlegroups.com
So I've been messing with this since you responded and have mad no progress. I don't know why I can't grasp this. I've been looking at this stuff for a month now, and my end goal isn't much closer haha. I appreciate all the help though, I'll keep plugging away and maybe I'll end up getting there.

Grant Hobgood

unread,
Jan 12, 2014, 7:56:31 AM1/12/14
to accels...@googlegroups.com
Alright, I've woken up with a fresh mind and thought I had figured this out. Would you mind reading over this and just pointing me in the right direction?  I have my buttonPin on pin 2 of my motor shield, and dirA as pin 12 per the shield as well. This doesn't work, though when I jump to pin 12 directly rather than pin 2 I get a single step and return from a push of the button. Where am i messing up? I can post  photo of my breadboard if need be.

 #include <AccelStepper.h>

AccelStepper stepper(2,12,13);

const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 8;
const int brakeB = 9;
const int dirA = 12;         // direction of motor when button pressed
const int buttonPin = 2;     // the number of the pushbutton pin


void setup()
{  
{
  // initialize the LED pin as an output:
  pinMode(dirA, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

pinMode(pwmA, OUTPUT);
pinMode(pwmB, OUTPUT);
pinMode(brakeA, OUTPUT);
pinMode(brakeB, OUTPUT);

digitalWrite(pwmA, HIGH);
digitalWrite(pwmB, HIGH);
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);

stepper.setMaxSpeed(1500);
stepper.setSpeed(500);

stepper.setAcceleration(200);
stepper.moveTo(1500);


}

void loop(){  


  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonPin == HIGH) {     
    // turn motor dirA:    
    digitalWrite(dirA, HIGH);  
  } 
  else {
    // turn motor off:
    digitalWrite(dirA, LOW);

  if (stepper.distanceToGo() == 0) {
    stepper.run();                   // let the AccelStepper to disable motor current after stop
    delay(2000);                     // wait 2 sec in final position 
   
  stepper.run();
  }
  }
  {


--
You received this message because you are subscribed to a topic in the Google Groups "accelstepper" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/accelstepper/yT5XWeaJS2A/unsubscribe.
To unsubscribe from this group and all its topics, send an email to accelstepper...@googlegroups.com.

Sandy Noble

unread,
Jan 12, 2014, 9:59:17 AM1/12/14
to accels...@googlegroups.com
Hi Grant, you're going to crash into all kinds of problems if you are using accelstepper _and_ also changing the pin states manually, you should either use accelstepper, OR step everything manually, don't mix the two. The purpose of accelstepper (and AFMotor) is to wrap the pin-level manipulations, so you don't have to keep track of the state of the motors - position, direction, last stepped etc.

Not saying you should _never_ step (ha) around the drivers and do it directly, but be prepared for very odd behaviour if you do. (Needless to say, accelstepper doesn't support being used in that way.)

I've just had a closer look at your sketch, and scratched my head - is it a Adafruit motorshield v2? Because you aren't using AFMotor anywhere. The usual way to use the motorshield is as in the AFMotor_ConstantSpeed example

#include <AccelStepper.h>
#include <AFMotor.h>

AF_Stepper motor1(200, 1);

// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
void forwardstep() {  
  motor1.onestep(FORWARD, SINGLE);
}
void backwardstep() {  
  motor1.onestep(BACKWARD, SINGLE);
}

AccelStepper stepper(forwardstep, backwardstep); // use functions to step

But if you've got it working directly, then fair enough!

Right though, your stepper definition

    AccelStepper stepper(2,12,13);

Defines a two-wire connection, with pin 12 being the STEP wire and pin 13 being the DIRECTION (http://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html has the documentation.) So if pin 12 really is wired to the STEP pin of your driver board, then connecting the button to it will trigger one step, that is expected. 

The real problem in your sketch is that you are not reading the button. You set the value of buttonPin (= 2), then in your loop(), you check the value of it (if (buttonPin == HIGH) { .. }), well it'll never be high, because you've already set it to 2. (weirdly, in this case HIGH == 1, and LOW == 0).

You need a line like 

int buttonValue = digitalRead(buttonPin);

to read the voltage on the button pin.

See if the following makes any sense:

boolean buttonWasPressed;
int buttonValue;

void setup() {
    stepper.setMaxSpeed(1500);
    stepper.setAcceleration(200);
    stepper.moveTo(1500);
    pinMode(buttonPin, INPUT);
    buttonWasPressed = false;
}

void loop() {
    // sample the pin
    buttonValue = digitalRead(buttonPin);

    // deal with initial press
    if (buttonValue == HIGH && buttonWasPressed == false) {
        stepper.moveTo(1500);
        buttonWasPressed = true;
    }
    // deal with the button being held down
    else if (buttonValue == HIGH && buttonWasPressed) {
        stepper.run();
    }
    // deal with the button being released
    else if (buttonValue == LOW && buttonWasPressed) {
        stepper.stop();
        while (stepper.distanceToGo() != 0) {
            stepper.run();
        }
        // reset the button so this doesn't trigger again next loop
        buttonWasPressed = false;
    }

}


sn


--
You received this message because you are subscribed to the Google Groups "accelstepper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to accelstepper...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.



--
Sandy Noble

Grant Hobgood

unread,
Jan 12, 2014, 10:10:47 AM1/12/14
to accels...@googlegroups.com

On Saturday, January 11, 2014 1:59:02 PM UTC-6, Grant Hobgood wrote:

Grant Hobgood

unread,
Jan 12, 2014, 10:25:30 AM1/12/14
to accels...@googlegroups.com
It is becoming more and more clear that I need to find someone local to me, or push this to the side until I know more. My ultimate goal is far beyond simple button presses, this was just a way to keep moving. What I am hoping to come away with is like this, though powered in both directions via belt/pulley  http://www.youtube.com/watch?v=4TvTfEk7eHg


--

Sandy Noble

unread,
Jan 12, 2014, 2:25:25 PM1/12/14
to accels...@googlegroups.com
Aha, right sorry I misread your email, saw arduino and read adafruit, oops! Ok, that does explain why it didn't make sense :)

I don't understand how the motorshield works, in the past Mike suggested that the definition should be 

AccelStepper stepper(AccelStepper::FULL4WIRE, 13, 12, 9, 8);

That dollyduino looks good doesn't it. I think the challenge there would be getting the physical circuit wired up rather than the software (well, because the software is already done).

sn


--
You received this message because you are subscribed to the Google Groups "accelstepper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to accelstepper...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.



--
Sandy Noble

Grant Hobgood

unread,
Jan 12, 2014, 4:49:47 PM1/12/14
to accels...@googlegroups.com
The sketch you drew out a bit earlier, it is saying I need to call out buttonPin. How should I do that? Do I set it == to an open pin and it just references back to that?

Grant Hobgood

unread,
Jan 12, 2014, 4:53:41 PM1/12/14
to accels...@googlegroups.com
This is what I have so far. I've added a pot to the mix which is not perfect yet, but it is controllable. Thinking I may need a resistor on it, but I haven't confirmed yet. This moves, the pot works, but when I press my button (cons int buttonPin = 2...I think this may be wrong?) it seems like it tries to either reverse the movement...or bump it up to max. Simply put...it jams it up because my max speed is a bit high I believe.

Grant Hobgood

unread,
Jan 12, 2014, 4:53:48 PM1/12/14
to accels...@googlegroups.com
#include <AccelStepper.h>

AccelStepper stepper(2,12,13);

const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 8;
const int brakeB = 9;
const int buttonPin = 2;

boolean buttonWasPressed;
int buttonValue;

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup()
{  
  pinMode(pwmA, OUTPUT);
  pinMode(pwmB, OUTPUT);
  pinMode(brakeA, OUTPUT);
  pinMode(brakeB, OUTPUT);
  
  digitalWrite(pwmA, HIGH);
  digitalWrite(pwmB, HIGH);
  digitalWrite(brakeA, LOW);
  digitalWrite(brakeB, LOW);
  
   stepper.setMaxSpeed(1000);
   stepper.setSpeed(1);
    stepper.moveTo(1500);
    pinMode(buttonPin, INPUT);
    buttonWasPressed = false;
}

void loop()
{  
  {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

    // deal with initial press
    if (buttonState == HIGH && buttonWasPressed == false) {
        stepper.moveTo(1500);
        buttonWasPressed = true;
    }
    // deal with the button being held down
    else if (buttonValue == HIGH && buttonWasPressed) {
        stepper.run();
    }
    // deal with the button being released
    else if (buttonValue == LOW && buttonWasPressed) {
        stepper.stop();
        while (stepper.distanceToGo() != 0) {
            stepper.run();
        }
        // reset the button so this doesn't trigger again next loop
        buttonWasPressed = false;
    }

}
 stepper.runSpeed();
   int sensorReading = analogRead(A0);
  // map it to a range from 0 to 100:
  int motorSpeed = map(sensorReading, 0, 1023, 1, 1600); //1600 is the max speed of my motor
  stepper.setSpeed(motorSpeed);
  Serial.println(sensorReading);
}

Sandy Noble

unread,
Jan 12, 2014, 6:45:29 PM1/12/14
to accels...@googlegroups.com
First thing.. What do you actually want it to do? The code I gave was a bit of guesswork on my part, so has probably contradicted what your parts are doing.

So describe the controls, what effect you want them to have etc, just in plain english - I should have asked that earlier!


sn

Grant Hobgood

unread,
Jan 12, 2014, 6:54:17 PM1/12/14
to accels...@googlegroups.com
I want to build a control box with 1 rotary encoder w/ push button to scroll a menu and step to the next option, effectively selecting each variable. Variables would be speed, delay, and run time. Speed being motor speed, delay being how long it stalls - based on shutter speed needed, and run time - dependent on how long I need my final timelapse to be in length. With those 3 selected, I would have a confirm option that would be my "go" button. Aside from that, I'd like to be able to either use the rotary encoder to select speed or use a pot to select speed seperately and be able to press and hold a button that would allow for video mode (read: constant moving).  I'd like to do this in both directions, up and down the rail. I'm not sure the easiest way to do this, whether put delay to 0 and run time to something suitable...or have it done seperately. 

The goal is basically an automated timelapse/video slider combo, with a menu on a LCD. I have a 16x2 serial LCD for that. 

Sandy Noble

unread,
Jan 12, 2014, 7:37:06 PM1/12/14
to accels...@googlegroups.com

That's a fairly ambitious plan, but in the current iteration, I guess you are just aiming for the video part of the project?

So. Controls are a pot and a button, in your current version.

The pot sets the speed of the movement, when it happens. When the button is held down, the motor will run at the speed set by the pot.  When the motor is released, the motor stops.

Questions then: does the pot define a constant speed, or a maximum speed? That is, when the button is held down, does the motor instantly start moving at a constant speed, or does it accelerate up to a max speed?

Same question for stopping. When releasing the button, does the motor stop instantly, or does it smoothly decelerate to a standstill?

Does anything special happen when the carriage is still moving when it runs out of track? (simple mechanical solution is it just judders around at the end if you make the links strong enough and don't over-spec the motor drive.)

I assume you will add a second button for moving in the opposite direction. Not something you need to fix now (don't add button 2 until you have solved button 1), but think about what happens when you press both buttons together, or try to reverse while in a deceleration phase.

What happens when you turn the pot while the motor is moving?

The answers to the questions form the basis of the logic in your program, so you do need to know them.

Re your variables - isn't motor speed and run time the same thing?

Sn

Grant Hobgood

unread,
Jan 12, 2014, 7:46:23 PM1/12/14
to accels...@googlegroups.com

Questions then: does the pot define a constant speed, or a maximum speed?  

       The pot would define a constant speed, which would be determined by turning through its range. It could be 5 or 800. Max being 800.


That is, when the button is held down, does the motor instantly start moving at a constant speed, or does it accelerate up to a max speed?

        I would like it to ramp slightly, but not over a prolong period. Ideally it would ramp up/down over a 1 second period at each end of the movement.

Same question for stopping. When releasing the button, does the motor stop instantly, or does it smoothly decelerate to a standstill?

        Decelerate over a 1 second period.

Does anything special happen when the carriage is still moving when it runs out of track? (simple mechanical solution is it just judders around at the end if you make the links strong enough and don't over-spec the motor drive.)

        I do not have, but can get, reed switches that would kill the motor instantly if hit. Ideally I would not hit them...but safety. 

I assume you will add a second button for moving in the opposite direction. Not something you need to fix now (don't add button 2 until you have solved button 1), but think about what happens when you press both buttons together, or try to reverse while in a deceleration phase.

What happens when you turn the pot while the motor is moving?

        Speed is to be set before movement, with no effect if changed during movement. Button deactivates changes from the pot in other words.

The answers to the questions form the basis of the logic in your program, so you do need to know them.

Re your variables - isn't motor speed and run time the same thing? No, motor speed would be how fast it moves down the track while run time would say how long it receives power. For example, my final clip will be 30 seconds long but it takes such minuscule steps (maybe 100 steps and stops for each movement) and I know it will take 3 hours to collect the amount of photos I need. motor speed will be say 500, while the run time will be 3 hours, 180 minutes...however it needs to be setup.



--

Alan Reinhart

unread,
Jan 12, 2014, 9:07:00 PM1/12/14
to accels...@googlegroups.com
Grant - I am working on exactly such an interface, but with a different approach.

I have a 4-digit, seven segment display which will show the numeric contents of various functions for an index head.  There is a 'menu' system consisting of a series of LED's.  A rotary encoder is used to cycle thru each 'menu' option: Tooth Count, Step by Degree, RPM, and one for setting micro-stepping options (1,1/2,1/4,1/8,1/16).  The functions could be anything desired.

At each menu item, when the rotary encoder knob is DEPRESSED (it is also a switch), the LED  turns green indicating that menu is in "edit" mode (I'm using RGB LED's so could have 3 states!). Further turning of the rotary encoder increments or decrements the displayed value.  When the RE knob is depressed again, the LED returns to RED and that value is set in that register or menu item. 

When the RUN button is pressed, the motor will step in accordance with the contents of the currently selected menu item.  Thus if the Tooth Count menu is selected, and is showing 12, each press of the run button will step the motor 1/12 of a revolution - or this could be for machining a piece of round stock to have 12 sides... or whatever value.  In the Degree menu, if it contains 30, the motor will step 30 degrees with each press of the run button.

As it turns out, this sort of application doesn't really take full advantage of AccelStepper as the speed of revolution isn't really an issue, and at low speeds, acceleration/deceleration  isn't such an issue.  For that reason I'm looking into a more simplistic 'bit-banger' style of stepper control.  I looked at AS as it looks like a powerful tool and I want to learn it for other projects.

I'll be documenting this on my website - send me a note if you'd like any further info.
=Alan R.  (a...@avrdev.net)

Grant Hobgood

unread,
Jan 13, 2014, 6:09:27 PM1/13/14
to accels...@googlegroups.com
Alan, a lot of what you said is probably really incredible...but I have no idea what it all means haha. I'd be interested in seeing what you come away with though!

Sandy Noble

unread,
Jan 13, 2014, 6:59:51 PM1/13/14
to accels...@googlegroups.com
Hi Grant, see below, maybe that has got the logic divided up right. Compiles ok anyway. (I only assume the motor definition stuff is right, I don't have a motorshield to test on.)

So there's three paths the execution can take, in the sequence they'll be taken too:
1) Button pressed, and hadn't been pressed on the last loop (aka "initial press"). In this path, we read the pot to see what speed it's been set to, and set that as the motor's maxSpeed. And we set a variable buttonWasPressed to TRUE, so that next time we're whizzing through the loop, we know that we're already moving, and we don't need to do all this reading pots/setting speed stuff again.

2) Button pressed, and HAD been pressed on the last loop (aka "held down"). In this path, we know the max speed has been set, all we do is run the motor. Because we're using run(), it'll accelerate up to the maxSpeed that we set in path 1. I changed the stepper.setAcceleration(..) in the setup() to quite a high value, so it should still be fairly responsive. You can actually usually whack acceleration up really high before it starts causing problems, but of course it depends what you're moving (top-heavy wobbly cameras probably not, ha!).

3) Button NOT pressed, but HAD been pressed on the last loop (aka "released"). In this path, we call stop() on the stepper, which basically sets the target to be as near as possible, while still allowing a proper deceleration, and we run in a loop until it's stopped.

The fourth path is not specified, but its 
4) Button NOT pressed and button had NOT been pressed last time around. This just means nothing is happening, nothing has changed and there's no input, so no need to mention it.

Things that spring out of this:
Deceleration is not instantaneous. So if you wait until the very end of the dolly to let go, it still might crash.
Limit switches on either end for sensing crashes are a good idea, and you can also use them to calibrate the dolly position too, but if you are happy doing it by eye, don't rush to get limit switches. You will probably want to use interrupts for them eventually.
Serial.prints are brilliant for debugging but they are also slow, and might prevent you from reaching your top speed (if you're anywhere close to it).
Reading the button using digitalRead() in the main loop is also slow. Again, interrupt-based IO is better, but a bit more complicated, especially for multiple inputs (buttons, limit switches, encoders etc).

See if this makes any sense!


long dollyLength = 1500L; // just set this to a value you know will never be reached
int maxSpeed = 1000;

void setup()
{  
  pinMode(pwmA, OUTPUT);
  pinMode(pwmB, OUTPUT);
  pinMode(brakeA, OUTPUT);
  pinMode(brakeB, OUTPUT);
  
  digitalWrite(pwmA, HIGH);
  digitalWrite(pwmB, HIGH);
  digitalWrite(brakeA, LOW);
  digitalWrite(brakeB, LOW);
  
   stepper.setMaxSpeed(maxSpeed);
   stepper.setAcceleration(1000);

   pinMode(buttonPin, INPUT);
   buttonWasPressed = false;
}

void loop()
{  
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

    // deal with initial press
    if (buttonState == HIGH && buttonWasPressed == false) {
        // See what setting the pot is at
        int sensorReading = analogRead(A0);
        // map it to a range from 0 to 1000 (maxSpeed):
        int motorSpeed = map(sensorReading, 0, 1023, 1, maxSpeed);
        Serial.print(sensorReading);
        Serial.print(" sensed, mapped to ");
        Serial.println(motorSpeed);
        stepper.setMaxSpeed(motorSpeed);

        stepper.moveTo(dollyLength);
        buttonWasPressed = true;

Grant Hobgood

unread,
Jan 13, 2014, 7:16:18 PM1/13/14
to accels...@googlegroups.com
Man, I appreciate that more than you know! I need to learn about buttons as far as wiring now. I got my pot working roughly with an analogSerialRead yesterday but it wasn't reading it's full range so I'll have to swap that in the morning for a new one. Once I figure the wiring for the button I should be able to test this out. Thanks again!

Grant Hobgood

unread,
Jan 13, 2014, 8:20:20 PM1/13/14
to accels...@googlegroups.com
I know you asked me previously why I had my buttonPin set to pin 2, and I'm wondering why that is wrong. I tried just copying how the wiring is done on the arduino.cc example and it just shuts off the boards power when I press the button. Just seems strange...

Sandy Noble

unread,
Jan 14, 2014, 4:42:13 PM1/14/14
to accels...@googlegroups.com
There's nothing special about pin 2 (as far as I know), connecting a button or a signal, or 5v or ground to it shouldn't make it reset. I only mentioned it before because you weren't actually reading the value of the pin (with digitalRead), only the value of the pin number.

What's the switch circuit you're using? The only thing I can think that would make the board reset using a switch on pin 2 would be if you were shorting the 5v line directly to the ground when you press it. Double check your circuit maybe? Also what kind of switch?



sn

Grant Hobgood

unread,
Jan 14, 2014, 5:45:37 PM1/14/14
to accels...@googlegroups.com
I'm using a simple two post SPST normally open push button. Honestly, I can't even tell you what I had anymore, it's been taken apart as I started to redo all of the basic tutorials to try and learn a bit. What I do remember though is I was just using it like a basic LED circuit but using it for motor power to pin 2 as it wasn't defined for a function already. I don't know that is necessarily right, but it was an attempt.

Grant Hobgood

unread,
Jan 14, 2014, 5:51:49 PM1/14/14
to accels...@googlegroups.com
I've got the previous code up again, with a potentiometer I know is good...and the resolution is crap. I don't know if it just isn't going to work like I'm hoping or if I've done something wrong. I've got it on 5v, GND, and A0. It controls it as it should, but it only gives me a few speed settings. I think before I add buttons I should maybe figure that out first. Looking at it in the Serial window only gives me 8 or 9 at the lowest and high 900s at the highest. I can't get down to 0, and I can't get up to 1023. Can I do something to possibly limit the range? Resistor of some amount?

Grant Hobgood

unread,
Jan 14, 2014, 5:56:31 PM1/14/14
to accels...@googlegroups.com
It also jumps quickly. It is fairly smooth from 9-45ish, then it jumps to mid 100s then it jumps WAY up to 900s. 

Sandy Noble

unread,
Jan 14, 2014, 7:16:21 PM1/14/14
to accels...@googlegroups.com
There's two kinds of potentiometers, linear and logarithmic - in logarithmic ones the resistance increases fast like that, I guess that's what you've got anyway. (They are mostly used for audio control.) Could just be a crappy pot of course. The actual practical range doesn't much matter though, you just change your code so that you map the values from 8 to 980 instead of 0 to 1023. Though you're right in principle, when the pot is turned fully one way, the wiper (the centre pin, the bit that moves along the resistive track) should be sitting on a pad directly shorted to 5v, and at the other end, it should be directly shorted to GND. 

Check it by wiring GND or 5v directly to your A0, and look at the values maybe.

sn

Grant Hobgood

unread,
Jan 14, 2014, 7:29:15 PM1/14/14
to accels...@googlegroups.com
I've got a linear, that's the thing. I've bought two pots from radioshack...both are identical, and they both had linear on the bag/tag. If I change the code for 9-980 say, will 9 be a dead stop?

Sandy Noble

unread,
Jan 15, 2014, 3:59:31 PM1/15/14
to accels...@googlegroups.com
Yes if you change the map range, it'll do that. int motorSpeed = map(sensorReading, 10, 980, 0, maxSpeed); (or something like that).

I guess the amount that the full 1-1024 range is truncated depends on the pot, and maybe the supply voltage. http://www.tinkerkit.com/map-and-calibration/ has a chat about this. Testing with an arduino on the desk in front of me indicates that ground has a value of 0 (which is reasonable), but wiring to the 5v line only gives a reading of around 916.

I can't explain the non-linear voltage change, if it isn't a log pot.


sn

Grant Hobgood

unread,
Jan 16, 2014, 3:57:38 PM1/16/14
to accels...@googlegroups.com
I'm trying to do away with the pot in the code you posted, in hopes I can at least get the button working. I'm not getting much success from my efforts. I'll try it with them in first though because I ordered more pots, this time from adafruit. I can use it on a breadboard as well so it should be easier to wire up. 


I got this working on a different code that I peacemealed but it got deleted lol. People moving things they shouldn't ;). I've tried getting it back together but no luck there either. I'm going to let this rest until I get back from Louisville this weekend and then jump back on it and tackle it. 

Sandy Noble

unread,
Jan 16, 2014, 6:05:49 PM1/16/14
to accels...@googlegroups.com
Since this isn't really anything to do with accelstepper, I'm happy to take it off-list if you want to email directly.


sn

Grant Hobgood

unread,
Jan 16, 2014, 7:19:29 PM1/16/14
to accels...@googlegroups.com
I will email you when I get back. Thanks!
Reply all
Reply to author
Forward
0 new messages