return state=S_second from my function

What is the equivalent of echo in Arduino? I want to return the case switch name S_second from my function. How do I "echo" this back to the case statements?

*some code in missing because it's not necessary to explaining the point of my question. - geez I suck at this sometimes...most of the time

const int S_first = 1;
const int S_second = 2;
int button = 2;

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:


  static int state = S_first;

  switch (state) {


    case S_first: {
        if (button == 1) {
          timerCheck();
        }

        break;
      }


    case S_second: {

        Serial.println("We made it to state: S_second")

        break;
      }

  }


  void timerCheck() {
    if (timerCheck == 1) {
      echo "state=S_second - how do I get this to switch the case?";
    }

  }

What is 'echo'?

Are you thinking serial.Print(state); or maybe just state = S_second ?

If you want to access 'state' out side of loop() make it global.

echo is php will return whatever is within the " "

So for instance it would be like > … if (timer ==1) {echo “state=S_second”}

What is the “equivalent” of this or other way to return the text back to the switch case?

Your code makes no sense.. Because it is partial and that is because the rest is your secret or because you do not think it makes any sense to us.. Well..

If you question is: How to give a "void" a return type, then look at this:

int voidWithResult() {
  return 123;
}

You seem to already know how Serial.print works and that is basically the equivalent of echo..

are you saying this?

int timerCheckt() {
return state=S_second;
}

This doesn't work because "state wan not declared in this scope"

Your code does not compile. Your curly braces don’t match, you miss a semicolon, etc.
button can never be one, because you assigned it a value of two.
You have to start the Serial connection. You have to enable the pull-up resistor if you want to use a button.

What are you trying to do?

Something like this?

Code:[hr][/td][/tr][tr][td][size=9pt][tt][color=#00979c]const[/color] [color=#00979c]uint8_t[/color] [color=#000000]S_first[/color] [color=#434f54]=[/color] [color=#000000]1[/color][color=#000000];[/color]
[color=#00979c]const[/color] [color=#00979c]uint8_t[/color] [color=#000000]S_second[/color] [color=#434f54]=[/color] [color=#000000]2[/color][color=#000000];[/color]

[color=#00979c]const[/color] [color=#00979c]uint8_t[/color] [color=#000000]buttonPin[/color] [color=#434f54]=[/color] [color=#000000]2[/color][color=#000000];[/color]

[color=#00979c]void[/color] [color=#5e6d03]setup[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color]
  [b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]begin[/color][color=#000000]([/color][color=#000000]115200[/color][color=#000000])[/color][color=#000000];[/color]
  [color=#d35400]pinMode[/color][color=#000000]([/color][color=#000000]buttonPin[/color][color=#434f54],[/color] [color=#00979c]INPUT_PULLUP[/color][color=#000000])[/color][color=#000000];[/color] [color=#434f54]// Enable the internal pull-up resistor[/color]
                                    [color=#434f54]// Connect the button between pin 2 and ground[/color]
[color=#000000]}[/color]

[color=#00979c]void[/color] [color=#5e6d03]loop[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color]
  [color=#00979c]static[/color] [color=#00979c]int[/color] [color=#000000]state[/color] [color=#434f54]=[/color] [color=#000000]S_first[/color][color=#000000];[/color]

  [color=#5e6d03]switch[/color] [color=#000000]([/color][color=#000000]state[/color][color=#000000])[/color] [color=#000000]{[/color]
    [color=#5e6d03]case[/color] [color=#000000]S_first[/color][color=#434f54]:[/color]
      [color=#5e6d03]if[/color] [color=#000000]([/color][color=#d35400]digitalRead[/color][color=#000000]([/color][color=#000000]buttonPin[/color][color=#000000])[/color] [color=#434f54]==[/color] [color=#000000]1[/color][color=#000000])[/color] [color=#000000]{[/color] [color=#434f54]// If the button is released[/color]
        [color=#5e6d03]if[/color] [color=#000000]([/color][color=#000000]timerCheck[/color][color=#000000]([/color][color=#000000])[/color][color=#000000])[/color] [color=#000000]{[/color]
          [color=#000000]state[/color] [color=#434f54]=[/color] [color=#000000]S_second[/color][color=#000000];[/color]
        [color=#000000]}[/color]
      [color=#000000]}[/color]
      [color=#5e6d03]break[/color][color=#000000];[/color]
    [color=#5e6d03]case[/color] [color=#000000]S_second[/color][color=#434f54]:[/color]
      [b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]println[/color][color=#000000]([/color][color=#005c5f]"We made it to state: S_second"[/color][color=#000000])[/color][color=#000000];[/color]
      [color=#5e6d03]break[/color][color=#000000];[/color]
  [color=#000000]}[/color]
[color=#000000]}[/color]

[color=#00979c]bool[/color] [color=#000000]timerCheck[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color]
  [color=#434f54]// Do your actual timer checking here, whatever it may be[/color]
  [color=#5e6d03]return[/color] [color=#00979c]true[/color] [color=#5e6d03]or[/color] [color=#00979c]false[/color][color=#000000];[/color]
[color=#000000]}[/color]

[/size]
Pieter

Nope.. But you almost had it! :wink:

int state = 0; //Global declaration, available to the entire sketch

void loop() {
  static int state = 0; //Local declaration, only available for "void loop()"
  int state = getState();
}

int getState() {
  return state;
}

The above code does not work because "state" is declared twice.

YIKES! wow....I dind't realize i had to be so literal....I know I'm missing buttonState=digitalRead..whatever
and probably missing some semis and some colons and possibly a banana or 2 but anyway....

All I'm asking is how do I get the words state=S_second back into my first case of S_first so I can switch out of it? The function is outside of the switch so state is flagged as outside of the scope.

Make state a global variable.

Holy Moses!...does it need to be at the beginning of my code? I had it at the very bottom of my code but still outside of loop. I just put it at top and it works. How is this? Am I in Nintendo vortex?

What you are asking is not making much sense. In the code you posted, "state" is declared locally whereas it is references globally - this does not work for what you are trying. Also, you cannot "nest" functions unless you are using lambdas (do not even bother...!) :wink:

Oh ...wait...nope....that didn't work. OK I think I get what you are saying so I have to declare state outside of the loop?

Hard to tell what's wrong without being able to see the code.. Whenever you use "{" you start a block and whenever you use "}" you end the started block. What you declare inside a block cannot be accessed outside it. Usually you would declare all global variables as the first thing in your sketch after includes.

What is timerCheck?
Explain clearly what you want your program to do.

Somehow being mad, having to spell out everything made it work. Yes, you were right, State just needed to be global. Here it is working.

Also, for anyone who does no php, is there a equivalent of echo for Arduino? Like return a string of text?

int button = 2;
int buttonState = 0;
const int S_startUp = 1;
const int S_second = 2;

static int state = S_startUp;

void setup() {
  Serial.begin(9600);
  pinMode (button, INPUT_PULLUP);

}

void loop() {

  switch (state) {

    case S_startUp: {


        buttonState = digitalRead(button);
        Serial.println("OK we are here now in S_startup");
        changeTheState();

        break;
      }


    case S_second: {

        Serial.println("OK we are here now in S_second");


        break;
      }


  }


}


void changeTheState() {
  if (buttonState == 0) {

    state = S_second;
  }

}

is there a equivalent of echo for Arduino? Like return a string of text?

Return it to where and where from ?

PHP's echo doesn't return anything. It adds text to the HTTP response.
I'd recommend reading a book on programming basics, and wowrking your way through the examples that come with the Arduino IDE.

You must explicitly declare a return type for a function in C, like so:

int returnInteger() {
  return 123;
}

float returnFloat() {
  return 123.0f;
}

char* returnString() {
  return "123";
}

It is possible to return mixed data types from the same method using pointers or auto but I would not advice a beginner to experiment with this :slight_smile: