Driving A Stepper motor using 3 button settings and 2 pots.

Hi,

Updated this post a few time to reflect the early comments on this thread, please take a fresh look at the issue I am having below.

I have 2 pots,
1 stepper motor

1st pot ( val is on input 0 )
2nd pot ( frame is on input 1 )

3 push buttons.

When I rotate potA attached to a wheel the stepper motor keeps rotateing in one direction and then stops when it matches potB that’s attached to the frame. When potA rotates the opposite way the stepper motor rotates and stops when potB matches potA again.

The 3 buttons are supposed to be used for altering the perameters of the stepper motors value etc when each button is pressed.

So when I press button 2 ( track setting ) the stepper should only recieve the figures etc that is relevant for that setting.

My issue is that is appears to be stuck on reading the 3rd option or button 2 of 0,1 & 2? It rotates exactly as I need it to and changes direction etc, However it won’t update or call on the other motor settings when I press another button.

The sketch compiles with no errors at this point as below.
I can also see on the com display that the sketch is recognising both pots and button selection.

There is also an issue wherby if the following line;
Serial.begin(9600); // set up serial port for output
is included in the sketch the stepper motor doesn’t behave properly as if it’s missing steps or not getting enough? as soon as I take the line out, I cant see the com results but the motor behaves properly?

// 2 Pots, 3 Buttons, Stepper motor rotation control jason187781.

#include <Stepper.h>

#define STEPS 400

Stepper stepper(STEPS, 8, 9, 10, 11);

int frame = 0;    // the initial position of the frame.

const int RallyButton = 7;   // Setting Button on button 0
const int RoadButton = 6;    // Setting Button on button 1
const int TruckButton = 5;   // Setting Button on button 2

boolean RallyState;    // Setting Rally State
boolean RoadState;     // Setting Road State
boolean TruckState;    // Setting Truck State

int Setting = 0;    // 0 Rally. 1 Road, 2 Truck

int Rally_prev;
int Rally_current;
int Road_prev;
int Road_current;
int Truck_prev;
int Truck_current;

void setup()

{
  pinMode(RallyButton, INPUT);   // set up button 0 as input
  pinMode(RoadButton, INPUT);    // set up button 1 as input
  pinMode(TruckButton, INPUT);   // set up button 2 as input

  // set the speed of the motor to 50 RPMs
  stepper.setSpeed(150);
}
void loop()
{

  int val = analogRead(0); // Wheel Postion Read
  int frame = analogRead(1); // Frame Position Read

  // Output the values to the serial port
  Serial.print(val, DEC);
  Serial.print(",");
  Serial.print(frame, DEC);
  Serial.print(",");
  Serial.print(Setting, DEC);
  Serial.println();

  // check if the "Rally" button has been pressed
  // if yes, then set then run "true" setup as below.
  Rally_prev = Rally_current; Rally_current = digitalRead(RallyButton);
  if (Rally_prev == LOW && Rally_current == HIGH) {
    changeRallyButtonState();
  }
  if (RallyState == true)
    Setting = 0;

  // Stepper Motor Settings for Rally State.
  val = map (val, 0, 1023, 0, 1023);
  if ((val - frame) != 0) {
    if ((val - frame) > 400) { // Deadzone
      stepper.step(1); // move a step toward the pot reading.
      frame++;
    }
    if ((val - frame) < -400) { // Deadzone
      stepper.step(-1); // move a step toward the pot reading.
      frame--;
    }
  }

  // End of Rally settings state.

  RallyState = false;

  // check if the "Road" button has been pressed
  // if yes, then set then run "true" setup as below.
  Road_prev = Road_current; Road_current = digitalRead(RoadButton);
  if (Road_prev == LOW && Road_current == HIGH) {
    changeRoadButtonState();
  }
  if (RoadState == true)
    Setting = 1;

  // Stepper Motor Settings for Rally State.
  val = map (val, 0, 1023, 150, 850);
  if ((val - frame) != 0) {
    if ((val - frame) > 100) { // Deadzone
      stepper.step(1); // move a step toward the pot reading.
      frame++;
    }
    if ((val - frame) < -100) { // Deadzone
      stepper.step(-1); // move a step toward the pot reading.
      frame--;
    }
  }

  // End of Rally settings state.

  RoadState = false;

  // check if the "Truck" button has been pressed
  // if yes, then set then run "true" setup as below.
  Truck_prev = Truck_current; Truck_current = digitalRead(TruckButton);
  if (Truck_prev == LOW && Truck_current == HIGH) {
    changeTruckButtonState();
  }
  if (TruckState == true)
    Setting = 2;

  // Stepper Motor Settings for Rally State.
  val = map (val, 0, 1023, 300, 800);
  if ((val - frame) != 0) {
    if ((val - frame) > 150) { // Deadzone
      stepper.step(10); // move a step toward the pot reading.
      frame++;
    }
    if ((val - frame) < -150) { // Deadzone
      stepper.step(-10); // move a step toward the pot reading.
      frame--;
    }
  }

  // End of Rally settings state.

  TruckState = false;

  // ensures that Processing can read the values before the next values arrive
}
void changeRallyButtonState() // Rally button state logic
{
  if (RallyState == true) {
    RallyState = false;
  }
  else {
    RallyState = true;
  }
}
void changeRoadButtonState() // Road button state logic
{
  if (RoadState == true) {
    RoadState = false;
  }
  else {
    RoadState = true;
  }
}
void changeTruckButtonState() // Truck button state logic
{
  if (TruckState == true) {
    TruckState = false;
  }
  else {
    TruckState = true;
  }
}

Any help in figuring out why it won’t read the values under the button selection insead of the 3rd ( truck ) option regardless of what I press or why the addion of serial read messes with the stepper motor control would be appreciated :smiley:

Thanks

Jason

Your code is very poorly formatted so it is too hard to follow.

Please re-format it so every statement and every { and } is on its own line.

Get rid of all the unnecessary {} pairs.

Then use the AutoFormat tool to indent everything logically

...R

Robin2:
Your code is very poorly formatted so it is too hard to follow.

Please re-format it so every statement and every { and } is on its own line.

Get rid of all the unnecessary {} pairs.

Then use the AutoFormat tool to indent everything logically

...R

Hi,

Thanks for the cleanup tip, that made it instantly clearer. Sorry I'm new to this and still finding my way.

Hopefully it's clearer now for some further advise.

Thanks

Jason

You still have your {} all wrong. For example the } on line 107 terminates the loop() function prematurely.

Have you tried to compile the program?

...R

Robin2:
You still have your {} all wrong. For example the } on line 107 terminates the loop() function prematurely.

Have you tried to compile the program?

...R

Hi,

I have taken out some of the {} and it's at least compiling ok.

New version is in the first post.

The pot's are working ok as well and recognsing which direction the stepper motor should be going.

So it's definitly a step in the right direction :smiley: The motor sketch now works within the new sketch as it did on it's own.

However it doesn't appear to be recognising my selection of button press to update values for the motor defined under each button heading? It is however using the Truck settings which is the last setup of the three options near the bottom of the sketch. Pressing buttons for the other settings doesn't do anything or re-define what values it should be calling on?

Would be happy to hear if you have anymore tips.

Thanks again

Jason.

Jason187781:
New version is in the first post.

Don't do that. It makes a nonsense of the Thread for other readers. Please re-instate the first post and put the new code in after this.

...R

I can't figure how you are using RoadState, RallyState etc. It looks like it runs through them all each iteration of loop(). For example, you call changeRoadButtonState() and a few lines later you have RoadState = false;

Maybe it's just that I don't understand what you are trying to do.

...R

Robin2:
I can’t figure how you are using RoadState, RallyState etc. It looks like it runs through them all each iteration of loop(). For example, you call changeRoadButtonState() and a few lines later you have RoadState = false;

Maybe it’s just that I don’t understand what you are trying to do.

…R

Hi firstly thanks for sticking with me.

My lack of exsperience in Arduino ( Been on it for about 3 weeks now ) is showing so you may have to bare with me if you can.

I altered the first post to skip the noob quick changes that I exspected to get but will leave it alone from now on and add a bit more history back when I get home from work later.

I’m not 100% sure about the button state logic in the sketch, The way I see it is that when the button is pressed it sets the Setting to a number ( 0,1,2 ) if it’s not pressed then it skips to the next part of the sketch ( Rally, Road, Truck ) and checks if it’s been pressed there. If it has then the Setting is used as an output i think? The setting was originally a colour setting for the original Etch a Sketch programme. I need it to define a set of peramiters for a stepper motor. I will try and exsplain a bit further on.

changeRoadButtonState(); ( So this is for the state? )
}
if (RoadState == true) ( Checks if the button is on? )

Setting = 1; ( if it is then the setting is 1? )

Mabye there should be an ‘else’ here?

RoadState = false;

I maybe trying to implement the 3 button control all wrong, if I start from the begining I will try and exsplain it as clear as possible.

I needed a sketch that would work with a stepper motor so looked for some examples and altered an exsisting one I found that gave me the desired result. I had to introduce a second pot into the sketch but ultimatley with a bit of tweaking I got it to work. So I had my first working sketch.

This first Sketch for the Stepper allowed me to rotate one potentiometer ( PotA ) in any direction, this would trigger the stepper motor to rotate until the second potentiometer ( PotB ) matched the first pot. If they were the same then the stepper motor wouldn’t move, great :slight_smile:

It also had enough detail within the sketch to allow me to tweak the speed, Comparitive range from potA - PotB, Deadzone and a few other tweaks so was perfect for my hardware application.

Then I set out looking for another sketch that would allow me to use 3 buttons, with a plan for each button to re-defining the stepper motor values ( Setting ).

So I found another sketch that was meant for an Etch a Sketch setup. It had 3 buttons, 2 pots and worked perfectly on it’s own. When running the com3 display I could see that it was reading the 2 pot values and 3 button presses. It switched between the button presses and worked perfectly in it’s loop. The ‘Setting’ value as I now define it was originally meant to change a ‘colour’

So I renamed some of the other colour values, stripped out the old Pot setup ( or at least most of it ) and added in the first sketch I tweaked for the Stepper motor.

So now when the RallyState is true the setting would be ‘0’

if (RallyState == true)
Setting = 0;

This is where it all gets a bit theoretical and my exsperience shows massivly.

So after this bit of sketch for each button I thought a good start was to enter the stepper motor loop and hoped this simple addition would look at the setting and if it was true for the button press then it would only read these values below the ( Setting = 0; ) line, if it was false it would move onto the next button which has a different motor steps, range value etc. I think I can see now that it’s reading the last motor setup ( Truck ) because it’s the last one on the loop.

I appreciate the {} and () may be confilicting but when I upload the sketch and check the com3 reading it does register the button press and stays on the button. Is this an indication that it’s reading the button presses or keeping the button press as the go to value for the sketch?

I may be looking at this all wrong.

Should I be putting something like or at least similar in working code like this below?

If setting = 0 then (val = map (val, 0, 1023, 300, 800));
If setting = 0 then (if ((val - frame) != 0)) {
If setting = 0 then (if ((val - frame) > 150)) { // Deadzone
If setting = 0 then (stepper.step(10)); // move a step toward the pot reading.
frame++;
}
If setting = 0 then (if ((val - frame) < -150)) { // Deadzone
If setting = 0 then (stepper.step(-10):wink: // move a step toward the pot reading.
frame–;
}
}

And do this for all 3 ( Rally, Truck, Road ) settings?

The truth is I feel like I’m getting close to a solution now and I have done all the example sketches working with buttons and potentiometers, the code that looked like something out of the Matrix at the begining is getting clearer day by day but it’s just that bit too complex for me right now to be able know where to look, Clearing out some of the {} may have gotten the code to compile but now if I start stripping out anymore I feel I may be heading backwards?

Again, thanks for your advice.

Any more you can comment on would be great.

Cheers

Jason

I don't have time to study Reply #7 now. I will bookmark it and hopefully remember to look at it in the morning.

...R