I wrote a code in which a switch case is calling a function, that receives the value of the joystick. For example for case 1, I want the x-value of the joystick to be 1023 and y-value to be 0.
However, it kind of does not give me the values I inputted to the functions but gives me 1023 for both x and y and I don't know why.
Can somebody help me please?
#include <math.h>
const int Joystick_X = A0;
const int Joystick_Y = A1;
const int pushbutton = 3;
int JoystickValue_X = 0;
int JoystickValue_Y = 0;
int pushbuttonState;
int JoystickValue_X_Y;
//Define experiment (0 = manual, 1 = x is 1023 and y is 0)
int experiment = 1;
//Define functions--------------------------------------------------------------------
int manual_function() {
JoystickValue_X = analogRead (Joystick_X); // [0, 1023]
JoystickValue_Y = analogRead (Joystick_Y); // [0, 1023]
return JoystickValue_X;
return JoystickValue_Y;
}
int case_function( int JoystickInput_X, int JoystickInput_Y) {
JoystickValue_X = JoystickInput_X;
JoystickValue_Y = JoystickInput_Y;
return JoystickValue_X;
return JoystickValue_Y;
}
//---------------------------------------------------------------------------------
void setup() {
pinMode(Joystick_X, INPUT); // X-axis
pinMode(Joystick_Y, INPUT); // Y-axis
pinMode(pushbutton,INPUT); // press button
digitalWrite(pushbutton, INPUT_PULLUP);
pushbuttonState = digitalRead(pushbutton);
Serial.begin(9600);
while (digitalRead(pushbutton) == HIGH);
}
void loop() {
switch(experiment) {
case 0:
JoystickValue_X_Y = manual_function();
Serial.print(JoystickValue_X);
Serial.print(";");
Serial.println(JoystickValue_Y);
case 1:
JoystickValue_X = case_function(1023, 0);
JoystickValue_Y = case_function(1023, 0);
Serial.print(JoystickValue_X);
Serial.print(";");
Serial.println(JoystickValue_Y);
}
}
you can't return 2 values like this. the function ends after the first return
you could return a structure with the 2 values or not return anything as you are modifying global variables
Actually I do have another question/ problem and I'd be thankful if you could help me again.
I want the function to have a if-else statement, that works like:
int case_function( int JoystickInput_X, int JoystickInput_Y) {
if (currentTime < 45000) {
JoystickValue_X = JoystickInput_X;
JoystickValue_Y = JoystickInput_Y;
}
else {
JoystickValue_X = 0;
JoystickValue_Y = 0;
}
}
So the JoystickValue should change according how much time has passed.
But now I get (1023,0); (1023, 1023) alternatingly as data and I don't really know why..
Sorry fot confusing, I actually have a longer code and this is just a snippet as I am trying to focus just on this the problem in that part of the code
#include <math.h>
const int Joystick_X = A0;
const int Joystick_Y = A1;
const int pushbutton = 3;
int JoystickValue_X = 0;
int JoystickValue_Y = 0;
unsigned long startTime;
unsigned currentTime;
//Define experiment (0 = manual, 1 = x is 1023 and y is 0)
enum {MANUAL, FIXED} experiment = FIXED;
//---------------------------------------------------------------------------------
void manual_function() {
JoystickValue_X = analogRead (Joystick_X); // [0, 1023]
JoystickValue_Y = analogRead (Joystick_Y); // [0, 1023]
}
void case_function( int JoystickInput_X, int JoystickInput_Y) {
if (currentTime < 45000) {
JoystickValue_X = JoystickInput_X;
JoystickValue_Y = JoystickInput_Y;
}
else {
JoystickValue_X = 0;
JoystickValue_Y = 0;
}
}
//---------------------------------------------------------------------------------
void setup() {
startTime = millis(); //internal timer
pinMode(Joystick_X, INPUT); // X-axis
pinMode(Joystick_Y, INPUT); // Y-axis
pinMode(pushbutton, INPUT); // press button
digitalWrite(pushbutton, INPUT_PULLUP);
Serial.begin(9600);
while (digitalRead(pushbutton) == HIGH);
}
void loop() {
switch(experiment) {
case MANUAL:
currentTime = millis();
manual_function();
Serial.print(JoystickValue_X);
Serial.print(";");
Serial.print(JoystickValue_Y);
break;
}
case FIXED:
currentTime = millis();
case_function(1023, 0);
Serial.print(JoystickValue_X);
Serial.print(";");
Serial.print(JoystickValue_Y);
break;
}