What's wrong with my code?

So I’m trying to figure out the code for a class assignment. The idea is to use an arduino and breadboard to do the following:

  • a push button to act as a on/off switch. must be on before anything happens. (works)

  • a potentiometer that will control the speed of a connect step motor (works)

  • a second push button that will turn an led on when pressed. the code needs to count each time the button is pressed, increasing the count by one from 0-9. when the button is pressed a 9th time, everything should revert back to the off state. (this is where I’m having problems with my code)

So pretty much everything works but that last part. The problem is that when I press the first push button to turn it on, the led starts flashing and the count starts by itself without me pressing the second button. It flashes 9 times and then goes into the off state. I’m not sure why. Any help is appreciated. Thanks.

#include <Stepper.h>

int count = 0; // counter for button presses/sensor
int pb_pin = 8; // button to turn power on; pin input
bool power = false; // variable to store read value
bool sensor = false; // variable to store read sensor button
int sb_pin; // sensor button acts as if something passes through when pressed; pin input
int led_pin = 10; // pin connected to led; output pin

int potentiometer = 0;

const int stepsPerRevolution = 1500; // value to fit number per revs

// intialize stepper library on pins 4, 5, 6, 7
Stepper myStepper(stepsPerRevolution, 4, 5, 6, 7);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(pb_pin, INPUT);
  pinMode(sb_pin, INPUT);
  pinMode(led_pin, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  sensor = false; 
  while (power == false) {
    if (digitalRead(pb_pin) == HIGH) {
      power = true;
    }
  }
  if (power == true) {
    while (count < 9) {
      //set the speed potentiometer
      potentiometer = analogRead(A0);
      int motorspeed = map(potentiometer, 0, 1023, 0, 100);
      if (potentiometer > 0) {
        myStepper.setSpeed(potentiometer);
        myStepper.step(stepsPerRevolution / 100);
      }

      if (sensor == false) {
        if (digitalRead(sb_pin) == HIGH) {
          sensor = true;
        }
      }

      else if (sensor == true){
          count = count + 1;
        digitalWrite (led_pin, HIGH);
        delay(1000);
        digitalWrite (led_pin, LOW);
        delay(1000);
        sensor = false;
      }

      else if (count == 9) {
        myStepper.setSpeed(0);
        sensor = false;
        power = false;
        count = 0;
        potentiometer = 0;
        break;
      }
    }
  }
}

Don’t do it like this in your loop:

  while (power == false) {
    if (digitalRead(pb_pin) == HIGH) {
      power = true;
    }
  }

Simply use an if statement :

if ( power == false && digitalRead(pb_pin) == HIGH ) power = true ;

if ( power == true ) { 
// your main code here 
}

Then after 9 presses or so, you can simply set power = false again.