How to return the value from void function and use it inside in another function or in the void loop?

How to return the value from a void function and use it inside in another function or in the void loop?

Goal: I want the variable get the data to from another function (byte selector) to another function (byte signalpwm_1). The result will be at serial which either (1 or 2 or 3) so than I can switch-case afterwards.

Problem: the outcome is shown as available. the output is quite strange:
000 - correct
010 - wrong should've = 012
100 - wrong should've = 101

code:

#define pwm_1a 5 // signal output 1a
#define pwm_1b 6 // signal output 1b
#define joystick_1 A0 // signal analog 1
#define signalpwm_1a 7 // signal digital 1a
#define signalpwm_1b 8 // signal digital 1b

byte valsignalpwm[5];
int valsignalpwm_1a;
int valsignalpwm_1b;
int i;

byte selector(int selector_a, int selector_b){

if((selector_a == 1) && (selector_b == 0)){
valsignalpwm[i] == 1;
if((selector_a == 0) && (selector_b == 1)){
valsignalpwm[i] == 2;
}
else{
valsignalpwm[i] == 0;
}
}
Serial.print(valsignalpwm[1]);
return valsignalpwm[i];
}

byte signalpwm_1(){
valsignalpwm_1a = digitalRead(signalpwm_1a);
valsignalpwm_1b = digitalRead(signalpwm_1b);
i = 1;

delay(100);
selector(valsignalpwm_1a, valsignalpwm_1b);
}

void serial(){
Serial.print(valsignalpwm_1a);
Serial.println(valsignalpwm_1b);

delay(500);
}

void setup() {
Serial.begin(9600);
pinMode(signalpwm_1a, INPUT);
pinMode(signalpwm_1b, INPUT);
pinMode(pwm_1a, OUTPUT);
pinMode(pwm_1b, OUTPUT);

}

void loop() {

signalpwm_1();
serial();

}Untitled

Maybe Serial.print(valsignalpwm[1]); should be Serial.print(valsignalpwm[i]);

1 Like

it does not work. Should i change the data type?

if a function is defined as void will return void type of data (let's say nothing).
If you need to return a value from a function you have to define it with the type of data you want to be returned

int mySum(int a, int b) {
  int res = a + b;
  return res;
}
1 Like

Please use code tags when posting code. It makes it more readable.

Why don't you pass the 'i' variable as a parameter to the selector function? This would be much clearer than a global with such a generic name.

valsignalpwm[i] == 1;

This statement is not assigning the variable the value 1, it is doing a compare and throwing away the result. Same for the other similar statements in the selector() function.

Serial.print(valsignalpwm[1]);
return valsignalpwm[i];

The .print should be for [i] not [1].

Also, you return the value but do nothing with it, is this deliberate?

1 Like

It is not yet clear to me what you want to do.

To explain it describe the functionality
there is a first function called selector
there is a second function called signalpwm_1

function selector uses two parameters
byte selector(int selector_a, int selector_b) {

depending on the two parameters the return-value shall be
1, 2 or 3

so what should happen with the return-value of your function selector?

please describe the functionality because your code does some nonsense.

if selector returns value 1 then .... .... should be ......
if selector returns value 2 then .... .... should be ......
if selector returns value 3 then .... .... should be ......

shortest way to create values 1, 2, 3 from two single bits beeing 0 or 1
is to bitshift left one bit and add the other bit

here is a democode with two - in general - useful functions that demonstrates that

void PrintFileNameDateTime() {
  Serial.println("Code running comes from file ");
  Serial.println(__FILE__);
  Serial.print("  compiled ");
  Serial.print(__DATE__);
  Serial.print(" ");
  Serial.println(__TIME__);  
}

// easy to use non-blocking timer-function
// which becomes true after EACH Timeperiod
// suitable for continuous On/Off-"blinking"
// not suitable for one-shot-timing
boolean TimePeriodIsOver (unsigned long &expireTime, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();  
  if ( currentMillis - expireTime >= TimePeriod )
  {
    expireTime = currentMillis; // set new expireTime
    return true;                // more time than TimePeriod) has elapsed since last time if-condition was true
  } 
  else return false;            // not expired
}

unsigned long MyTestTimer = 0;                   // variables MUST be of type unsigned long
// on ESP8266 and ESP32 onboard-LED is IO-pin 2 
// on Arduino Uno Onboard-LEDis IO-pin 13
const byte    OnBoard_LED = 2;  

void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
  static unsigned long MyBlinkTimer;
  pinMode(IO_Pin, OUTPUT);
  
  if ( TimePeriodIsOver(MyBlinkTimer,BlinkPeriod) ) {
    digitalWrite(IO_Pin,!digitalRead(IO_Pin) ); 
  }
}



void setup() {
  // make sure the baudrate in the serial-monitor 
  // is adjusted to 115200 baud
  Serial.begin(115200);
  delay(200); // short wait for serial to open on computer
  Serial.println("Setup-Start");
  PrintFileNameDateTime();

}

int MyVarA;
int MyVarB;
int MyOutput;

void loop() {
  // indicates loop is running
  BlinkHeartBeatLED(OnBoard_LED,250);
  
  if ( TimePeriodIsOver(MyTestTimer,1000) ) {
    MyVarA = 0;
    MyVarB = 0;
    MyOutput = (MyVarB << 1) + MyVarA;
    Serial.print("A=0, B=0 Out=");
    Serial.println(MyOutput);

    MyVarA = 1;
    MyVarB = 0;
    MyOutput = (MyVarB << 1) + MyVarA;
    Serial.print("A=1, B=0 Out=");
    Serial.println(MyOutput);

    MyVarA = 0;
    MyVarB = 1;
    MyOutput = (MyVarB << 1) + MyVarA;
    Serial.print("A=0, B=1 Out=");
    Serial.println(MyOutput);

    MyVarA = 1;
    MyVarB = 1;
    MyOutput = (MyVarB << 1) + MyVarA;
    Serial.print("A=1, B=1 Out=");
    Serial.println(MyOutput);
    Serial.println();    
    Serial.println();
  }  
}

best regards Stefan

1 Like

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