calling a function from main loop using push button code

i have a loop (main loop) with 2 functions ( loop1() and loop2() ) i need a help in how to call one
functions from main loop by using push button code.

#include <NewPing.h>
#include <PS3BT.h>
#include <usbhub.h>
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>


int Motor_A_Enable = 9;
int Motor_A_Reverse = 4;
int Motor_A_Forward = 3;


int Motor_B_Enable = 10;
int Motor_B_Reverse = 7;
int Motor_B_Forward = 12;


void setup() {
  setup1();
  setup2();
}


void loop() {
  loop1();
  loop2();
}




void setup1() {
  // put your setup code here, to run once:
  Serial.begin(38400);


  pinMode(Motor_A_Enable, OUTPUT);
  pinMode(Motor_A_Forward, OUTPUT);
  pinMode(Motor_A_Reverse, OUTPUT);


  pinMode(Motor_B_Enable, OUTPUT);
  pinMode(Motor_B_Forward, OUTPUT);
  pinMode(Motor_B_Reverse, OUTPUT);
}


void loop1() {


  if (Serial.available() > 0)
  {
    char data;
    data = Serial.read();
    Serial.write(Serial.read());


    switch (data)
    {
      case '4': //FORWARD


        //delay(1000);


        digitalWrite(Motor_A_Reverse, LOW);
        digitalWrite(Motor_B_Reverse, LOW);


        digitalWrite(Motor_A_Forward, HIGH);
        digitalWrite(Motor_B_Forward, HIGH);

        for (int a = 0; a < 200; a++)
        {
          analogWrite(Motor_B_Enable, a);
          analogWrite(Motor_A_Enable, a);
          delay(5);
        }


        break;
      case '5': //REVERSE


        //delay(1000);


        digitalWrite(Motor_A_Forward, LOW);
        digitalWrite(Motor_B_Forward, LOW);




        digitalWrite(Motor_A_Reverse, HIGH);
        digitalWrite(Motor_B_Reverse, HIGH);


        for (int a = 0; a < 200; a++)
        {
          analogWrite(Motor_B_Enable, a);
          analogWrite(Motor_A_Enable, a);
          delay(5);
        }




        break;
      case '7': //FORWARD LEFT




        for (int a = 0; a < 255; a++)
        {
          analogWrite(Motor_A_Enable, a);
          //analogWrite(Motor_B_Enable, a);
          delay(5);
        }




        // analogWrite(Motor_A_Enable, 230);
        //analogWrite(Motor_B_Enable, 255);


        digitalWrite(Motor_A_Forward, HIGH);
        //  digitalWrite(Motor_B_Reverse, HIGH);
        digitalWrite(Motor_A_Reverse, LOW);
        //  digitalWrite(Motor_B_Forward, LOW);


        break;
      case '6': //FORWARD RIGHT




        for (int a = 0; a < 255; a++)
        {
          //analogWrite(Motor_A_Enable, a);
          analogWrite(Motor_B_Enable, a);
          delay(5);
        }
        // analogWrite(Motor_A_Enable, 250);
        analogWrite(Motor_B_Enable, 230);




        //  digitalWrite(Motor_A_Reverse, HIGH);
        digitalWrite(Motor_B_Forward, HIGH);
        //  digitalWrite(Motor_A_Forward, LOW);
        digitalWrite(Motor_B_Reverse, LOW);


        break;
      default: //If bluetooth module receives any value not listed above, both motors turn off
        analogWrite(Motor_A_Enable, 0);
        analogWrite(Motor_B_Enable, 0);
    }
  }
}

#define trig_pin A1 //analog input 1
#define echo_pin A2 //analog input 2
#define maximum_distance 200
boolean goesForward = false;
int distance = 100;
NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
USB Usb;
BTD Btd(&Usb);
//PS3BT PS3(&Btd);
PS3BT PS3(&Btd); // This will just create the instance
//PS3BT PS3(&Btd, 0x00, 0x15, 0x83, 0x3D, 0x0A, 0x57); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch
int const PWMA = 3;
int const PWMB = 5;
int const dirA = 2;
int const dirB = 4;
void _mForward()
{
  digitalWrite(dirA, 255);
  analogWrite(PWMA, HIGH);
  digitalWrite(dirB, 255);
  analogWrite(PWMB, HIGH);
}
void _mBack()
{
  digitalWrite(dirA, LOW);
  analogWrite(PWMA, 255);
  digitalWrite(dirB, LOW);
  analogWrite(PWMB, 255);;
}
void _mleft()
{
  digitalWrite(dirA, LOW);
  analogWrite(PWMA, 255);
  digitalWrite(dirB, HIGH);
  analogWrite(PWMB, 255);
}
void _mright()
{
  digitalWrite(dirA, HIGH);
  analogWrite(PWMA, 255);
  digitalWrite(dirB, LOW);
  analogWrite(PWMB, 255);
}
void _mStop()
{
  digitalWrite(dirA, 0);
  analogWrite(PWMA, 0);
  digitalWrite(dirB, 0);
  analogWrite(PWMB, 0);
}
void setup2() {
  Serial.begin(9600);
  if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while (1); //halt
  }
  Serial.print(F("\r\nPS3 Bluetooth Library Started"));
  pinMode(PWMA, OUTPUT);
  pinMode(PWMB, OUTPUT);
  pinMode(dirA, OUTPUT);
  pinMode(dirB, OUTPUT);
  distance = readPing();
}
void loop2() {
  Usb.Task();
  int distanceRight = 0;
  int distanceLeft = 0;
  if (PS3.PS3Connected || PS3.PS3NavigationConnected) {
    if (distance <= 20) {
      _mStop();
      delay(300);
      _mForward();
      delay(400);
      _mStop();
      delay(300);
    }
    else {
      if (PS3.getButtonPress(RIGHT))
      {
        _mright();
      }
      else if (PS3.getButtonPress(LEFT)) {
        _mleft();
      }
      else if (PS3.getButtonPress(CROSS))
      {
        _mForward();
      }
      else if (PS3.getButtonPress(TRIANGLE))
      {
        _mBack();
      }
      else {
        _mStop();
      }
    }
  }
  distance = readPing();
}
int readPing() {
  delay(0);
  int cm = sonar.ping_cm();
  if (cm == 0) {
    cm = 250;
  }
  return cm;
}

i have a code with two loops

Do you ?
I see a program with one function that loops (the loop() function) and 2 functions named loop1() and loop2() that don't loop but are both called repeatedly.

Do you mean that you want to call either loop1() or loop2() repeatedly from the loop() function depending on whether an input is HIGH or LOW (or maybe if an input becomes HIGH or LOW) ?

Also, please read the posts at the top of the Forum on how to post code using code tags (not quotes). It takes up last space than quotes since the code can be scrolled.

UKHeliBob:
Do you ?
I see a program with one function that loops (the loop() function) and 2 functions named loop1() and loop2() that don't loop but are both called repeatedly.

Do you mean that you want to call either loop1() or loop2() repeatedly from the loop() function depending on whether an input is HIGH or LOW (or maybe if an input becomes HIGH or LOW) ?

yes you are right !!
i need to embed the push button code in the original code to select between 2 functions ( loop1() and loop2() )

i need to embed the push button code in the original code to select between 2 functions ( loop1() and loop2() )

void loop()
{
  if (digitalRead(anInput) == HIGH)
  {
    loop1();
  }
  else
  {
    loop2();
  }
}

Have a look at the readButton() function in Several Things at a Time

It should not be difficult to adapt that to call another function, or to set a variable so that another function will always be called if that variable has a certain value. For example your code in loop() could be something like this

void loop() {
   readButton();
   if (loopChosen == 1) {
      loop1();
   }
   else if (loopChosen == 2) {
     loop2();
   }
}

and put code in your readButton() function that sets the value of loopChosen appropriately

...R

UKHeliBob:

void loop()

{
 if (digitalRead(anInput) == HIGH)
 {
   loop1();
 }
 else
 {
   loop2();
 }
}

Robin2:
Have a look at the readButton() function in Several Things at a Time

It should not be difficult to adapt that to call another function, or to set a variable so that another function will always be called if that variable has a certain value. For example your code in loop() could be something like this

void loop() {

readButton();
  if (loopChosen == 1) {
      loop1();
  }
  else if (loopChosen == 2) {
    loop2();
  }
}




and put code in your readButton() function that sets the value of loopChosen appropriately

...R

in case of changing push button state from low to high state during the working of project it will automatically move fromfunction ( loop1 ()) to function ( loop2() ) ? or it will keep executing first function ( loop1() )

the code after modification

#include <NewPing.h>
#include <PS3BT.h>
#include <usbhub.h>
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>




int Motor_A_Enable = 9;
int Motor_A_Reverse = 4;
int Motor_A_Forward = 3;


int Motor_B_Enable = 10;
int Motor_B_Reverse = 7;
int Motor_B_Forward = 12;

const int buttonPin = 13;
int buttonState = 0; 

void setup() {
  setup1();
  setup2();
  PBS();
}


void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW){
  loop1();}
  else if (buttonState == HIGH){
  loop2();}
}




void setup1() {
  // put your setup code here, to run once:
  Serial.begin(38400);


  pinMode(Motor_A_Enable, OUTPUT);
  pinMode(Motor_A_Forward, OUTPUT);
  pinMode(Motor_A_Reverse, OUTPUT);


  pinMode(Motor_B_Enable, OUTPUT);
  pinMode(Motor_B_Forward, OUTPUT);
  pinMode(Motor_B_Reverse, OUTPUT);
}


void loop1() {


  if (Serial.available() > 0)
  {
    char data;
    data = Serial.read();
    Serial.write(Serial.read());


    switch (data)
    {
      case '4': //FORWARD


        //delay(1000);


        digitalWrite(Motor_A_Reverse, LOW);
        digitalWrite(Motor_B_Reverse, LOW);


        digitalWrite(Motor_A_Forward, HIGH);
        digitalWrite(Motor_B_Forward, HIGH);

        for (int a = 0; a < 200; a++)
        {
          analogWrite(Motor_B_Enable, a);
          analogWrite(Motor_A_Enable, a);
          delay(5);
        }


        break;
      case '5': //REVERSE


        //delay(1000);


        digitalWrite(Motor_A_Forward, LOW);
        digitalWrite(Motor_B_Forward, LOW);




        digitalWrite(Motor_A_Reverse, HIGH);
        digitalWrite(Motor_B_Reverse, HIGH);


        for (int a = 0; a < 200; a++)
        {
          analogWrite(Motor_B_Enable, a);
          analogWrite(Motor_A_Enable, a);
          delay(5);
        }




        break;
      case '7': //FORWARD LEFT




        for (int a = 0; a < 255; a++)
        {
          analogWrite(Motor_A_Enable, a);
          //analogWrite(Motor_B_Enable, a);
          delay(5);
        }




        // analogWrite(Motor_A_Enable, 230);
        //analogWrite(Motor_B_Enable, 255);


        digitalWrite(Motor_A_Forward, HIGH);
        //  digitalWrite(Motor_B_Reverse, HIGH);
        digitalWrite(Motor_A_Reverse, LOW);
        //  digitalWrite(Motor_B_Forward, LOW);


        break;
      case '6': //FORWARD RIGHT




        for (int a = 0; a < 255; a++)
        {
          //analogWrite(Motor_A_Enable, a);
          analogWrite(Motor_B_Enable, a);
          delay(5);
        }
        // analogWrite(Motor_A_Enable, 250);
        analogWrite(Motor_B_Enable, 230);




        //  digitalWrite(Motor_A_Reverse, HIGH);
        digitalWrite(Motor_B_Forward, HIGH);
        //  digitalWrite(Motor_A_Forward, LOW);
        digitalWrite(Motor_B_Reverse, LOW);


        break;
      default: //If bluetooth module receives any value not listed above, both motors turn off
        analogWrite(Motor_A_Enable, 0);
        analogWrite(Motor_B_Enable, 0);
    }
  }
}

#define trig_pin A1 //analog input 1
#define echo_pin A2 //analog input 2
#define maximum_distance 200
boolean goesForward = false;
int distance = 100;
NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
USB Usb;
BTD Btd(&Usb);
//PS3BT PS3(&Btd);
PS3BT PS3(&Btd); // This will just create the instance
//PS3BT PS3(&Btd, 0x00, 0x15, 0x83, 0x3D, 0x0A, 0x57); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch
int const PWMA = 3;
int const PWMB = 5;
int const dirA = 2;
int const dirB = 4;
void _mForward()
{
  digitalWrite(dirA, 255);
  analogWrite(PWMA, HIGH);
  digitalWrite(dirB, 255);
  analogWrite(PWMB, HIGH);
}
void _mBack()
{
  digitalWrite(dirA, LOW);
  analogWrite(PWMA, 255);
  digitalWrite(dirB, LOW);
  analogWrite(PWMB, 255);;
}
void _mleft()
{
  digitalWrite(dirA, LOW);
  analogWrite(PWMA, 255);
  digitalWrite(dirB, HIGH);
  analogWrite(PWMB, 255);
}
void _mright()
{
  digitalWrite(dirA, HIGH);
  analogWrite(PWMA, 255);
  digitalWrite(dirB, LOW);
  analogWrite(PWMB, 255);
}
void _mStop()
{
  digitalWrite(dirA, 0);
  analogWrite(PWMA, 0);
  digitalWrite(dirB, 0);
  analogWrite(PWMB, 0);
}
void setup2() {
  Serial.begin(9600);
  if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while (1); //halt
  }
  Serial.print(F("\r\nPS3 Bluetooth Library Started"));
  pinMode(PWMA, OUTPUT);
  pinMode(PWMB, OUTPUT);
  pinMode(dirA, OUTPUT);
  pinMode(dirB, OUTPUT);
  distance = readPing();
}
void loop2() {
  Usb.Task();
  int distanceRight = 0;
  int distanceLeft = 0;
  if (PS3.PS3Connected || PS3.PS3NavigationConnected) {
    if (distance <= 20) {
      _mStop();
      delay(300);
      _mForward();
      delay(400);
      _mStop();
      delay(300);
    }
    else {
      if (PS3.getButtonPress(RIGHT))
      {
        _mright();
      }
      else if (PS3.getButtonPress(LEFT)) {
        _mleft();
      }
      else if (PS3.getButtonPress(CROSS))
      {
        _mForward();
      }
      else if (PS3.getButtonPress(TRIANGLE))
      {
        _mBack();
      }
      else {
        _mStop();
      }
    }
  }
  distance = readPing();
}
int readPing() {
  delay(0);
  int cm = sonar.ping_cm();
  if (cm == 0) {
    cm = 250;
  }
  return cm;
}



void PBS() {
 pinMode(buttonPin, INPUT_PULLUP);
}

Alaxwora:
in case of changing push button state from low to high state during the working of project it will automatically move fromfunction ( loop1 ()) to function ( loop2() ) ? or it will keep executing first function ( loop1() )

Why don't you try it and see what happens.

I believe my suggestion will work differently from @UKHeliBob's

You should get into the habit of writing really short programs to try out new concepts before applying them into a serious program.

...R

Robin2:
Why don't you try it and see what happens.

I believe my suggestion will work differently from @UKHeliBob's

You should get into the habit of writing really short programs to try out new concepts before applying them into a serious program.

...R

you are right but i used to ask experienced people before doing any thing to avoid making mistakes that's all specially for beginners like me

regards...

Alaxwora:
you are right but i used to ask experienced people before doing any thing to avoid making mistakes that's all specially for beginners like me

The Arduino system is great for learning-by-doing and often you can figure things out yourself faster than by asking questions here.

By the way I am NOT trying to discourage you from asking questions.

...R