Answer the question
In order to leave comments, you need to log in
How to expand data in Arduino?
Perhaps the question is not correctly posed, but the situation is such.
I play with a PS2 joystick and get data from the knobs
ps2x.Analog(PSS_LX);
from 127 to 0 this is the lever tilted to the left, and 0 is the maximum tilted
128 is the rest figure
from 129 to 255 tilted to the right, 255 is
the
maximum up to 4095 or in another version from 0 to 255
Now I do this
//----------Мотор 5, захват----------------------------
if (analogPSS_RX < 120 && analogPSS_RX != 0) { //Движение в верх
drive5Up = 1;
} else {
drive5Up = 0;
}
if (analogPSS_RX > 150) { //Движение в низ
drive5Down = 1;
} else {
drive5Down = 0;
}
if (drive5Up == 1 && drive5Down == 0) {
MSS.analogWrite(14, 2047); // Устанавливаем на 0 выводе сигнал ШИМ с коэффициентом заполнения 1023 (25%), допускаются значения от 0 до 4095
MSS.analogWrite(15, 0); // Устанавливаем на 1 выводе сигнал ШИМ с коэффициентом заполнения 2047 (50%), допускаются значения от 0 до 4095
}
if (drive5Down == 1 && drive5Up == 0) {
MSS.analogWrite(14, 0); // Устанавливаем на 0 выводе сигнал ШИМ с коэффициентом заполнения 1023 (25%), допускаются значения от 0 до 4095
MSS.analogWrite(15, 2047); // Устанавливаем на 1 выводе сигнал ШИМ с коэффициентом заполнения 2047 (50%), допускаются значения от 0 до 4095
}
Answer the question
In order to leave comments, you need to log in
expand
// LEFT --> [127 ... 0] --> [32 ... 4095]
//
// RIGHT --> [129 ... 255] --> [32 ... 4095]
int calc_speed(int value)
{
int speed = 0; // для наглядности без ? :
if(value > 128)
{
speed = 4095 / 127 * (value - 128);
}
else
{
speed = 4095 / 127 * (128 - value);
}
return speed;
}
void drive_forward(int speed)
{
MSS.analogWrite(15, 0);
MSS.analogWrite(14, speed);
}
void drive_backward(int speed)
{
MSS.analogWrite(14, 0);
MSS.analogWrite(15, speed);
}
void stop()
{
MSS.analogWrite(14, 0);
MSS.analogWrite(15, 0);
}
void move()
{
int ds = ps2x.Analog(PSS_LX);
if(ds > 128)
{
drive_forward(calc_speed(ds));
}
else if(ds < 128)
{
drive_backward(calc_speed(ds));
}
else
{
stop();
}
}
If it is quite primitive map to help you. If it is real, then the engine speed is seriously dependent on the load and will not be proportional to the PWM. Those. if PWM 100 is increased to 200, this does not mean at all that the speed will double. If the load is constant, then you can write a table of PWM values for each joystick value (or not for each, but calculate intermediate ones)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question