Function in switch case doesn't get values as given

Hi!

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

1 Like

ooh okay! And how would that work with the strucuture?

something like this as it's all global variables. You don't need the structure

#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;

//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) {
  JoystickValue_X = JoystickInput_X;
  JoystickValue_Y = JoystickInput_Y;
}
//---------------------------------------------------------------------------------

void setup() {

  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);

  switch (experiment) {
    case MANUAL:
      manual_function();
      break; // <<=== DON'T FORGET THE BREAK

    case FIXED:
      case_function(1023, 0);
      break;; // <<=== DON'T FORGET THE BREAK
  }
  // print the values
  Serial.print(JoystickValue_X);
  Serial.print(";");
  Serial.println(JoystickValue_Y);
}

void loop() {}

(typed here, mind the typos)

1 Like

thank you so much, it worked! :))

:slight_smile: glad it helped

study the code to make sure you understand

1 Like

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..

case 11:
          currentTime = millis();    
          case_function(1023, 0); 
          Serial.print(JoystickValue_X);
          Serial.print(";");
          Serial.print(JoystickValue_Y);
      
          break;
          }

I should add that the switch case is inside the void loop()

I had it in the setup only for demonstration

Try with the code in the loop and post the whole code

I have put it inside the loop already and without the if-else statement it still works like you said.

#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;

//Define experiment (0 = manual, 1 = x is 1023 and y is 0)
int 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() {

  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 0:
          currentTime = millis();    
          manual_function();
          Serial.print(JoystickValue_X);
          Serial.print(";");
          Serial.print(JoystickValue_Y);
      
          break;
          }

case 11:
          currentTime = millis();    
          case_function(1023, 0); 
          Serial.print(JoystickValue_X);
          Serial.print(";");
          Serial.print(JoystickValue_Y);
      
          break;
          }

You removed the enum?

oh I did not, its just I forgot to type it in the post I uploaded.

Post the real code :wink:

Does it even compile?
Where is

Defined?

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;
          }

can't work with snippets - sorry. Loosing my time

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.