Hi guys, im new here and with arduino stuff i need some help.
I've been into a project making a camera slider
its running great with the joystick but i want to change the motor step resolution with a simple button with 3 cases like 1-full step 2- half step 3-quarter step.
im using arduino uno with the drv8825
my code is a bit mess and i have no clue what im doing
This slider runs from side to side with a slide pot controlling it.
It goes faster the further i push the slider pot.
There are limit switches on both ends to prevent further traveling of the sliding plate.
I think its not possible to have delay on loop because of convertAnalogToSpeed part, please correct me.
#include "stepLib.h";
#include "smoothJoystick.h";
//not in use #define LEFT_PIN 4
//not in use #define STOP_PIN 5
//not in use #define RIGHT_PIN 6
#define Limit01 2
#define Limit02 3
#define joystickPin A1 // analog pin A1 (27)
int msstep = 7; // Pin 7 pushbutton
int MS0 = 10; // Pin 10 connected to MS0 pin
int MS1 = 11; // Pin 11 connected to MS1 pin
int MS2 = 12; // pin 12 conected to MS2 pin
int val;
int buttonState =0;
int stepmode = 0;
#define maxSpeed 4000 // speed measured in Hz
#define minSpeed 1 // speed measured in Hz
#define deadband 50 // deadband for consideration of 0 speed
// define our step pins
# define sliderStep 9 // 9
// define our direction pins
# define sliderDir 8 // 8
// instantiate out objects
stepMotor slider(sliderStep, sliderDir); // stepper motor
joystick joy(joystickPin, 512); // declare a new instance of a joystick on our joystickPin, let's initialize our buffer to center joystick value: 512
void setup() {
Serial.begin(115200);
//not in use pinMode(LEFT_PIN, INPUT_PULLUP);
//not in use pinMode(STOP_PIN, INPUT_PULLUP);
//not in use pinMode(RIGHT_PIN, INPUT_PULLUP);
pinMode(msstep, INPUT_PULLUP);
pinMode(Limit01, INPUT);
pinMode(Limit02, INPUT);
pinMode(sliderDir, OUTPUT);
pinMode(MS1, OUTPUT);
pinMode(MS2, OUTPUT);
}
void loop() {
int buttonState = digitalRead(msstep);
val = digitalRead(msstep);
if (val == HIGH) {
if (val != buttonState) { // the button state has changed!
if (val == HIGH) { // check if the button is pressed
if (stepmode == 0) { // if its off
stepmode = 1; // microstep full step!
} else {
if (stepmode == 1) { // if its all-on
stepmode = 2; // microstep 1/8 step!
} else {
if (stepmode == 2) { // if its blinking
stepmode = 0; // microstep 1/16!
}
}
}
}
}
buttonState = val;
}
if (stepmode == 0) {
digitalWrite (MS0, LOW);
digitalWrite (MS1, LOW);
digitalWrite (MS2, LOW);
}
if (stepmode == 1) {
digitalWrite (MS0, HIGH);
digitalWrite (MS1, HIGH);
digitalWrite (MS2, LOW);
}
if (stepmode == 2) {
digitalWrite (MS0, LOW);
digitalWrite (MS1, LOW);
digitalWrite (MS2, HIGH);
}
unsigned int smoothAnalogVal = joy.smoothRead(); // get our smoothed analog value
signed int stepperSpeed = convertAnalogToSpeed(smoothAnalogVal); // convert our analog value to a stepper speed with direction
Serial.println(stepperSpeed);
slider.step(stepperSpeed); // request a step with our joystick driven speed
}
// this function takes as an input, an unsigned analog value and will convert it into a signed value which represents stepper speed
// the output is bounded by a min and max stepper speed
signed int convertAnalogToSpeed(unsigned int analogVal) {
if (analogVal < 512 + deadband && analogVal > 512 - deadband) { // we are within deadband, return a 0 speed
return 0;
}
if (analogVal >= 512 + deadband) { // move in forward direction - return a positive speed. joyLeft
if (!digitalRead(Limit01)) {
return 0; // check if limit switch is activated
}
else {
return map(analogVal, 512 + deadband, 1024, minSpeed, maxSpeed); // interpolate our joystick value to the proper speed
}
}
if (analogVal <= 512 + deadband) { // move in reverse direction - return a negative speed. joyRight
if (!digitalRead(Limit02)) {
return 0; // check if limit switch is activated
}
else {
return -map(analogVal, 512 - deadband, 0, minSpeed, maxSpeed); // interpolate our joystick value to the proper speed
}
}
}
I've been over this for days, im preety sure its very simple but i just cant get it.
Im like ZERO experience with coding arduino.
If someone could help i would apreciate. Thanks