get confusion with While() function

I make communication between 2 arduino via Bluetooth.
My purpose is that when I click button1 in master, slaver will do the code in my purpose. The code will stop when I click the previous button1 again.
Note: Im using bluetooth to send data when the button is pushed
Number 1 is sent when I click the button.
Here my sending code

buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) 
    {
    Serial.print("<");
    Serial.print(1);
    Serial.print(">");
   
    digitalWrite(ledPin,HIGH);
    delay(200);
    digitalWrite(ledPin,LOW);
    }

Receiving code:

int integerFromPC4=0;
if( integerFromPC4==1)
{
 integerFromPC4=2  //   set integerFromPC4=2
 while(integerFromPC4==2)
{
   recordservo();
}

I thought that when I click the button again integerFromPC4 =1 and while() function will be stopped. Can any help me to solve my code.

int integerFromPC4=0;
if( integerFromPC4==1)
  {

This can never be executed because 0!=1.

On the receiving side write something like this:

initial action state = 0 // initialize state
.....
if(button push received )

toggle state // have only two states just switch it

if( state )
perform whatever action for state 1 - start etc.
else
perform action state 0 - stop etc.

Each time you detect button push just switch to the other state.

Can you give me an example of code It is so general
Sincere thanks

aarg:

int integerFromPC4=0;

if( integerFromPC4==1)
 {



This can never be executed because 0!=1.

so I just remove 0, then just write
int IntegerFromPC4;
And it still doesnt work

phamb587:
Can you give me an example of code It is so general
Sincere thanks

// global variables
boolean state = FALSE;
byte buttonPushed; this is generic variable - there is really no button , but whatever your BT code passed thru as "button has been pushed" on sending side.
....

in loop()
{
...
if(buttonPushed)) // button press detected
{
state != state; // toggle state
if(state) // state = true 1
{ // code block
digitalWrite(13,LOW) // send LOW to LED 13 as indicator of state 1
... // process anything else

}
else
{ // state = 0
digitalWrite(13,HIGH) // send HIGH to LED 13 as indicator of state 1
... // process anything else

}
}
.. rest of the loop code

Just a tip here. Vaclav writes in pseudo-code, to force you to work out what he really means.

digitalWrite(13,LOW)  // send LOW to LED 13 as indicator of state 1

In that line, for example, you need a semicolon after the closing bracket.

Your sender is messed up. Until you fix that, you are wasting your time working on the receiver.

Your sender needs to send a value when the switch BECOMES pressed, not when the switch IS pressed.

Look at the state change detection example.

Thanks for your notification

PaulS:
Your sender needs to send a value when the switch BECOMES pressed, not when the switch IS pressed.

I really dont get what you mean . Can you explain me clearly

He means you need to detect the transition from "not being pressed " to "being pressed", rather than the continuous state of " being pressed "

phamb587:
I really dont get what you mean . Can you explain me clearly

The loop() function is performed continuously. Anything you do there is repeated thousands of times a second. Do you want to send thousands of key press events per second? I didn't think so. So you send the first "becomes" one. Not the rest, where you already know the state.

Vaclav:
// global variables
boolean state = FALSE;
byte buttonPushed; this is generic variable - there is really no button , but whatever your BT code passed thru as "button has been pushed" on sending side.
....

in loop()
{
...
if(buttonPushed)) // button press detected
{
state != state; // toggle state
if(state) // state = true 1
{ // code block
digitalWrite(13,LOW) // send LOW to LED 13 as indicator of state 1
... // process anything else

}
else
{ // state = 0
digitalWrite(13,HIGH) // send HIGH to LED 13 as indicator of state 1
... // process anything else

}
}
.. rest of the loop code


thank for your code . I try and confirm you later if it works

to force you to work out what he really means.

Nope, Vaclav just do not believe in mindless "cut and paste " of code in quotation brackets.

And he is happy to put Nick back on "thunder" list for being so supportive in such nice way.

to force you to work out what he really means.

Which, in itself, can be a major challenge.

Vaclav just do not

sp. " Vaclav just does not"

Vaclav:
Nope, Vaclav just do not believe in mindless "cut and paste " of code in quotation brackets.

Perhaps, but when beginners come here for advice, posting as "code" something that doesn't work isn't all that helpful.

That's like saying put lots of salt into your chocolate cake in a cooking forum (when you really mean sugar) but you don't want cooks to mindlessly "cut and paste" your advice.