I'm totaly new in arduino programming, I got code for stepper motor moving with two buttons cw and ccw. I want to embed code for 3rd switch to run stepper until 4th switch is activated ... but no luck for now. I need some help please.
Code:
// testing a stepper motor with a Pololu A4988 driver board or equivalent
// this version uses millis() to manage timing rather than delay()
// and the movement is determined by a pair of momentary push switches
// press one and it turns CW, press the other and it turns CCW
byte directionPin = 4;
byte stepPin = 3;
byte buttonLevel = 8;
byte endSwitch = 9;
byte buttonCWpin = 10;
byte buttonCCWpin = 11;
boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;
boolean buttonLevelpressed = false;
boolean endSwitchpressed = false;
byte ledPin = 13;
unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 1; // milliseconds
void setup() {
Serial.begin(9600);
Serial.println("Starting Stepper Demo with millis()");
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonCWpin, INPUT_PULLUP);
pinMode(buttonCCWpin, INPUT_PULLUP);
pinMode(buttonLevel, INPUT_PULLUP);
pinMode(endSwitch, INPUT_PULLUP);
}
void loop() {
curMillis = millis();
readButtons();
actOnButtons();
}
void readButtons() {
buttonCCWpressed = false;
buttonCWpressed = false;
buttonLevelpressed = false;
endSwitchpressed = true;
if (digitalRead(buttonCWpin) == LOW) {
buttonCWpressed = true;
}
if (digitalRead(buttonCCWpin) == LOW) {
buttonCCWpressed = true;
}
if (digitalRead(buttonLevel) == LOW) {
buttonLevelpressed = true;
}
}
void actOnButtons() {
if (buttonCWpressed == true) {
digitalWrite(directionPin, LOW);
singleStep();
}
if (buttonCCWpressed == true) {
digitalWrite(directionPin, HIGH);
singleStep();
}
if (buttonLevelpressed == true) {
digitalWrite(directionPin, LOW);
multiStep();
}
}
void singleStep() {
if (curMillis - prevStepMillis >= millisBetweenSteps) {
// next 2 lines changed 28 Nov 2018
//prevStepMillis += millisBetweenSteps;
prevStepMillis = curMillis;
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
}
}
void multiStep() {
for (int i = 0; i < 200000; i++) {
if (curMillis - prevStepMillis >= millisBetweenSteps) {
// next 2 lines changed 28 Nov 2018
//prevStepMillis += millisBetweenSteps;
prevStepMillis = curMillis;
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
}
if (digitalRead(endSwitch) == false) {
break;
}
}
}
When I press "buttonLevel" nothing happens - this button is meant to run stepper until "endStop" button gets hit.
I repaired code and added commands that when stop switch is on, motor runs some small amount backwards. I use this on home made laser engraver to have some kind of auto-focus add-on (always same distance from laser head and material, no matter what thickness of material is).
New code:
// testing a stepper motor with a Pololu A4988 driver board or equivalent
// this version uses millis() to manage timing rather than delay()
// and the movement is determined by a pair of momentary push switches
// press one and it turns CW, press the other and it turns CCW
byte directionPin = 4;
byte stepPin = 3;
byte buttonLevel = 8;
byte endSwitch = 9;
byte buttonCWpin = 10;
byte buttonCCWpin = 11;
boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;
boolean buttonLevelpressed = false;
boolean endSwitchpressed = false;
byte ledPin = 13;
unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 1; // milliseconds
void setup() {
Serial.begin(9600);
Serial.println("Starting Stepper Demo with millis()");
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonCWpin, INPUT_PULLUP);
pinMode(buttonCCWpin, INPUT_PULLUP);
pinMode(buttonLevel, INPUT_PULLUP);
pinMode(endSwitch, INPUT_PULLUP);
}
void loop() {
curMillis = millis();
readButtons();
actOnButtons();
}
void readButtons() {
buttonCCWpressed = false;
buttonCWpressed = false;
buttonLevelpressed = false;
endSwitchpressed = false;
if (digitalRead(buttonCWpin) == LOW) {
buttonCWpressed = true;
}
if (digitalRead(buttonCCWpin) == LOW) {
buttonCCWpressed = true;
}
if (digitalRead(buttonLevel) == LOW) {
buttonLevelpressed = true;
}
if (digitalRead(endSwitch) == LOW) {
endSwitchpressed = true;
}
}
void actOnButtons() {
if (buttonCWpressed == true) {
digitalWrite(directionPin, LOW);
singleStep();
}
if (buttonCCWpressed == true) {
digitalWrite(directionPin, HIGH);
singleStep();
}
if (buttonLevelpressed == true) {
digitalWrite(directionPin, LOW);
multiStep();
}
if (endSwitchpressed == true) {
}
}
void singleStep() {
if (curMillis - prevStepMillis >= millisBetweenSteps) {
prevStepMillis = curMillis;
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
}
}
void multiStep() {
while (digitalRead(endSwitch) == HIGH) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
delay(200);
backStep();
}
void backStep() {
digitalWrite(directionPin, HIGH);
for (int i=0; i<1000; i++) {
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
}
}
I'm not smart enough to figure out how to add one more condition to this code. I want for "while" to stop if endSwitch OR buttonLevel are pressed. I tried like this:
while ((digitalRead(endSwitch) == HIGH) && (digitalRead(buttonLevel) == HIGH))
But it just skips to "backStep()" instantly
If I understood right "&&" operator: it should run until one of buttons gets pushed?