First project 99% working, minor gremlin

Hi all,
First post but Ive been lurking about a month, great community!

So I’ve got my first project nearly complete but I’m having a minor issue.

My project simply moves a tv up/down with the press of a button, like a garage door button.

It uses an Arduino Uno R3, stepper motor, stepper driver, 2 micro limit switches, and 1 momentary button.

Ok here’s my issue:
It works as designed except the momentary button seems to have a mind of its own sometimes.
I’m leaning towards interference, but I honestly don’t know. (I have not tried shielded cable yet)
The button seems to trigger the movement operation completely at random sometimes, other times it doesn’t do it at all. I can’t purposefully recreate the problem,

I use a 1k pullup resistor on both switches, and momentary button. all are pulling 5v from the Arduino.

I can post my code if you think that might be it.
Any guidance is much appreciated,
Thank you,

Drew

DrewZ: I can post my code if you think that might be it.

Not a bad idea just in case. A quick schematic or wiring diagram would also be a good idea in a case like this I would think.

How long are the wires from the buttons to the controller?

Delta_G: Not a bad idea just in case. A quick schematic or wiring diagram would also be a good idea in a case like this I would think.

How long are the wires from the buttons to the controller?

Thank you for the quick reply.

The momentary button is connected with 50' of 20ga stranded wire. Note, I did have this problem when it was all being tested on my bench with 6 inches of wire. It did only seem to happen though when I would pick up the switch and the wires would drag across other wires. The button was not screwed down to anything at that time.

I will draw up a wiring diagram.

Thanks again!

Sounds like a grounding issue. Are all pins grounded or pulled high? The reset pin on a pull-up?

Johnny010:
Sounds like a grounding issue. Are all pins grounded or pulled high? The reset pin on a pull-up?

Yes they’re all grounded.
I’m using 3 wires. 5v+resistor digital pin on Arduino, and ground.
I’m using pins 4 and 5 for my limit switches, and pin 2 for my momentary button.

They’re all pulled high with a 1k ohm resistor.
Re-reading my code it appears the limit switches are NO, when they’re pressed they read HIGH.
I have them wired 5v to resistor to the C contact, and pin 4 goes to the C contact as well and a ground to the NC contact.
I need to draw up that wiring diagram.

The momentary button is not, its always HIGH until pressed.

I will have to double check, I do not have my project in front of me right now, only my code.

Thank you.

50' is a long long ways for a 5V signal. You might want to try 12v and level shift it at the Arduino end.

I normally use a 4k7 or 10k for pull up's. It sounds like to me a noise issue with out see the drawing or code, what voltage are the stepper motors running from ? I had a simular issue with a project that I did where it just randomly operated on its own causing it to start lifting, at first I thought it was a code issue rather than hardware but after some investigation it was related to a noise issue as the buttons where on a long lead which was picking up interface/noise with electrical stuff been turned on/off causing a spike and activating the function. Basicly the cable was acting as a big antenna. I cured this by placing an input filter on the button circuit and carried out loads of testing and the fault did not recur and it has under gone CE testing. I'll try and post the circuit tomorrow as I'm on my I-pad and drawing is on PC

Thank you Delta_G, Id have to get another power source for 12v so if one of these other solutions doesn't remedy it I will give that a shot.

Steveiboy: I normally use a 4k7 or 10k for pull up's. It sounds like to me a noise issue with out see the drawing or code, what voltage are the stepper motors running from ? I had a simular issue with a project that I did where it just randomly operated on its own causing it to start lifting, at first I thought it was a code issue rather than hardware but after some investigation it was related to a noise issue as the buttons where on a long lead which was picking up interface/noise with electrical stuff been turned on/off causing a spike and activating the function. Basicly the cable was acting as a big antenna. I cured this by placing an input filter on the button circuit and carried out loads of testing and the fault did not recur and it has under gone CE testing. I'll try and post the circuit tomorrow as I'm on my I-pad and drawing is on PC

That would be great, thanks. I'm thinking noise too, the wiring goes up into an acoustic ceiling where there are other wires like CTTV etc. I'm using a 48vdc regulated power supply for my stepper/driver, its a 269oz-in stepper, driver is rated 50vdc I'm using the same power supply to power the Arduino, at the Arduino I have a buck converter that takes that 48 volts down to 9v.

Drew

It appears I am using 10k ohm resistors, my mistake.

Here is my code. note like my title says, I’m a newbie.
tried my best to comment / formatted correctly.

#include <AccelStepper.h>
const byte stepPin1 = 8;
const byte stepPin2 = 9;
const byte stepPin3 = 10;
const byte stepPin4 = 11;

const byte buttonPin = 2;
const byte limitTop = 4;
const byte limitBottom = 5;

int Direction = HIGH;
const int topSpeed = 10000;  // Set stepper max speed

boolean Initialized = false;
boolean ToggleSwitch = false;
boolean limitTpressed = false;
boolean limitBpressed = false;
boolean inMotion = false;


AccelStepper myStepper(AccelStepper::FULL4WIRE, stepPin1, stepPin2, stepPin3, stepPin4);

void setup ()
{
  pinMode (limitTop, INPUT);
  pinMode (limitBottom, INPUT);
  pinMode (buttonPin, INPUT);
  myStepper.setMinPulseWidth(20);
  myStepper.setMaxSpeed(topSpeed);
}

void loop ()
{
  if (!Initialized) { // System turned on, move to top and initialize
    movetoTop();
  }
  if (Initialized)  {  // If it's initialized, proceed
    if (!ToggleSwitch) {  // If the Momentary button hasn't been pressed, check it,
      ToggleCheck(); // sets Momentary button(ToggleSwitch) value.
    }
    if ((inMotion) && (Direction == HIGH)) {
      movetoTop();
    }
    if ((inMotion) && (Direction == LOW)) {
      movetoBottom();
    }  //if its not inMotion do nothing.
  } 
}

void ToggleCheck()
{
  int val = digitalRead(buttonPin);
  if (val == LOW) { 
    ToggleSwitch = true;
    inMotion = true;
    Direction = (Direction == HIGH) ? LOW : HIGH;  //Toggles which direction it has been moved when button is pressed
  }
  if (val == HIGH) {
    ToggleSwitch = false;
  }
}

void movetoTop()
{
  int val = digitalRead(limitTop); //get top limit switch value
  if (val == HIGH) {
    limitTpressed = true;
  }
  if (val == LOW) {
    limitTpressed = false;
  }

  if (!limitTpressed) { //if the top limit switch isn't pressed, move it up.
    inMotion = true;
    myStepper.moveTo(400000); //uses acceleration so motor doesnt stall
    myStepper.run();
  }
  if (limitTpressed) { //halt movement
    myStepper.setCurrentPosition(0);
    Initialized = true;
    inMotion = false;
    ToggleSwitch = false;
    Direction = HIGH;
  }
}

void movetoBottom()
{
  int val = digitalRead(limitBottom);   //get bottom limit switch value
  if (val == HIGH) {
    limitBpressed = true;
  }
  if (val == LOW) {
    limitBpressed = false;
  }
  if (!limitBpressed) { //if the bottom limit switch isn't pressed, move it down at full speed.
    inMotion = true;
    myStepper.setSpeed(-topSpeed); // Sets top speed to go in reverse (Move down)
    myStepper.runSpeed();
  }
  if (limitBpressed) { //halt movement
    myStepper.setCurrentPosition(0);
    inMotion = false;
    Direction = LOW;
    ToggleSwitch = false;
  }
}

I've had a quick glance through your code, I'm no expert my self but I would say try swaping const byte for your inputs to #Define I think that you have 7 inputs for switches but you have only set 3 in setup ? I will look more into your code later but some stuff could be done better I think, it's all part of the fun and learning curve

Steveiboy: I've had a quick glance through your code, I'm no expert my self but I would say try swaping const byte for your inputs to #Define I think that you have 7 inputs for switches but you have only set 3 in setup ? I will look more into your code later but some stuff could be done better I think, it's all part of the fun and learning curve

8,9,10,11 are my stepper motor pins

I've changed all those pin definitions to #define

Steveiboy: I've had a quick glance through your code, I'm no expert my self but I would say try swaping const byte for your inputs to #Define

What exactly do you hope to achieve with that change?

aarg: What exactly do you hope to achieve with that change?

Well like I said I'm no expert and never seen a setup pin's like that before, I've seen an Int but not const byte.

Drew looking through your code again I don't see any debounce routines for the input button's, May be this could be causing the issue or does not help having any debounce in there. I'm looking into the filter that I used which I shall upload in a short while

This is the filter design I used in my project and works very well, Mine worked on a 24V system.
Depending on what push buttons/limit switches you have, R2,R4&R5 place a small load on the push buttons or limit switches as the data sheet for mine recommended they had to have a small amount of current flowing through them to give a positive switching action(I tested this on of the machines that used the same type limit switch and they had 47 milli-amps flowing through them). D2 can be any Schttky didoe, D1can be a Transil-doide or zener diode. C1,C2 & C3 act as a filter and R1 helps limit the current, R2 just makes sure the pin is held high, When the push button is pressed the output goes low.
In my project I have 3 push buttons and 4 limit switches and each one as the input filter added.
Like I mentioned my system was 24V so I've changed the values of R3,R4 & R5 which draws about 30mA and I've only simulated this, You may just want to build and try it on a 48V system to test first and make sure you only get 5V to the input pins

Using defines will encourage best practices.

I would add some debug lines to your code so that you can see what is happening. You have a lot of if then logic going on and my guess is the logic is getting messed it somewhere.

It could also be your wiring, either with one of the endstops or the button but I think software is most likely.

I’ve had a little play around with your code and added debounce to the ToggleCheck() loop.
Now it complies but I have not tried it as I don’t have hardware for it, Others may correct me and there may be a better ways, but I find this works ok, I’m still learning myself and enjoy tinkering about.

#include <AccelStepper.h>
const byte stepPin1 = 8;
const byte stepPin2 = 9;
const byte stepPin3 = 10;
const byte stepPin4 = 11;
const byte buttonPin = 2;
const byte limitTop = 4;
const byte limitBottom = 5;
const unsigned long debounceTime = 10;  // milliseconds without delay
unsigned long switchPressTime;  // when the switch last changed state
byte oldSwitchState = HIGH;  // assume switch open because of pull-up resistor
int Direction = HIGH;
const int topSpeed = 10000; // Set stepper max speed

boolean Initialized = false;
boolean ToggleSwitch = false;
boolean limitTpressed = false;
boolean limitBpressed = false;
boolean inMotion = false;


AccelStepper myStepper(AccelStepper::FULL4WIRE, stepPin1, stepPin2, stepPin3, stepPin4);

void setup ()
{
  pinMode (limitTop, INPUT);
  pinMode (limitBottom, INPUT);
  pinMode (buttonPin, INPUT);
  myStepper.setMinPulseWidth(20);
  myStepper.setMaxSpeed(topSpeed);
}

void loop ()
{
  if (!Initialized) { // System turned on, move to top and initialize
    movetoTop();
  }
  if (Initialized) { // If it's initialized, proceed
    if (!ToggleSwitch) { // If the Momentary button hasn't been pressed, check it,
      ToggleCheck(); // sets Momentary button(ToggleSwitch) value.
    }
    if ((inMotion) && (Direction == HIGH)) {
      movetoTop();
    }
    if ((inMotion) && (Direction == LOW)) {
      movetoBottom();
    } //if its not inMotion do nothing.
  }
}

void ToggleCheck()
{

  int val = digitalRead(buttonPin);
  //##############################
  // switch debounce starts here #
  // #############################
  if (val != oldSwitchState)
  {
    // debounce switch starts here
    if (millis () - switchPressTime >= debounceTime)
    {
      switchPressTime = millis ();  // when we closed the switch
      oldSwitchState =  val;  // remember for next time
      if (val == LOW)
      {
        ToggleSwitch = true;
        inMotion = true;
        Direction = (Direction == HIGH) ? LOW : HIGH; //Toggles which direction it has been moved when button is pressed
      }  // end if switchState is LOW
      else
      {
        ToggleSwitch = false;
      }
    }
  }// end of state change
}

void movetoTop()
{
  int val = digitalRead(limitTop); //get top limit switch value
  if (val == HIGH) {
    limitTpressed = true;
  }
  if (val == LOW) {
    limitTpressed = false;
  }

  if (!limitTpressed) { //if the top limit switch isn't pressed, move it up.
    inMotion = true;
    myStepper.moveTo(400000); //uses acceleration so motor doesnt stall
    myStepper.run();
  }
  if (limitTpressed) { //halt movement
    myStepper.setCurrentPosition(0);
    Initialized = true;
    inMotion = false;
    ToggleSwitch = false;
    Direction = HIGH;
  }
}

void movetoBottom()
{
  int val = digitalRead(limitBottom); //get bottom limit switch value
  if (val == HIGH) {
    limitBpressed = true;
  }
  if (val == LOW) {
    limitBpressed = false;
  }
  if (!limitBpressed) { //if the bottom limit switch isn't pressed, move it down at full speed.
    inMotion = true;
    myStepper.setSpeed(-topSpeed); // Sets top speed to go in reverse (Move down)
    myStepper.runSpeed();
  }
  if (limitBpressed) { //halt movement
    myStepper.setCurrentPosition(0);
    inMotion = false;
    Direction = LOW;
    ToggleSwitch = false;
  }
}

Like said there is a lot of logic behind it and there would be a better way to do it.
Let us know how it goes