Issue with function call and scope in loop()

Well here is my code :

#include <Servo.h>                           // Include servo library
 
Servo servoLeft;                             // Declare left and right servos
Servo servoRight;
int button=2;
int led=11; 
int d3=3; 
int d4=4;
int d5=5;
int d6=6;
int d7=7;
int s1=0;
int s2=0;
int s3=0;
int s4=0;
int s5=0;


void setup()                                 // Built-in initialization block
{ 
   Serial.begin(9600);
   pinMode( button, INPUT_PULLUP);
   pinMode(led, OUTPUT);
   pinMode( d7, INPUT);                         // Set right whisker pin to input
   pinMode( d6, INPUT);  
   pinMode( d5, INPUT);                         // Set right whisker pin to input
   pinMode( d4, INPUT);  
   pinMode( d3, INPUT); 
   servoLeft.writeMicroseconds(1480);         // Left wheel counterclockwise
   servoRight.writeMicroseconds(1472);
  servoLeft.attach(12);                      // Attach left signal to P12 
  servoRight.attach(13);                     // Attach right signal to P13
  
  //disableServos();                           // Stay still indefinitely
}  
 
void loop()                                  // Main loop auto-repeats
{         
         
  int sensor1 = digitalRead(d7);          //Rightmost
  int sensor2 = digitalRead(d6);
  int sensor3 = digitalRead(d5);          //middle
  int sensor4 = digitalRead(d4);
  int sensor5 = digitalRead(d3);         //Lefttmost


  //Serial.println(sensor5);
  //Serial.println(sensor4);
  //Serial.println(sensor3);
  //Serial.println(sensor2);
  //Serial.println(sensor1);
  //forward();delay(2000);
  
  if(sensor2==LOW){turnRight();}                               //--->line follower segment begins 
  if(sensor4==LOW) {turnLeft();}
  if(sensor2==LOW && sensor3==LOW ){turnRight();}
  if(sensor4==LOW && sensor3==LOW ){turnLeft();}
  if(sensor3==LOW) {forward();}                                //----> line follower segment ends
  //delay(1000);
 // if(sensor1==LOW){ ++s1;}
 // if(sensor2==LOW){ ++s2;} 
  //if(sensor3==LOW){ ++s3;} 
  //if(sensor4==LOW){ ++s4;} 
 // if(sensor5==LOW){ ++s5;}  
  //forward(2000);turnLeft(1000);forward(4000);turnRight(1000);disableServos();
  //disableServos();
  //if(s5==0){turnRight();}
  //if(s3=1){forward();}
  
   formatPrint("s5=",s5);
   formatPrint("s4=",s4);
   formatPrint("s3=",s3);
   formatPrint("s2=",s2);
   formatPrint("s1=",s1);
     
   int buttonState = digitalRead(button);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(led, HIGH);
  } else {
    // turn LED off:
    digitalWrite(led, LOW);
  }
  

 // delay(100);
}

void forward()                       // forward function
{
     servoLeft.writeMicroseconds(1700);         // Left wheel counterclockwise
     servoRight.writeMicroseconds(1300); }
    // Right wheel clockwise
  //delay(time);                               // Maneuver for time ms


void turnLeft()                      // Left turn function
{
  servoLeft.writeMicroseconds(1300);         // Left wheel clockwise
  servoRight.writeMicroseconds(1300);        // Right wheel clockwise
  //delay(time);                               // Maneuver for time ms
}

void turnRight()                     // Right turn function
{
  servoLeft.writeMicroseconds(1700);         // Left wheel counterclockwise
  servoRight.writeMicroseconds(1700);        // Right wheel counterclockwise
  //delay(time);                               // Maneuver for time ms
}

void backward()                      // backward function
{
  servoLeft.writeMicroseconds(1300);         // Left wheel clockwise
  servoRight.writeMicroseconds(1700);        // Right wheel counterclockwise
  //delay(time);                               // Maneuver for time ms
}
void formatPrint( char *leftStr, int MyVar1){
     Serial.println (leftStr);
     Serial.println (MyVar1);
     }

void stay()                     // Right turn function
{
  servoLeft.writeMicroseconds(1480);         // Left wheel counterclockwise
  servoRight.writeMicroseconds(1472);        // Right wheel counterclockwise
  //delay(time);                               // Maneuver for time ms
}
void disableServos()                         // Halt servo signals
{                                            
  servoLeft.detach();                      // Stop sending servo signals
  servoRight.detach(); 
}

So my question is that Can ii make a function for the line follower outside the loop and call it ?..It tried to do that but it won’t work properly…I think mainly because of the digitalRead part which is being done in the loop… So is there anyway around it ?

p.s…-Please ignore the commented lines it’s just for my reference.

It tried to do that but it won't work properly...

If we knew what you tried we may be able to help. "it won't work properly" conveys on real information that we can use to help you. What does the code actually do? How is that different from what you want?

Were there compile errors. What were the errors? If so, please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

This will prevent the "arduino_modified_sketch_668161\sketch_feb07a.ino:70:24: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
formatPrint("s5=",s5)" warning

void formatPrint( const char *leftStr, int MyVar1){  // **** added const

The sensor1 .. sensor5 variables are local to the loop (their scope) so they won’t be known outside the loop() function. If you write another function which depends on using those values you either need to pass the values as parameters, or make the variable global in scope (declared at the start of the code)

// global variables
  int sensor1;          //Rightmost
  int sensor2;
  int sensor3;          //middle
  int sensor4;
  int sensor5;         //Lefttmost
...
void setup() {...}

void loop()                                  // Main loop auto-repeats
{         
         
  sensor1 = digitalRead(d7);          //Rightmost
  sensor2 = digitalRead(d6);
  sensor3 = digitalRead(d5);          //middle
  sensor4 = digitalRead(d4);
  sensor5 = digitalRead(d3);         //Lefttmost

...

Yes, I did the passing the variables as arguments and now it's working fine. Thanks.

Great

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.