need help with pushbutton

okay i’m pretty new at programming so i need help if you want to help if I like that very much
i would like to start a script and stop with a 1 pushbutton without resistor the script i want to start is
(and its not my code)


const int frontEchoPin = 7;
const int frontTriggerPin = 6;
const int leftEchoPin = 11;
const int leftTriggerPin = 10;
const int rightEchoPin = 9;
const int rightTriggerPin = 8;
const int motorL1 = 2;
const int motorL2 = 3;
const int motorR1 = 4;
const int motorR2 = 5;
volatile float maxFrontDistance = 25.00;
volatile float frontDuration, frontDistanceCm, leftDuration, leftDistanceCm, rightDuration, rightDistanceCm;
volatile float maxLeftDistance, maxRightDistance = 20.00;
void setup() {
// serial
Serial.begin(9600);
// ultrasonic
pinMode(frontTriggerPin, OUTPUT);
pinMode(frontEchoPin, INPUT);
pinMode(leftTriggerPin, OUTPUT);
pinMode(leftEchoPin, INPUT);
pinMode(rightTriggerPin, OUTPUT);
pinMode(rightEchoPin, INPUT);
// motors
pinMode(motorL1, OUTPUT);
pinMode(motorL2, OUTPUT);
pinMode(motorR1, OUTPUT);
pinMode(motorR2, OUTPUT);
}
void loop() {
// front distance check
checkFrontDistance();
if (frontDistanceCm < maxFrontDistance) {
Serial.println(“Too close”);
checkLeftDistance();
delay(20);
checkRightDistance();
delay(20);
if (leftDistanceCm < rightDistanceCm)
moveRight();
else if (leftDistanceCm > rightDistanceCm) {
moveLeft();
}
}
else {
Serial.println(“OK”);
moveForward();
}
// left distance check
checkLeftDistance();
if (leftDistanceCm < maxLeftDistance) {
Serial.println(“Left too close”);
delay(20);
checkLeftDistance();
delay(20);
checkRightDistance();
delay(20);
if (leftDistanceCm > rightDistanceCm)
moveForward();
else if (leftDistanceCm < rightDistanceCm) {
moveRight();
}
}
// right distance check
checkRightDistance();
if (rightDistanceCm < maxRightDistance) {
Serial.println(“Right too close”);
delay(20);
checkRightDistance();
delay(20);
checkLeftDistance();
delay(20);
if (rightDistanceCm > leftDistanceCm)
moveForward();
else if (rightDistanceCm < leftDistanceCm) {
moveLeft();
}
}
}
void checkFrontDistance() {
digitalWrite(frontTriggerPin, LOW); //para generar un pulso limpio ponemos a LOW 4us
delayMicroseconds(4);
digitalWrite(frontTriggerPin, HIGH); //generamos Trigger (disparo) de 10us
delayMicroseconds(10);
digitalWrite(frontTriggerPin, LOW);
frontDuration = pulseIn(frontEchoPin, HIGH); //medimos el tiempo entre pulsos, en microsegundos
frontDistanceCm = frontDuration * 10 / 292 / 2; //convertimos a distancia, en cm
Serial.print(“Distance: “);
Serial.print(frontDistanceCm);
Serial.println(” cm”);
}
void checkLeftDistance() {
digitalWrite(leftTriggerPin, LOW); //para generar un pulso limpio ponemos a LOW 4us
delayMicroseconds(4);
digitalWrite(leftTriggerPin, HIGH); //generamos Trigger (disparo) de 10us
delayMicroseconds(10);
digitalWrite(leftTriggerPin, LOW);
leftDuration = pulseIn(leftEchoPin, HIGH); //medimos el tiempo entre pulsos, en microsegundos
leftDistanceCm = leftDuration * 10 / 292 / 2; //convertimos a distancia, en cm
Serial.print(“Left distance: “);
Serial.print(leftDistanceCm);
Serial.println(” cm”);
}
void checkRightDistance() {
digitalWrite(rightTriggerPin, LOW); //para generar un pulso limpio ponemos a LOW 4us
delayMicroseconds(4);
digitalWrite(rightTriggerPin, HIGH); //generamos Trigger (disparo) de 10us
delayMicroseconds(10);
digitalWrite(rightTriggerPin, LOW);
rightDuration = pulseIn(rightEchoPin, HIGH); //medimos el tiempo entre pulsos, en microsegundos
rightDistanceCm = rightDuration * 10 / 292 / 2; //convertimos a distancia, en cm
Serial.print(“Right distance: “);
Serial.print(rightDistanceCm);
Serial.println(” cm”);
}
void moveBackward() {
Serial.println(“Backward.”);
digitalWrite(motorL1, HIGH);
digitalWrite(motorL2, LOW);
digitalWrite(motorR1, HIGH);
digitalWrite(motorR2, LOW);
}
void moveForward() {
Serial.println(“Forward.”);
digitalWrite(motorL1, LOW);
digitalWrite(motorL2, HIGH);
digitalWrite(motorR1, LOW);
digitalWrite(motorR2, HIGH);
}
void moveLeft() {
Serial.println(“Left.”);
digitalWrite(motorL1, LOW);
digitalWrite(motorL2, HIGH);
digitalWrite(motorR1, HIGH);
digitalWrite(motorR2, LOW);
}
void moveRight() {
Serial.println(“Right.”);
digitalWrite(motorL1, HIGH);
digitalWrite(motorL2, LOW);
digitalWrite(motorR1, LOW);
digitalWrite(motorR2, HIGH);
}

i would like to start a script and stop with a 1 pushbutton without resistor

here's some code to demonstrate how to detect a button press.

#define butPin  A1

void setup() {
    Serial.begin(115200);
    pinMode (butPin, INPUT_PULLUP);
}

void
butPressed ()
{
    Serial.println ("button pressed");
}

void loop() {
    static bool butLst   = HIGH;
           bool but      = digitalRead (butPin);

    if (butLst != but)  {
        butLst = but;
        if (LOW == but)
            butPressed ();
    }

    delay (10);     // to debounce button
}

you may want to simply toggle a flag that controls whether you do something instead of calling butPress()

if you change this line:

void loop() {

to this line:

void theSketch() {

then you can write a new loop() to turn it on and off.

const byte OnOffButton_DIPin = 12;
// In setup() put pinMode(OnOffButton_DIPin, INPUT_PULLUP);
// Wire the button between Pin 12 and Ground.


bool Running = false;


void loop()
{
  if (digitalRead(OnOffButton_DIPin) == LOW) // Button Pressed
    Running = !Running;  // Toggle the flag


  if (Running)
    theSketch();
  else
    delay(500);
}

like this? thnx for helping me


const int frontEchoPin = 7;
const int frontTriggerPin = 6;
const int leftEchoPin = 11;
const int leftTriggerPin = 10;
const int rightEchoPin = 9;
const int rightTriggerPin = 8;
const int motorL1 = 2;
const int motorL2 = 3;
const int motorR1 = 4;
const int motorR2 = 5;
volatile float maxFrontDistance = 25.00;
volatile float frontDuration, frontDistanceCm, leftDuration, leftDistanceCm, rightDuration, rightDistanceCm;
volatile float maxLeftDistance, maxRightDistance = 20.00;
void setup() {
// serial
Serial.begin(9600);
// ultrasonic
pinMode(frontTriggerPin, OUTPUT);
pinMode(frontEchoPin, INPUT);
pinMode(leftTriggerPin, OUTPUT);
pinMode(leftEchoPin, INPUT);
pinMode(rightTriggerPin, OUTPUT);
pinMode(rightEchoPin, INPUT);
// motors
pinMode(motorL1, OUTPUT);
pinMode(motorL2, OUTPUT);
pinMode(motorR1, OUTPUT);
pinMode(motorR2, OUTPUT);
}
void theSketch() {
const byte OnOffButton_DIPin = 12;
// In setup() put pinMode(OnOffButton_DIPin, INPUT_PULLUP);
// Wire the button between Pin 12 and Ground.

bool Running = false;

void loop()
{
if (digitalRead(OnOffButton_DIPin) == LOW) // Button Pressed
Running = !Running; // Toggle the flag

if (Running)
theSketch();
else
delay(500);
}
// front distance check
checkFrontDistance();
if (frontDistanceCm < maxFrontDistance) {
Serial.println(“Too close”);
checkLeftDistance();
delay(20);
checkRightDistance();
delay(20);
if (leftDistanceCm < rightDistanceCm)
moveRight();
else if (leftDistanceCm > rightDistanceCm) {
moveLeft();
}
}
else {
Serial.println(“OK”);
moveForward();
}
// left distance check
checkLeftDistance();
if (leftDistanceCm < maxLeftDistance) {
Serial.println(“Left too close”);
delay(20);
checkLeftDistance();
delay(20);
checkRightDistance();
delay(20);
if (leftDistanceCm > rightDistanceCm)
moveForward();
else if (leftDistanceCm < rightDistanceCm) {
moveRight();
}
}
// right distance check
checkRightDistance();
if (rightDistanceCm < maxRightDistance) {
Serial.println(“Right too close”);
delay(20);
checkRightDistance();
delay(20);
checkLeftDistance();
delay(20);
if (rightDistanceCm > leftDistanceCm)
moveForward();
else if (rightDistanceCm < leftDistanceCm) {
moveLeft();
}
}
}
void checkFrontDistance() {
digitalWrite(frontTriggerPin, LOW); //para generar un pulso limpio ponemos a LOW 4us
delayMicroseconds(4);
digitalWrite(frontTriggerPin, HIGH); //generamos Trigger (disparo) de 10us
delayMicroseconds(10);
digitalWrite(frontTriggerPin, LOW);
frontDuration = pulseIn(frontEchoPin, HIGH); //medimos el tiempo entre pulsos, en microsegundos
frontDistanceCm = frontDuration * 10 / 292 / 2; //convertimos a distancia, en cm
Serial.print(“Distance: “);
Serial.print(frontDistanceCm);
Serial.println(” cm”);
}
void checkLeftDistance() {
digitalWrite(leftTriggerPin, LOW); //para generar un pulso limpio ponemos a LOW 4us
delayMicroseconds(4);
digitalWrite(leftTriggerPin, HIGH); //generamos Trigger (disparo) de 10us
delayMicroseconds(10);
digitalWrite(leftTriggerPin, LOW);
leftDuration = pulseIn(leftEchoPin, HIGH); //medimos el tiempo entre pulsos, en microsegundos
leftDistanceCm = leftDuration * 10 / 292 / 2; //convertimos a distancia, en cm
Serial.print(“Left distance: “);
Serial.print(leftDistanceCm);
Serial.println(” cm”);
}
void checkRightDistance() {
digitalWrite(rightTriggerPin, LOW); //para generar un pulso limpio ponemos a LOW 4us
delayMicroseconds(4);
digitalWrite(rightTriggerPin, HIGH); //generamos Trigger (disparo) de 10us
delayMicroseconds(10);
digitalWrite(rightTriggerPin, LOW);
rightDuration = pulseIn(rightEchoPin, HIGH); //medimos el tiempo entre pulsos, en microsegundos
rightDistanceCm = rightDuration * 10 / 292 / 2; //convertimos a distancia, en cm
Serial.print(“Right distance: “);
Serial.print(rightDistanceCm);
Serial.println(” cm”);
}
void moveBackward() {
Serial.println(“Backward.”);
digitalWrite(motorL1, HIGH);
digitalWrite(motorL2, LOW);
digitalWrite(motorR1, HIGH);
digitalWrite(motorR2, LOW);
}
void moveForward() {
Serial.println(“Forward.”);
digitalWrite(motorL1, LOW);
digitalWrite(motorL2, HIGH);
digitalWrite(motorR1, LOW);
digitalWrite(motorR2, HIGH);
}
void moveLeft() {
Serial.println(“Left.”);
digitalWrite(motorL1, LOW);
digitalWrite(motorL2, HIGH);
digitalWrite(motorR1, HIGH);
digitalWrite(motorR2, LOW);
}
void moveRight() {
Serial.println(“Right.”);
digitalWrite(motorL1, HIGH);
digitalWrite(motorL2, LOW);
digitalWrite(motorR1, LOW);
digitalWrite(motorR2, HIGH);
}

multidays:
like this?

Nope. It looks like you put the new loop() function INSIDE the olde loop() function before renaming the old loop() function. You can;t define a function inside a function so the compiler would have told you that was incorrect. I intended you to put the new loop() function OUTSIDE the old loop() function (I would put it ABOVE, but BELOW would also work).