Hi
I recently asked for help and advice on controlling a stepper motor's rotation with a thumb joystick and thanks to forum users "noisyvoid" and "ash901226" I got enough help to setup my project successfully. However, I now want to enclose the project and bought an arcade joystick to replace the thumb joystick. When I got the joystick I saw that it was not a potentiometer joystick, but a relay switch joystick instead. Since I'm still learning to get the hang of things, I'm not sure how the code will have to affected in order for the joystick to work with my current code...
Here is the code:
#include <Stepper.h>
#include <Servo.h>
const int num_step = 50; // motor steps per revolution
const int pwm_cha = 3;
const int pwm_chb = 11;
const int dir_cha = 12;
const int dir_chb = 13;
const int brake_cha = 9;
const int brake_chb = 8;
int Pot1 = 2;
int val;
Servo myservo; // create servo object to control a servo
Stepper stepper(num_step, dir_cha, dir_chb);
void setup()
{
Serial.begin(9600);
pinMode(pwm_cha, OUTPUT);
pinMode(pwm_chb, OUTPUT);
pinMode(brake_cha, OUTPUT);
pinMode(brake_chb, OUTPUT);
digitalWrite(pwm_cha, HIGH);
digitalWrite(pwm_chb, HIGH);
digitalWrite(brake_cha, LOW);
digitalWrite(brake_chb, LOW);
myservo.attach(2);
}
void loop()
{
int PVal=analogRead(Pot1);
if (PVal >= 507 || PVal <= 517)
{ // fill in with the one you need to make it brake
}
if (PVal > 517 )
{
int K= map (PVal, 518,1023,0,400); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/
int S=map (K, 0,400,0,num_step);
stepper.setSpeed(K); //this set the speed of the stepper motor
stepper.step(S); //this set the direction and the number of step it would take
Serial.print("Forward, K=");
Serial.print(K);
Serial.print(" PVAL=");
Serial.println(PVal);
}
if (PVal < 507 )
{
int K= map (PVal, 0,506,400,0); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/
int S=map (K, 0,400,0,num_step);
stepper.setSpeed(K); //this set the speed of the stepper motor
stepper.step(-S); //this set the direction and the number of step it would take
Serial.print("Backward, K=");
Serial.print(K);
Serial.print(" PVAL=");
Serial.println(PVal);
}
// This next bit just outputs the pot position to a servo
val = map(PVal, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15);
}
I need to know what the new code will look like using the relay switches. The above code worked on a mapping for the thumb joystick and as the joystick moved further away from the center pin, the stepper motor rotated faster and the other way around. Now, with the new relay switch joystick, I'm not sure if this function can still be the same since it is only a switch. So now the code must be amended in such a way that when a relay switch is pressed with the joystick, the motor must rotate in one direction and when the joystick is not pressed and goed back to its default state (center) the motor must stand still. This however will affect the way in which I control the speed since I'll have to use a normal pot to set the speed and then use that as the speed for the motor.
Attached are two pictures of the joystick that I'll be using. The relay at the bottom is just for the switch button on top of the joystick. I don't think I'll be using it.
I also need to know how to physically connect the wires from the joystick to the Arduino.
I hope this makes enough sense for someone who can help me. Like I said, code wise I'm still very dum and learning.
Regards
TheBlommie