I have a very simple application where I am moving a stepper motor each direction till it hits a switch. When a switch is triggered it will reverse the motor towards the other switch. The motor moves a carriage along a rail so the moves are fairly long (few meters).
The code works well, however, sometimes (can happen after 20 minutes, or 1 hour without any consistency or any rule), it would break in either of 2 ways:
It stops reacting to one of the switches (happens to be the same switch each time) and all it takes to fix this is just to cycle power or reset the Arduino Every board.
Upon hitting a switch (either of them) it would first accelerate the motor to about twice the speed for a few revolutions and then reverse it. And it will keep doing this till cycling power or resetting the board.
I would have referred it to a wiring problem or noise (the wires to the switches are long as you may imagine), but if for the first issue it may have any relation, but then for the second don't see how it could relate and furthermore, if it were a noise, then how resetting fixes this...? BTW, I have 0.1uF capacitor beween the switch input and 5V to filter the noise.
Any ideas?
Here is my code:
#include <AccelStepper.h>
AccelStepper stepper(1,3,4); // Step pin #3, DIR pin #4
byte SW1 = 5; // switch1 pin
byte SW2 = 6; // switch2 pin
int potPin = A0;
int potValue;//potentiometer input for motor speed setting
int motorSpeed = 0;
long movement = 1; //used as movement segment
void setup()
{
pinMode(SW1, INPUT_PULLUP); // set to input pin to active low
pinMode(SW2, INPUT_PULLUP); //set to input pin to active low
potValue = analogRead(potPin);
motorSpeed = map(potValue, 0, 1023, 100, 1600); //map to speed range
stepper.setMaxSpeed(motorSpeed);
stepper.setAcceleration(1000);
}
void loop()
{
while (digitalRead(SW1)) { //keeps advancing the stepper a step at a time as long as the switch is not trigerred
stepper.move(movement);
stepper.run();
movement += 1;
}
movement = -1;
while (digitalRead(SW2)) {
stepper.move(movement);
stepper.run();
movement -= 1;
}
movement = 1;
}
just my experience. cnc plotter wont homing, Y axes always stops, but not X axes because of short wires. external resistor give some help but not always. after i twisted the wires of Y axes switch was no problem more, capacitor and resistor was not desoldered, i don't know if it work without them too.
One interesting new thing I discovered: when the issue happened, namely it bumped into the switch but failed to reverse direction, I (as opposed to previous times) left like this, hearing the stepper loosing steps and after some time it eventually did reverse the direction and started normal movement for the next 20 minutes or so.
I am suspecting the internal pull-up resistors - is there any chance when one input is pulled down externally it would influence another input's voltage level? Do the inputs on the Nano Every have physical explicit pull-up resistors?
Cannot understand from the ATmega4808 datasheet what are the values of the pull-up resistors, looks like they are anywhere between 20k and 50k (typical 35k), which looks way too large values - currents in the range of few micro amps at 5V. Looks like I would benefit using external 2k or so pull-up resistors...? 2.5mA looks much better than 0.14 uA to me.
The actual values of the pullups in the microcontroller depend on fabrication process variations. Usually the value is "good enough" when all the circuitry is on the board or within an inch or two, you certainly can go with external resistors in your case, unless you want to use shielded wire for your connection to the switch! A few mA won't hurt you.
Well, this was my understanding and this was the other reason keeping the moves small, which seems to work.
I think this will drive it crazy - when the switch is hit and till it released the loop will be executed more than once and keep changing directions forever, not giving the motor to get off the switch.
you are right. do you can to correct this?
wait, how long will 10 movement made? is it not enough time to leave the switch position?
how much "movement" is actually the whole way from switch to switch?
A carriage slides over the switch, so it takes a while till it leaves releasing the switch. Don't know how much time, I would guess around 0.5 sec. From switch to switch is around 120 revolutions of the motor.
Anyway, my code does the work in practice, I was just wondering if you can recognize anything that might be causing the issue I described, otherwise I see no point changing a working code.