Need Help Start Button Help to lance Programme

the probleme i can’t configure my Start Button to lance my program …
I put all my call function to setup to make them just one time because in loop function they be repeted
So I need help to make the fuction called just one time in Loop and make my start Button work when he is pushed … This is my program:
#include <QTRSensors.h>
#define NUM_SENSORS 6 // number of sensors used
#define NUM_SAMPLES_PER_SENSOR 4 // average 4 analog samples per sensor reading
#define EMITTER_PIN 2 // emitter is controlled by digital pin 2

#define sagmotor1 9
#define sagmotor2 10
#define solmotor1 3
#define solmotor2 11
#define MZ80 5
#define startbuton 2

QTRSensorsAnalog qtra((unsigned char) {0, 1, 2, 3, 4, 5},
NUM_SENSORS, NUM_SAMPLES_PER_SENSOR, EMITTER_PIN);
unsigned int sensorValues[NUM_SENSORS];

unsigned long Last_Time = 0;
unsigned long Start = 0;
const int LED = 26;
int leftmotor, rightmotor, Integral = 0;
int error, last_error;
int Power_Motor;
int Position = 0;
int last_value = 0;
int Background = 1;
int i = 0;
void setup() {

pinMode(sagmotor1, OUTPUT);
pinMode(sagmotor2, OUTPUT);
pinMode(solmotor1, OUTPUT);
pinMode(solmotor2, OUTPUT);
pinMode(MZ80, INPUT_PULLUP);
pinMode(startbuton, INPUT_PULLUP);
delay(1000);
for (int i = 0; i < 150; i++)
{
if ( 0 <= i && i < 5 ) Motor(35,-35);
if ( 5 <= i && i < 15 ) Motor(-35,35);
if ( 15 <= i && i < 25 ) Motor(35,-35);
if ( 25 <= i && i < 35 ) Motor(-35,35);
if ( 35 <= i && i < 40 ) Motor(35,-35);
if ( 45 <= i && i < 55 ) Motor(-35,35);
if ( 55 <= i && i < 65 ) Motor(35,-35);
if ( 65 <= i && i < 75 ) Motor(-35,35);
if ( 75 <= i && i < 80 ) Motor(35,-35);

if ( i >= 80 ) {
Motor(0,0);
delay(3);
}

qtra.calibrate();
delay(1);
if (digitalRead(startbuton)==HIGH) { do frenle(); while(digitalRead(startbuton)==HIGH);} // He doesn’t work :frowning:

}
if (digitalRead(startbuton)==LOW) { Motor(0,0);}
if (digitalRead(startbuton)==HIGH)
{
TrackTime(60,0.05,0,0.8,1000);
TrackTime(40,0.05,0,0.8,1000);
}
void loop() {
Motor(0, 0);
}

Please post your code using code tags so it makes it easier for people to help you.

I think you want your code to wait for the start button to be pressed before it does anything.
A couple of things to note

  • your do…while() loop inside setup needs braces.
  • if your start button is INPUT_PULLUP, it reads LOW when it is pressed, not HIGH

try this untested code

#include <QTRSensors.h>
#define NUM_SENSORS             6  // number of sensors used
#define NUM_SAMPLES_PER_SENSOR  4  // average 4 analog samples per sensor reading
#define EMITTER_PIN             2  // emitter is controlled by digital pin 2


#define sagmotor1 9
#define sagmotor2 10
#define solmotor1 3
#define solmotor2 11
#define MZ80 5
#define startbuton 2

QTRSensorsAnalog qtra((unsigned char[]) {
  0, 1, 2, 3, 4, 5
},
NUM_SENSORS, NUM_SAMPLES_PER_SENSOR, EMITTER_PIN);
unsigned int sensorValues[NUM_SENSORS];

unsigned long Last_Time = 0;
unsigned long Start = 0;
const int LED = 26;
int leftmotor, rightmotor, Integral = 0;
int error, last_error;
int Power_Motor;
int Position = 0;
int last_value = 0;
int Background = 1;
int i = 0;
int lastButtonState;


void setup() {

  pinMode(sagmotor1, OUTPUT);
  pinMode(sagmotor2, OUTPUT);
  pinMode(solmotor1, OUTPUT);
  pinMode(solmotor2, OUTPUT);
  pinMode(MZ80, INPUT_PULLUP);
  pinMode(startbuton, INPUT_PULLUP);
  delay(1000);
  for (int i = 0; i < 150; i++)
  {
    if ( 0 <= i && i < 5   ) Motor(35, -35);
    if ( 5 <= i && i  < 15   ) Motor(-35, 35);
    if ( 15 <= i && i < 25   ) Motor(35, -35);
    if ( 25 <= i && i < 35   )  Motor(-35, 35);
    if ( 35 <= i && i < 40   )  Motor(35, -35);
    if ( 45 <= i && i < 55   )  Motor(-35, 35);
    if ( 55 <= i && i < 65   )  Motor(35, -35);
    if ( 65 <= i && i < 75   ) Motor(-35, 35);
    if ( 75 <= i && i < 80   )  Motor(35, -35);

    if ( i >= 80  )  {
      Motor(0, 0);
      delay(3);
    }

    qtra.calibrate();
    delay(1);
    if (digitalRead(startbuton) == HIGH) {
      do {
        frenle();   // He doesn't work :(
      } while (digitalRead(startbuton) == HIGH);
    }
  }
  lastButtonState = digitalRead(startbutton);

void loop() {
  int state = digitalRead(startbutton);

  if ( lastButtonState != state ) {
    // button changed state
    lastButtonState = state;

    if ( state == LOW ) {
      // button was pushed
      doButtonPushed();
    } else {
      // button was released
      doButtonReleased();
    }
  }
}

void doButtonPushed() {
    TrackTime(60, 0.05, 0, 0.8, 1000);
    TrackTime(40, 0.05, 0, 0.8, 1000);
}

void doButtonReleased() {
  Motor(0,0);
}

You SHOULD put do...while loop outside of the for loop. Currently you are putting while loop inside of the for loop.

Also, I recommend to print some log to Serial Monitor for debugging purpose.

I am begginer in arduino programme but i thank that there is a confusion between Emitter_pin he work with pin number 2 and the Startbuton he use pin number 2
what you think he doesn’t work the push buton

#include <QTRSensors.h>
#define NUM_SENSORS             6  // number of sensors used
#define NUM_SAMPLES_PER_SENSOR  4  // average 4 analog samples per sensor reading
#define EMITTER_PIN             2  // emitter is controlled by digital pin 2


#define sagmotor1 9
#define sagmotor2 10 
#define solmotor1 3
#define solmotor2 11
#define MZ80 5
#define startbuton 2

void loop() {

int state = digitalRead(startbuton);

  if ( lastButtonState != state ) {
    // button changed state
    lastButtonState = state;

    if ( state == LOW ) {
      // button was pushed
      doButtonPushed();
    } else {
      // button was released
      doButtonReleased();
    }
  }
  }
 void doButtonPushed() {
    TrackTime(60, 0.05, 0, 0.8, 1000);
    TrackTime(40, 0.05, 0, 0.8, 1000);
}

void doButtonReleased() {
  Motor(0,0);
}

I eliminate EMITTER_PIN now the start Buton work but i must push the Buton twice (2 times ) why ?!!
Help me to make it work just onetime just i push it one time work…

#include <QTRSensors.h>
#define NUM_SENSORS             6  // number of sensors used
#define NUM_SAMPLES_PER_SENSOR  4  // average 4 analog samples per sensor reading



#define sagmotor1 9
#define sagmotor2 10 
#define solmotor1 3
#define solmotor2 11
#define MZ80 5
#define startbuton 2



QTRSensorsAnalog qtra((unsigned char[]) {0, 1, 2, 3, 4, 5}, 
  NUM_SENSORS, NUM_SAMPLES_PER_SENSOR);
unsigned int sensorValues[NUM_SENSORS];
  1. You should NOT share a pin between EMITTER_PIN and startbuton pin.

  2. You should debounce for the button. See Button debounce tutorial

  3. I recommend to unify the coding style (variable naming style)