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);
}
}