Hello, my name is José and I´m fascinated with arduino.
I´m doing a little project with a joystick, I´m triying to increment and decrement a variable till it goes to a limit (I choose the limit obviusly :D) but I´ve tried several times with differents codes but still I don´t get the answer could you help me please.
That is the code i´m using right now:
byte yPin = A1; //VRY
int minima=400;
int maxima=1800;
int velocidad=minima;
void setup() {
pinMode(yPin, INPUT);
Serial.begin(9600);
}
void loop() {
velocidad=calcular_velocidad();
Serial.println(velocidad);
delay(200);
}
int calcular_velocidad(){
do{
if(analogRead(yPin)>=1022){
velocidad=velocidad+100;
return(velocidad);
}
if (analogRead(yPin)<=2){
velocidad=velocidad-100;
return (velocidad);
}
}while ((velocidad>minima)and(velocidad<maxima));
if(velocidad==minima){
return (minima);
}
if(velocidad==maxima){
return (maxima);
}
}
The problem is that variable "velocidad" always exceeds the limits, please if somebody can help I´ll appreciate a lot.
I think you need to reorganize your calcular_velocidad() function so that it only has one return. The way you have it the first return means that none of the rest of the function is used
Hi guys thanks for the quick answer, the action to do with the joystick is when I press the joystick up it would have to increase of 100 numbers until I reach the limit or release the it... the opposite when I press down.
jotaPedro:
press up when the lever go forward and press down when the lever go backward.
Define "forward" and "backward". Is it a digital or analog stick? Do you want it to increase 100 only once for each "forward/backward" or increase continuously at some fixed rate?