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;
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;
}
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.
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();
}
}