function problem

Hello everybody , please i need your help
i’m making a small project line tracking robot in a warehouse , so i have two items each item has a array that saves the path , how to cross the warehouse to get it , or i can add a new path for new item .
so when i press 1 to choose one of the items A or B , for example i choose (A) , after i choose (A) we step inside for loop to read each element in the array and make some action when the element equal to one of the conditions , then do a function.
now my problem is when the array element equal to one of the conditions it does not do the function i need such as ( forward , turn left …) , it goes forward only and does not do any thing of the other functions.
so what i can do to implementation the functions ( FF() , TL() , LR() …)
the code :

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int RPWM_Output1 = 9;
int LPWM_Output1 = 8;
int RPWM_Output2 = 7;
int LPWM_Output2 = 6;
int LL = 34;
int L = 33;
int M =32;
int R = 31;
int RR= 30;
int a=0;
int counter =6;
char A_path[]={'f','t','f','l','f','s'};
char B_path[]={'f','tr','f','ll','f','s'};

void setup()
{
  Serial.begin(9600);
   for(int thispin = 6 ; thispin < 10 ; thispin++)
  {
    pinMode(thispin,OUTPUT);
  }
  for(int S = 30 ; S < 35 ; S++)
  {
    pinMode(S,INPUT);
  }
  lcd.init();
  lcd.backlight();
  lcd.setCursor(4,0);
  lcd.print("S L M R U");
  Serial.println("                                     WELCOME   ");
  Serial.println("===================================================================================");
  delay(15);
  Serial.println("   if you need one of the items press 1 , if you need to add new path press 2");
  Serial.println("====================================================================================");
}
void loop()
{
  int LL_sensor=digitalRead(LL);
  int L_sensor=digitalRead(L);
  int M_sensor=digitalRead(M);
  int R_sensor=digitalRead(R);
  int RR_sensor=digitalRead(RR);
  // the LCD display 
  lcd.setCursor(4,1);
  lcd.print(LL_sensor);
  lcd.setCursor(6,1);
  lcd.print(L_sensor);
  lcd.setCursor(8,1);
  lcd.print(M_sensor);
  lcd.setCursor(10,1);
  lcd.print(R_sensor);
  lcd.setCursor(12,1);
  lcd.print(RR_sensor);
  if(Serial.available()>0)
  {
    char x= Serial.read();
   Serial.println(x);
   Serial.println("====================================================================================");
    if (x=='1')
    {
   Serial.println("====================================================================================");
   delay(15);
   Serial.println("press one of the items A or B");
   delay(15);
   Serial.println("===================================================================================="); 
      while(Serial.available()==0);  // wait for input
      char z = Serial.read();
   Serial.println(z);
      if (z=='a') { 
   Serial.println("Thank you for chooes item AAAAA ");
    for (int y=0 ; y < counter ; y++)
       {
         if (A_path[y]=='f')
          {
            Serial.println("Forward");
            FF(); // forward function 
          } 
          if (A_path[y]=='l')
          {
            Serial.println("L section Right");
            LR(); // L section turn Right
          }
          if (A_path[y]=='t')
          {
            Serial.println("T section Left");
            TL();  // T section turn Left
          }
           if (A_path[y]=='s')
          {
            Serial.println("Stop");
            Stop_S();  // Stop
          }
       }
      }
      else if(z=='b')
    {
      Serial.println("Thank you for chooes item BBBB ");
      for (int i=0 ; i < counter ; i++)
       {
         if (B_path[i]=='f')
          {
            Serial.println("Forward");
            FF();  // Forward 
          }
         if (B_path[i]=='l')
          {
            Serial.println("L section Left");
            Ll();  // L section turn Left
          }  
          if (B_path[i]=='t')
          {
            Serial.println("T section Right");
            TR();  // T section turn Right
          }
           if (B_path[i]=='s')
          {
            Serial.println("Stop");
            Stop_S(); // stop
          }
       }
    }
           }
    if (x=='2') {
   Serial.println("I need to add new path");
    }
  }
}
void FF( )    // forward function                  
  { a=60;
  int LL_sensor=digitalRead(LL);
  int L_sensor=digitalRead(L);
  int M_sensor=digitalRead(M);
  int R_sensor=digitalRead(R);
  int RR_sensor=digitalRead(RR);
  if ( L_sensor == 0 && M_sensor == 1 && R_sensor == 0 )       
    { forward(); }
  else if (  L_sensor == 0 && M_sensor == 0 && R_sensor == 1  )
    { left(); }
  else if (  L_sensor == 1 && M_sensor == 0 && R_sensor == 0   )
    { right(); }
     else if ( L_sensor == 0 && M_sensor == 1 && R_sensor == 1 )
    { left(); }
  else if (L_sensor == 1 && M_sensor == 1 && R_sensor == 0   )
    { right(); }  
  }
    
 void TL() // T section turn Left
{ 
  int LL_sensor=digitalRead(LL);
  int L_sensor=digitalRead(L);
  int M_sensor=digitalRead(M);
  int R_sensor=digitalRead(R);
  int RR_sensor=digitalRead(RR);
  if (LL_sensor==1 && L_sensor == 1 && M_sensor == 1 && R_sensor == 1 && RR_sensor==1 )
       {turn_90L();}
}

void TR() // T section turn Right
{ 
  int LL_sensor=digitalRead(LL);
  int L_sensor=digitalRead(L);
  int M_sensor=digitalRead(M);
  int R_sensor=digitalRead(R);
  int RR_sensor=digitalRead(RR);
  if (LL_sensor==1 && L_sensor == 1 && M_sensor == 1 && R_sensor == 1 && RR_sensor==1 )
       {turn_90R();}
}
void LR()  // L section turn Right
 {
  int LL_sensor=digitalRead(LL);
  int L_sensor=digitalRead(L);
  int M_sensor=digitalRead(M);
  int R_sensor=digitalRead(R);
  int RR_sensor=digitalRead(RR); 
  if  (LL_sensor==0 && L_sensor == 0 && M_sensor == 1 && R_sensor == 1 && RR_sensor==1 ) 
      { turn_L_Right();} 
     }
 void Ll()  // L section turn Left
 {
  int LL_sensor=digitalRead(LL);
  int L_sensor=digitalRead(L);
  int M_sensor=digitalRead(M);
  int R_sensor=digitalRead(R);
  int RR_sensor=digitalRead(RR); 
  if  (LL_sensor==0 && L_sensor == 0 && M_sensor == 1 && R_sensor == 1 && RR_sensor==1 ) 
      { turn_L_Left();} 
     }
     
  
  void Stop_S() // stop 
  {  
  int LL_sensor=digitalRead(LL);
  int L_sensor=digitalRead(L);
  int M_sensor=digitalRead(M);
  int R_sensor=digitalRead(R);
  int RR_sensor=digitalRead(RR); 
  if  (LL_sensor==0 && L_sensor == 0 && M_sensor == 0 && R_sensor == 0 && RR_sensor==0 ) 
     {Stop();}
  }
    


void turn_90L() 
{int K=80;
 delay(100);
  analogWrite(LPWM_Output1, K);
  analogWrite(LPWM_Output2, 0);
  analogWrite(RPWM_Output1, 0);
  analogWrite(RPWM_Output2, K);
  delay(1550);
  forword();
  delay(250);
}
void turn_90R()
{int K=80;
 delay(100);
  analogWrite(LPWM_Output1, 0);
  analogWrite(LPWM_Output2, K);
  analogWrite(RPWM_Output1, K);
  analogWrite(RPWM_Output2, 0);
  delay(1550);
  forword();
  delay(250);
}
void turn_L_Right()
{
   int H=80;
  
  analogWrite(LPWM_Output1, 0);
  analogWrite(LPWM_Output2, H);
  analogWrite(RPWM_Output1, H);
  analogWrite(RPWM_Output2, 0);
  delay(1550);
  forword();
  delay(250);
}
void turn_L_Left()
{
   int H=80;
  
  analogWrite(LPWM_Output1, H);
  analogWrite(LPWM_Output2, 0);
  analogWrite(RPWM_Output1, 0);
  analogWrite(RPWM_Output2, H);
  delay(1550);
  forword();
  delay(250);
}

void forword()
{
   analogWrite(LPWM_Output1, 50);
  analogWrite(LPWM_Output2, 50);
  analogWrite(RPWM_Output1, 0);
  analogWrite(RPWM_Output2, 0);
}
  void forward()
{ 
  analogWrite(LPWM_Output1, a);
  analogWrite(LPWM_Output2, a);
  analogWrite(RPWM_Output1, 0);
  analogWrite(RPWM_Output2, 0);
}
void left ()
{
  analogWrite(LPWM_Output1,a);
  analogWrite(LPWM_Output2, a+20);
  analogWrite(RPWM_Output1, 0);
  analogWrite(RPWM_Output2, 0);
}
void right ()
{
  analogWrite(LPWM_Output1,a+20);
  analogWrite(LPWM_Output2, a);
  analogWrite(RPWM_Output1, 0);
  analogWrite(RPWM_Output2, 0);
}

void Stop()
{
  analogWrite(LPWM_Output1,0);
  analogWrite(LPWM_Output2, 0);
  analogWrite(RPWM_Output1, 0);
  analogWrite(RPWM_Output2, 0);
}

path_with_array.ino (7.12 KB)

Your code is ignoring time. You cannot ignore time to perform a timed series of actions...

Well I think that's the issue - some paths through your code just set somethings and
return to the for loop, so potentially it will just whizz though the loop in a millisecond or less.

However a few of your functions use delay() and probably are sensible...

sorry but i use a delay() , and does not do any thing
where is the best pales to make a delay so each function doing it's job

char B_path[]={'f','tr','f','ll','f','s'};

Single quotes are for single characters. Please post a picture of your keyboard with the tr key circled.

It is almost certainly unwise to use the delay() function. Much better to use millis() to manage timing as shown in the demo several things at a time.

Also, it is much much easier to build the use of millis() into the program at the start rather than trying to convert from delay() later.

...R

PaulS:

char B_path[]={'f','tr','f','ll','f','s'};

Single quotes are for single characters. Please post a picture of your keyboard with the tr key circled.

Thanks for the tip, because I inattention about it :slight_smile: