The function is executed once and regularly

Hi! Please help me finish the algorithm. I can't make the home_position() function run in the loop only once, after execution the function2() function is executed only once, after execution the secondFunction() function is executed, the secondFunction() function is executed as in the loop.

I could send the home_position() function to setup. But I need it to be in the loop.

Here is my code:

const int STBY = 5;
const int PWMA = 4;
const int AIN1 = 14;
const int AIN2 = 15;

const int PWMB = 6;
const int AIN1B = 16;
const int AIN2B = 17;

const int STBY1 = 8;
const int PWMC = 7;
const int AIN1C = 18;
const int AIN2C = 19;

const int PWMD = 9;
const int AIN1D_PIN  = 20;
const int AIN2D_PIN  = 21;

const int STBY2 = 11;
const int PWMG = 10;
const int AIN1G_PIN  = 22;
const int AIN2G_PIN  = 23;

const int PWMH = 12;
const int AIN1H_PIN  = 24;
const int AIN2H_PIN  = 25;


const int sensorPinA = A0;
const int sensorPinB = A1;
const int sensorPinC = A2;
const int sensorPinD = A3;
const int sensorPinG = A4;
const int sensorPinH = A5;

long sensorValueA, sensorAngleA;
long sensorValueB, sensorAngleB;
long sensorValueC, sensorAngleC;
long sensorValueD, sensorAngleD;
long sensorValueG, sensorAngleG;
long sensorValueH, sensorAngleH;
const int kizil = 35;
int jasil = 0;

const int kizil1 = 37;
int jasil1 = 0;

void setup() {
  Serial.begin(9600);

  pinMode(STBY, OUTPUT);
  pinMode(PWMA, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);

  pinMode(PWMB, OUTPUT);
  pinMode(AIN1B, OUTPUT);
  pinMode(AIN2B, OUTPUT);

  pinMode(STBY1, OUTPUT);
  pinMode(PWMC, OUTPUT);
  pinMode(AIN1C, OUTPUT);
  pinMode(AIN2C, OUTPUT);

  pinMode(PWMD, OUTPUT);
  pinMode(AIN1D_PIN, OUTPUT);
  pinMode(AIN2D_PIN, OUTPUT);

  pinMode(STBY2, OUTPUT);
  pinMode(PWMG, OUTPUT);
  pinMode(AIN1G_PIN, OUTPUT);
  pinMode(AIN2G_PIN, OUTPUT);

  pinMode(PWMH, OUTPUT);
  pinMode(AIN1H_PIN, OUTPUT);
  pinMode(AIN2H_PIN, OUTPUT);
  pinMode(kizil, INPUT_PULLUP);
  pinMode(kizil1, INPUT_PULLUP);


}
void loop()
{
  ////The home_position() function should be executed only once at the beginning
  home_position();
  // // After executing the home_position() function, the following function2() function is executed; Only once
  function2();
  ////after the secondFunction() function is executed; regularly
  secondFunction();
}

void function2() {
  Serial.println(sensorAngleG);
  sensorValueG = analogRead(sensorPinG);
  sensorAngleG = map(sensorValueG, 0, 1023, 0, 333);
  if (sensorAngleG < 65) {
    digitalWrite(STBY2, HIGH);
    digitalWrite(AIN1G_PIN, HIGH);
    digitalWrite(AIN2G_PIN, LOW);
    analogWrite(PWMG, 255);
  } else if (sensorAngleG < 174) {
    digitalWrite(STBY2, HIGH);
    digitalWrite(AIN1G_PIN, HIGH);
    digitalWrite(AIN2G_PIN, LOW);
    analogWrite(PWMG, 100);
  } else if (sensorAngleG > 270) {
    digitalWrite(STBY2, HIGH);
    digitalWrite(AIN1G_PIN, LOW);
    digitalWrite(AIN2G_PIN, HIGH);
    analogWrite(PWMG, 255);
  } else if (sensorAngleG > 176) {
    digitalWrite(STBY2, HIGH);
    digitalWrite(AIN1G_PIN, LOW);
    digitalWrite(AIN2G_PIN, HIGH);
    analogWrite(PWMG, 100);
  } else {
    digitalWrite(STBY2, LOW);
    digitalWrite(AIN1G_PIN, LOW);
    digitalWrite(AIN2G_PIN, LOW);
    analogWrite(PWMG, 0);
  }
}



void secondFunction() {


  jasil = digitalRead(kizil);
  jasil1 = digitalRead(kizil1);
  if (jasil == LOW && jasil1 == HIGH) {
    Serial.println(sensorAngleA);
    sensorValueA = analogRead(sensorPinA);
    sensorAngleA = map(sensorValueA, 0, 1023, 0, 333);
    sensorValueB = analogRead(sensorPinB);
    sensorAngleB = map(sensorValueB, 0, 1023, 0, 333);
    sensorValueC = analogRead(sensorPinC);
    sensorAngleC = map(sensorValueC, 0, 1023, 0, 333);
    sensorValueD = analogRead(sensorPinD);
    sensorAngleD = map(sensorValueD, 0, 1023, 0, 333);
    sensorValueG = analogRead(sensorPinG);
    sensorAngleG = map(sensorValueG, 0, 1023, 0, 333);
    sensorValueH = analogRead(sensorPinH);
    sensorAngleH = map(sensorValueH, 0, 1023, 0, 333);

    if (sensorAngleA >= 107) {
      digitalWrite(STBY, HIGH);
      digitalWrite(AIN1, LOW);
      digitalWrite(AIN2, HIGH);
      analogWrite(PWMA, 255);
    } else {
      digitalWrite(STBY, LOW);
      digitalWrite(AIN1, LOW);
      digitalWrite(AIN2, LOW);
      analogWrite(PWMA, 0);
    }

    if (sensorAngleB >= 102) {
      digitalWrite(STBY, HIGH);
      digitalWrite(AIN1B, LOW);
      digitalWrite(AIN2B, HIGH);
      analogWrite(PWMB, 255);
    } else {
      digitalWrite(AIN1B, LOW);
      digitalWrite(AIN2B, LOW);
      analogWrite(PWMB, 0);
    }

    if (sensorAngleC >= 54) {
      digitalWrite(STBY1, HIGH);
      digitalWrite(AIN1C, LOW);
      digitalWrite(AIN2C, HIGH);
      analogWrite(PWMC, 255);
    } else {
      digitalWrite(AIN1C, LOW);
      digitalWrite(AIN2C, LOW);
      analogWrite(PWMC, 0);
    }

    if (sensorAngleD >= 101) {
      digitalWrite(STBY1, HIGH);
      digitalWrite(AIN1D_PIN, LOW);
      digitalWrite(AIN2D_PIN, HIGH);
      analogWrite(PWMD, 255);
    } else {
      digitalWrite(AIN1D_PIN, LOW);
      digitalWrite(AIN2D_PIN, LOW);
      analogWrite(PWMD, 0);
    }

    if (sensorAngleH >= 142) {
      digitalWrite(STBY2, HIGH);
      digitalWrite(AIN1H_PIN, LOW);
      digitalWrite(AIN2H_PIN, HIGH);
      analogWrite(PWMH, 255);
    } else {
      digitalWrite(AIN1H_PIN, LOW);
      digitalWrite(AIN2H_PIN, LOW);
      analogWrite(PWMH, 0);
    }

  }
  else if (jasil == HIGH && jasil1 == LOW)
  {
    Serial.println(sensorAngleA);
    sensorValueA = analogRead(sensorPinA);
    sensorAngleA = map(sensorValueA, 0, 1023, 0, 333);
    sensorValueB = analogRead(sensorPinB);
    sensorAngleB = map(sensorValueB, 0, 1023, 0, 333);
    sensorValueC = analogRead(sensorPinC);
    sensorAngleC = map(sensorValueC, 0, 1023, 0, 333);
    sensorValueD = analogRead(sensorPinD);
    sensorAngleD = map(sensorValueD, 0, 1023, 0, 333);
    sensorValueG = analogRead(sensorPinG);
    sensorAngleG = map(sensorValueG, 0, 1023, 0, 333);
    sensorValueH = analogRead(sensorPinH);
    sensorAngleH = map(sensorValueH, 0, 1023, 0, 333);
    if (sensorAngleA <= 180) {
      digitalWrite(STBY, HIGH);
      digitalWrite(AIN1, HIGH);
      digitalWrite(AIN2, LOW);
      analogWrite(PWMA, 255);
    }
    else
    {
      digitalWrite(STBY, LOW);
      digitalWrite(AIN1, LOW);
      digitalWrite(AIN2, LOW);
      analogWrite(PWMA, 0);
    }

    if (sensorAngleB <= 208) {
      digitalWrite(STBY, HIGH);
      digitalWrite(AIN1B, HIGH);
      digitalWrite(AIN2B, LOW);
      analogWrite(PWMB, 255);
    }

    else
    {
      digitalWrite(STBY, LOW);
      digitalWrite(AIN1B, LOW);
      digitalWrite(AIN2B, LOW);
      analogWrite(PWMB, 0);
    }

    if (sensorAngleC <= 219) {
      digitalWrite(STBY1, HIGH);
      digitalWrite(AIN1C, HIGH);
      digitalWrite(AIN2C, LOW);
      analogWrite(PWMC, 255);
    }

    else
    {
      digitalWrite(STBY1, LOW);
      digitalWrite(AIN1C, LOW);
      digitalWrite(AIN2C, LOW);
      analogWrite(PWMC, 0);
    }

    if (sensorAngleD <= 215) {
      digitalWrite(STBY1, HIGH);
      digitalWrite(AIN1D_PIN, HIGH);
      digitalWrite(AIN2D_PIN, LOW);
      analogWrite(PWMD, 255);
    }
    else
    {
      digitalWrite(STBY1, LOW);
      digitalWrite(AIN1D_PIN, LOW);
      digitalWrite(AIN2D_PIN, LOW);
      analogWrite(PWMD, 0);
    }

    if (sensorAngleH <= 156) {
      digitalWrite(STBY2, HIGH);
      digitalWrite(AIN1H_PIN, HIGH);
      digitalWrite(AIN2H_PIN, LOW);
      analogWrite(PWMH, 255);
    }
    else
    {
      digitalWrite(STBY2, LOW);
      digitalWrite(AIN1H_PIN, LOW);
      digitalWrite(AIN2H_PIN, LOW);
      analogWrite(PWMH, 0);
    }
  }
  else {
    digitalWrite(STBY, LOW);
    digitalWrite(STBY1, LOW);
    digitalWrite(STBY2, LOW);
    digitalWrite(AIN1, LOW);
    digitalWrite(AIN2, LOW);
    digitalWrite(AIN1B, LOW);
    digitalWrite(AIN2B, LOW);
    digitalWrite(AIN1C, LOW);
    digitalWrite(AIN2C, LOW);
    digitalWrite(AIN1D_PIN, LOW);
    digitalWrite(AIN2D_PIN, LOW);
    digitalWrite(AIN1H_PIN, LOW);
    digitalWrite(AIN2H_PIN, LOW);
    analogWrite(PWMA, 0);
    analogWrite(PWMB, 0);
    analogWrite(PWMC, 0);
    analogWrite(PWMD, 0);
    analogWrite(PWMH, 0);
  }

}

void home_position()
{

  digitalWrite(STBY, HIGH);
  digitalWrite(AIN1, LOW);
  digitalWrite(AIN2, HIGH);
  analogWrite(PWMA, 255);

  digitalWrite(AIN1B, LOW);
  digitalWrite(AIN2B, HIGH);
  analogWrite(PWMB, 255);
  digitalWrite(STBY1, HIGH);
  digitalWrite(AIN1C, LOW);
  digitalWrite(AIN2C, HIGH);
  analogWrite(PWMC, 255);

  digitalWrite(AIN1D_PIN, LOW);
  digitalWrite(AIN2D_PIN, HIGH);
  analogWrite(PWMD, 255);

  digitalWrite(STBY2, HIGH);
  digitalWrite(AIN1G_PIN, LOW);
  digitalWrite(AIN2G_PIN, HIGH);
  analogWrite(PWMG, 255);
  digitalWrite(AIN1H_PIN, LOW);
  digitalWrite(AIN2H_PIN, HIGH);
  analogWrite(PWMH, 255);
  while (true) {
    sensorValueA = analogRead(sensorPinA);
    sensorAngleA = map(sensorValueA, 0, 1023, 0, 333);
    sensorValueB = analogRead(sensorPinB);
    sensorAngleB = map(sensorValueB, 0, 1023, 0, 333);
    sensorValueC = analogRead(sensorPinC);
    sensorAngleC = map(sensorValueC, 0, 1023, 0, 333);
    sensorValueD = analogRead(sensorPinD);
    sensorAngleD = map(sensorValueD, 0, 1023, 0, 333);
    sensorValueG = analogRead(sensorPinG);
    sensorAngleG = map(sensorValueG, 0, 1023, 0, 333);
    sensorValueH = analogRead(sensorPinH);
    sensorAngleH = map(sensorValueH, 0, 1023, 0, 333);

    if (sensorAngleA <= 107) {
      digitalWrite(AIN1, LOW);
      digitalWrite(AIN2, LOW);
      analogWrite(PWMA, 0);
    }
    if (sensorAngleB <= 102) {
      digitalWrite(AIN1B, LOW);
      digitalWrite(AIN2B, LOW);
      analogWrite(PWMB, 0);
    }
    if (sensorAngleC <= 54) {
      digitalWrite(AIN1C, LOW);
      digitalWrite(AIN2C, LOW);
      analogWrite(PWMC, 0);
    }
    if (sensorAngleD <= 101) {
      digitalWrite(AIN1D_PIN, LOW);
      digitalWrite(AIN2D_PIN, LOW);
      analogWrite(PWMD, 0);
    }
    if (sensorAngleG <= 125) {
      digitalWrite(AIN1G_PIN, LOW);
      digitalWrite(AIN2G_PIN, LOW);
      analogWrite(PWMG, 0);
    }
    if (sensorAngleH <= 142) {
      digitalWrite(AIN1H_PIN, LOW);
      digitalWrite(AIN2H_PIN, LOW);
      analogWrite(PWMH, 0);
    }
    if (sensorAngleA <= 107 && sensorAngleB <= 102 && sensorAngleC <= 54 && sensorAngleD <= 101 && sensorAngleG <= 125 && sensorAngleH <= 142) {
      digitalWrite(STBY, LOW);
      digitalWrite(STBY1, LOW);
      digitalWrite(STBY2, LOW);
      break;
    }
    delay(100);
  }
}

I need help here.

I can't see why you just don't end setup() with

  home_position();
  function2();
}

and lose the call to function2() in the loop.

Or do this in the loop

  static bool iDid;

  if (!iDid) {

    function2():

    iDid = true;
  }


HTH

a7

Make execution of the function conditional on a boolean being true. Set the boolean to true initially then, in the function set the booelan to false to prevent it being executed again

It is impossible for home_position() and function 2(); to be executed simultaneously, breaks the loop (tried). Is it possible that somehow the first two functions home_position() and function 2() were executed sequentially only once. And secondFunction() worked like in a loop. I need these three functions to be inside the loop, unfortunately setup is not possible

Why ?

Like this?
bool homePositionDone = false;

if (!homePositionDone) {
    home_position();
    homePositionDone = true;  /
  }

Because I will add another button to the algorithm to switch modes, and the sequence will add many many algorithms like this, so all this needs to be in the loop

See reply #3 from @alto777

I looked, I am not allowed to perform a function in setup. But I tried anyway, as alto777 wrote, function 2() does not work correctly, that is, it works when the second Function() function is running. THAT is, when the button is pressed. I need the function to work sequentially, moving to the next function only after its execution

Sry, I'm not able to figure out what needs to be done once.

ButI have no doubt that things that need to be done once can (should) be done it setup().

Let's say you needed to sequentially use three functions A(), B() and C(), and for some reason the first time through B() needed to be done twice, but forever more after just once.

In the setup(), do

void setup()
{
  A():
  B();
  B();  // B needs another call at the very first.
  C();
}

and in loop(), just

void setup()
{
  A():
  B();
  C();
}

So write down your sequence from the beginning, and chop it in two as soon as it is indeed fully repetitive.

Less ugly than booleans.

a7

We won't do anything in setup, it should be empty.

void setup()
{



}

There are 3 functions to be performed in the loop. 3 functions cannot be performed simultaneously because they contradict each other. The first function executed is home_position();

home_position() - Executed only once in the loop. After its completion (we are waiting for it to be completed)

The second function, function 2(), is executed sequentially; This function is also executed only once in the loop(after executing home_position) After its completion (we are waiting for it to be completed)

We execute secondFunction() sequentially; This function should be executed as a function is usually executed in a loop.

void loop()
{
  //The home_position() function should be executed only once at the beginning

  home_position();
  
  //After executing the home_position() function, the following function is executed: function 2(); Only once
  
  function2();
  
  //after executing the second function, function 2(); second Function() is executed regularly;
  
  secondFunction();
}

looks like your trying to drive several motors at once and stop each of them when some sensor specific to each motor becomes active. not sure

can you explain what you're trying to do rather than the way you think you need to do it?

code might be easier to read if you had stop, forward and reverse sub-functions for each motor

Why not ?

In any case you already broke this rule several times in your sketch

  Serial.begin(9600);

  pinMode(STBY, OUTPUT);
  pinMode(PWMA, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);

  pinMode(PWMB, OUTPUT);
  pinMode(AIN1B, OUTPUT);
  pinMode(AIN2B, OUTPUT);

  pinMode(STBY1, OUTPUT);
  pinMode(PWMC, OUTPUT);
  pinMode(AIN1C, OUTPUT);
  pinMode(AIN2C, OUTPUT);

  pinMode(PWMD, OUTPUT);
  pinMode(AIN1D_PIN, OUTPUT);
  pinMode(AIN2D_PIN, OUTPUT);

  pinMode(STBY2, OUTPUT);
  pinMode(PWMG, OUTPUT);
  pinMode(AIN1G_PIN, OUTPUT);
  pinMode(AIN2G_PIN, OUTPUT);

  pinMode(PWMH, OUTPUT);
  pinMode(AIN1H_PIN, OUTPUT);
  pinMode(AIN2H_PIN, OUTPUT);
  pinMode(kizil, INPUT_PULLUP);
  pinMode(kizil1, INPUT_PULLUP);

I'm doing a barrier project. There are 6 barriers in the project. When the project is launched, all the barriers open. After opening 5, the barrier closes and stops when the value 175 is shown in the sensor. After that, when I press the 1,2,3,4,6 button, the barriers close. If you press the second button 1,2,3,4,6, the barrier opens

Because similar algorithms will be added to the algorithm in the future. A button will be added that allows you to switch between modes.

For example, I will switch to the second mode, I will need to execute the home_position() function once, but I will not be able to return the function to setup. For this reason, I have an algorithm to do inside the loop

So those functions in void loop() are your modes that all do similar things you'd like to use a button to select from?

Looked where?

Functions do not run simultaneously, your descriptions and explanations are not making sense to me.

Perhaps if you shared the complete assignement, which we can hope was written well, we can get some idea of exactly what you are trying to do, and the seemingly arbitrary and unnecessary constraints that you have been burdened with.

Taken literally, there are very few sketches that do not call functions from setup().

Please post the assignment in its entirety.

a7

Check it out, is this what you're after?

@hallowed31 - this

  buttonMode = digitalRead(modeButton);
  delay(20); // low fi debouncing
  buttonMode = digitalRead(modeButton);// debounced button read
  switch (buttonMode) {

is the same as

  delay(20); // low fi debouncing
  buttonMode = digitalRead(modeButton);// debounced button read

Think about it - you read the button, wait, and read the button into the same variable. Therefore the first reading is without any effect.

Then you use the state of the button, not the state change, so this

  switch (buttonMode) {

will follow the button, no matter it has been released or pressed since last time.

You are getting away with it because of the delay in the actions; hold your finger on the button and watch.

Change the code to

  buttonMode = digitalRead(modeButton);// debounced button read
  switch (buttonMode) {

and test it again - the behaviour will be unchanged except you will have 20 milliseconds less time to get your finger off the button to avoid repeating the action. 1000 milliseconds instead of 1020, or 2000 instead of 2020, approximately.

I'm sure you must have debounced and detected a state change in a button by this time, it's not what is happening here.

Your loop, to which I only added or edited print statements (and slowed it down with a loop delay) shows what's really being called and when, I do not think this is what you were going for.

@muhammedjan having two functions called function2() and secondFunction() is not a very good idea. It dosen't have to be confusing like that. Just sayin'.

a7