Hi Im am looking for a code and trying to put one together from others I have found (badly) as I am a total beginner in Arduino code.
I have made a camera slider and have a Nema 17 with both a4899 and tmc2208 drivers at my disposal with a nano which I have successfully got working with 2 different codes, 1 that controls the speed (allowing me to film slow and fast shots etc) and the second code to reverse the direction (so when my camera dolly gets to the end I can reverse it with a microswitch)
I would like to do both things at once but cant find a way to join the 2 code together (as I have no real knowledge on coding. I have searched the net and yet cant find code that will do it. I would be very grateful if anyone could help me with this.
Thanks in advance
Here are the 2 codes I have:-
Button code
// Stepper motor run code with a4988 driver
// by Superb
const int stepPin = 3;
int dirPin = 4;
int dirButton = 2;
int state = HIGH;
int reading;
int previous = LOW;
int stepDelay=1000;
int customDelay,customDelayMapped; // Defines variables
long time = 0;
long debounce = 200;
void setup() {
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(dirButton, INPUT_PULLUP);
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,8000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
return newCustom;
}
void loop() {
reading = digitalRead(dirButton);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
digitalWrite(dirPin, state);
previous = reading;
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay);
}
2 nd code
Pot code
// Stepper motor run code with a4988 driver
// by Superb
const int stepPin = 3;
const int dirPin = 4;
int customDelay,customDelayMapped; // Defines variables
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
digitalWrite(dirPin,HIGH); //change the rotation direction HIGH for clockwise and LOW for anticlockwise
}
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;
}