Hello,
I'm trying to mess around with some code to learn how to do functions. Here I want to create two functions where:
function 1 (called "task1") monitors the state of a switch and stores it in a variable called "state".
function 2 (called "task2") reads "state" from function 1 and displays it only if "state" is 0.
This is my code:
byte SW1_GPIO = 22; //digital input (i.e. switch)
bool state; //state of the switch
//Task1 - Monitor switch state
bool Task1() {
state = digitalRead(SW1_GPIO); //read state of switch
return state;
}
//Task2 - print state to the serial port ONLY when the pushbutton(in Task 2) is pressed.
void Task2(state) {
if (state == 0) {
Serial.print(state);
}
else {
Serial.println("no button press");
}
}
void setup() {
Serial.begin(9600);
pinMode(SW1_GPIO, INPUT);
void loop() {
Task1();
Task2(state);
}
So I know if I want to pass a parameter I should include it in the function brackets, which I've done. I get the error "variable or field 'Task2' declared void", which I don't understand why because Task2 is not returning anything, that's why I put it as void. What's the right way to go about what I'm trying to do?
Thank you for your help ![]()