Hi,
What I'm trying to do is this : control the speed of a stepper with a potentiometer AND using a button to toggle between 5 steeping modes (full step, half step...).
My sketch to control the speed works perfectly, same for the one with the button to switch modes, but when I'm trying to merge them I can only control one function, depending on where I paste the code. And I can't understand why !
The code for the speed control with potentiometer :
// Defines pins numbers
const int stepPin = 9;
const int dirPin = 8;
int customDelay,customDelayMapped; // Defines variablesvoid setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);digitalWrite(dirPin,HIGH); //Enables the motor to move in a particular direction
}
void loop() {customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
// Function for reading the Potentiometer
int speedUp() {
int customDelay = analogRead(A0); // Reads the potentiometer
int newCustom = map(customDelay, 0, 1023, 300,4000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
return newCustom;
}
The code for switching between different stepping modes :
const int buttonPin = 7; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
int MS1 = 10;
int MS2 = 11;
int MS3 = 12;// Variables will change:
int buttonPushCounter = 1; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the buttonvoid setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
Serial.println("FULL STEP");
digitalWrite(MS1,LOW);
digitalWrite(MS2,LOW);
digitalWrite(MS3,LOW);
}void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
// Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button
// wend from on to off:
//Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounter > 5 ) {
buttonPushCounter = 0;
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
if (buttonPushCounter == 1 && buttonState == HIGH){
Serial.println("FULL STEP");
digitalWrite(MS1,LOW);
digitalWrite(MS2,LOW);
digitalWrite(MS3,LOW);}
if (buttonPushCounter == 2 && buttonState == HIGH){
Serial.println("HALF STEP");
digitalWrite(MS1,HIGH);
digitalWrite(MS2,LOW);
digitalWrite(MS3,LOW);}
if (buttonPushCounter == 3 && buttonState == HIGH){
Serial.println("1/4 STEP");
digitalWrite(MS1,LOW);
digitalWrite(MS2,HIGH);
digitalWrite(MS3,LOW);}
if (buttonPushCounter == 4 && buttonState == HIGH){
Serial.println("1/8 STEP");
digitalWrite(MS1,HIGH);
digitalWrite(MS2,HIGH);
digitalWrite(MS3,LOW);}
if (buttonPushCounter == 5 && buttonState == HIGH){
Serial.println("1/16 STEP");
digitalWrite(MS1,HIGH);
digitalWrite(MS2,HIGH);
digitalWrite(MS3,HIGH);}
delay(50);
}
My attempt to merge the sketches :
/* Simple Stepper Motor Control Exaple Code
*
- by Dejan Nedelkovski, www.HowToMechatronics.com
*/
// Defines pins numbers
const int stepPin = 9;
const int dirPin = 8;int customDelay,customDelayMapped; // Defines variables
const int buttonPin = 7; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
int MS1 = 10;
int MS2 = 11;
int MS3 = 12;// Variables will change:
int buttonPushCounter = 1; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the buttonvoid setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode (MS1,OUTPUT);
pinMode (MS2,OUTPUT);
pinMode (MS3,OUTPUT);digitalWrite(dirPin,HIGH); //Enables the motor to move in a particular direction
digitalWrite(MS1,LOW);
digitalWrite(MS2,LOW);
digitalWrite(MS3,LOW);// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
Serial.println("FULL STEP");
}
void loop() {
customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
// Function for reading the Potentiometer
int speedUp() {
int customDelay = analogRead(A0); // Reads the potentiometer
int newCustom = map(customDelay, 0, 1023, 25,4000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
return newCustom;// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
// Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button
// wend from on to off:
//Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;if (buttonPushCounter > 5 ) {
buttonPushCounter = 0;
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
if (buttonPushCounter == 1 && buttonState == HIGH){
Serial.println("FULL STEP");
digitalWrite(MS1,LOW);
digitalWrite(MS2,LOW);
digitalWrite(MS3,LOW);}
if (buttonPushCounter == 2 && buttonState == HIGH){
Serial.println("HALF STEP");
digitalWrite(MS1,HIGH);
digitalWrite(MS2,LOW);
digitalWrite(MS3,LOW);}
if (buttonPushCounter == 3 && buttonState == HIGH){
Serial.println("1/4 STEP");
digitalWrite(MS1,LOW);
digitalWrite(MS2,HIGH);
digitalWrite(MS3,LOW);}
if (buttonPushCounter == 4 && buttonState == HIGH){
Serial.println("1/8 STEP");
digitalWrite(MS1,HIGH);
digitalWrite(MS2,HIGH);
digitalWrite(MS3,LOW);}
if (buttonPushCounter == 5 && buttonState == HIGH){
Serial.println("1/16 STEP");
digitalWrite(MS1,HIGH);
digitalWrite(MS2,HIGH);
digitalWrite(MS3,HIGH);}
delay(50);
}
It looks like the second part of the code is ignored by the arduino, why ??
Thanks !