Do something if a variable keeps changing

Hello guys,
I have a variable A which is constantly changing between 1 and 0. I want to output something over serial (e.g. "power") as long as it is changing, but if it isnt changing anymore and is staying at 0 then I want something else to be sent (e.g. "stop").

I tried it with if and else but the problem then is I would of course recieve power...stop...power...stop...power..... (but instead I want power...power...power...power... and only if A is staying 0 I want to recieve stop...stop....).

What would be the best method to do this?

Who is changing that variable and how fast ?

You can not solve this with 'if' and 'else'.

In such a situation, there is often a timer. When the signal changes, then the timer is restarted. When the timer reaches a certain time (that means it was not restarted for some time), then a message is send.

What would be the best method to do this?

Look at the StateChangeDetection example in the IDE

You need to detect when the input becomes HIGH or LOW

I want to output something over serial (e.g. "power") as long as it is changing, but if it isn't changing any more.

You need to define 'changing'. If it changes once per minute is that 'changing'? Once per hour? Once per week? Until you know you can't distinguish 'changing' from 'not changing'.

Sorry for the missing information. I have a digital PAS (pedal assist sensor) which basicaly only is a digital hall sensor I guess. So if I am pedaling the sensor outputs a square wave (on, 1 and off, 0) compareable to a pwm signal I would say, If I am not pedaling it outputs 0 (off, low) all the time. So now I want to output "power" over serial if I am pedaling and "stop" if I am not pedaling.
Thank you for the hint to the StateChangeDetection this almost fullfilled my needs. I changed the code a bit

const int  buttonPin = 2;    // the pin that the pushbutton is attached to

int buttonPushCounter = 0; 
int buttonPushCounter1 = 0;// counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  /*Serial.print("counter0:");
  Serial.println(buttonPushCounter);
  Serial.print("counter1:");
  Serial.println(buttonPushCounter1);
  */// compare the buttonState to its previous state

  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
    if (buttonPushCounter != buttonPushCounter1){
      Serial.println("power");  
      buttonPushCounter1++;
      };
      

    delay(50);
  };


};

  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
  
}

and now I recieve "power" if the signal is applied, so that is working. What is not working is if I stop the signal it isnt showing "stop", but instead it stops outputting new "power". I tried it with an else statement after the last if clause and a few other things but I cant get it to work, because then I would recieve "power" ... "stop" ... "power" ... "stop" ... again. What would also be fine is if it counts for like 1second and if the state didnt change during that time it would output "stop".

I hope this was understandable.

There is nothing in your code the measures the time of the changes in state, so it will not work. Knowing the slowest rate of change that counts as changing is essential to solving this. I am guessing that maybe anything faster than once per half second is probably about right, at least as a starting point. You need to measure the time interval between changes and decide if the interval is more or less than half a second and print accordingly.

If I am not pedalling it outputs 0 (off, low) all the time.

I could be wrong but I suspect that's not true. I suspect that if you turn the pedals slowly you will find a spot where they can be stopped and output high.

At this point:

if (buttonState != lastButtonState)

... it doesn't know if the change is up or down, it's only just after that where it asks if it's high that it knows.

So I'd use that line as my trigger, then it doesn't matter if you're right about it always outputting 0 when you're not pedalling or if PerryBebbington's suspicion that it could be a 1 is correct; you're covered either way.

At that point where it changes (in either direction) capture millis() into a variable, and then continually compare the ever-increasing millis() to that stored value. You do need to have a time interval in mind as PerryBebbington says, up to which a change might be on the way and you just don't know it yet, and after which you are deemed to be not pedalling. (Put that interval into a variable too, then you can easily find it to change it as you refine it.)

This code tells you if the pin last changed (in either direction) more than "timeout" ago:

/*
  BASED ON State change detection (edge detection)
*/

// this constant won't change:
const int  buttonPin = 8;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonState;         // current state of the button
int lastButtonState;     // previous state of the button
bool ledState;
unsigned long pinChangedAt;
int timeout = 5000;
bool pinChanged = false; 

void setup()
{
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT_PULLUP);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  buttonState = digitalRead(buttonPin);
  lastButtonState = buttonState;
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState)
  {
    //we don't care which direction it changed
    ledState = !ledState; //toggle the led with each change as visual confirmation
    digitalWrite(ledPin, ledState);
    pinChangedAt = millis();
    pinChanged = true;
    Serial.print("button changed at "); Serial.println(pinChangedAt);
    // Delay a little bit to avoid bouncing
    delay(50);
  }//changed
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;

  if (millis() - pinChangedAt >= (unsigned long) timeout && pinChanged)
  {
    pinChanged = false;
    Serial.println("   *** pin changed too long ago ***");
  }
}//loop