Hi! I'm really new to Arduino and robotics.
I've been looking at code online, and am wondering why people convert form analogRead to voltage. I think this is what's going on in my project?
I've been dumped on a project where I'm trying to control a flexible robot arm with two segments. There are two joysticks, 8 stepper motors, an easy driver. I'm a bit confused with how its working...I added comments that give my thoughts, but please help me understand how this robot arm is being controlled!
If people can also give explanation to some of my comments in the other functions that'd also be extremely helpful
Below is the code for controlling one segment of the robot....
//VARIABLES, some are missing here but they're all in the code.
#define X1 A0 // Pin A0 connected to Joystick 1 x-axis
#define Y1 A1 // Pin A1 connected to Joystick 1 y-axis
#define button_1 38 // Pin 38 connected to Joystick Button 1
#define X2 A3 // Pin A2 connected to Joystick 2 x-axis
#define Y2 A2 // Pin A3 connected to Joystick 2 y-axis
#define button_2 39 // Pin 39 connected to Joystick Button 2
//rInput is analogRead from joysticks - 512 to center it
scale(double rInput)
{
double scaled = 0.0
if (rInput < -64.0 || rInput > 64.0 && abs(rInput) <= 512.0) //Not sure where 64 comes from!!!!
scaled = rInput / 512.0; //Why do this?
else if (rInput > 512.0) //when would this happen?
scaled = 1;
else if (rInput < -512.0) //when would this happen?
scaled = -1;
}
//to set the speed and direction based on joystick position
double set(double position, int dir1, int dir2, int maxS)
{
double speed;
if (position < -0.05){ //WHY DO THIS?
digitalWrite(dir_1, LOW);
digitalWrite(dir_2, HIGH); //X- or Y- on easy board
forward = true;
speed = -position * 8; //why do negative position * 8??? Where does 8 come in?
}
else if (position >= -0.05 && position <= 0.05)
speed = 0;
else if (position > 0.05){
digitalWrite(dir_1, HIGH); //X+ or Y+ on easy board
digitalWrite(dir_2, LOW);
forward = false;
speed = position * 8;
}
return speed;
}
void setBottomSpeed(double posX1, posY1) {
speedX1 = set(posX1, dir1, dir2, maxS); //dir pins connect to easy driver, are output pins
forwardX1 = false;
if (speedX1 == 0.0)
nX1 = 0; //var for timing steps
else
nX1 = (int) (9 - ceil(speedX1)); //why 9?? why Do this at all? What does this mean?
speedY1 = set(posY1, dir3, dir4, 8); //set speed and direction of joystick
forwardY1 = true;
if (speedY1 == 0.0)
nY1 = 0;
else
nY1 = (int) (9 - ceil(speedY1));
....
loop() {
if(joysticks have been pressed)
setbottomSpeed(scale(analogRead(X1) - 512.0), scale(analogRead(Y1) - 512.0));
.....
}