Easy questions regarding Setup, Loop standard functions

I am very frustrated. I've got several books and I thought I was following them. But I must not understand something basic. There are many errors in my code but latest says " Compilation error: " Leftdetect' does not name a type." I'm thinking, yes it does, bool! I typed and initialized it in Setup. So I put the variable in setup after I had declared it. Putting the computer's cursor over it the IDE displays a popup box the says it a Variable of type bool with a value of true, typed and initialized in setup. Yep, That's exactly what I did. Then I put the variable between setup and loop and dragged the cursor over it. Now it is type int, no value listed and it was typed in something called public. I don' t even know what public is, much less have used it. Then I put the variable in loop and the cursor also listed it as init, typed in public. Clearly something I don't understand here. I thought a variable typed in setup was typed everywhere and forever more. But no??? Here is my code.

// Today's date: 7/26/2024
// Line following code to operate L/R continous rotation servos and L/R IR line sensors
// Added routine for platform tilt 7/20/2024
// Less than 90 on left is CW on left continuous rotation servo motor.
// greater than 90 on right is CCW on right continuous rotation servo motor.
// 90 is stop on both motors. Adjust trim screw on servos for no motion at stop.
//
// Prototype Function Declarations for compiler. Full declartions later.
// void Straight () {};
// void Left() {};
// void Right() {};
// void No_movement() {};
//
#include <Servo.h>;
Servo Platform;
int N = 0; 
// ************************* SETUP *****************************
void setup()
{ 
  Servo LeftWheel;
  Servo RightWheel;
  bool Leftdetect = true;
  bool Rightdetect = true;
  pinMode(LED_BUILTIN, OUTPUT);  // LED on when platform tilt servo is active.
  // int Platform.attach(11);
  pinMode(9, OUTPUT);   // Left Wheel
  pinMode(10, OUTPUT);  // Right wheel
  pinMode(11, OUTPUT);  // Platform tilt
  // Sensors
  pinMode(6, INPUT);  // Left Line sensor
  pinMode(7, INPUT);  // Right Line sensor

  LeftWheel.attach(9);
  RightWheel.attach(10);
// N is a Marker so we won't call no-motion repeatedly without motion first.
// because no-motion function also activates platform tilt action.
// Since We have never called it before it is initialized to 0.
int N = 0;
//
// END OF SETUP
Leftdetect;
};
//  ************************ LØOP **************************
Leftdetect;
//
void loop(){};
leftdetect
  // 
  // LOOP to read sensors and call appropriate movement functions.
  //
  //
  // Read the Sensors.
  Leftdetect=digitalRead(6);
  Rightdetect = digitalRead(7);
  //
  // 1st test, call Straight if Line not seen in either left or right sensor
  if ((Leftdetect = false) && (Rightdetect == false)) ;
  {; 
    // We are about to do something other than stopping, Set Marker to 0.
    N = 0;
    Straight();  // Call straight Function
   }; // Close 1st test: straight Line 49
  //
  // 2nd test. Line is seen only in left sensor. Remember False is the line.
  if ((Leftdetect == false) && (Rightdetect == true)) {;   
  // (Tracking into line from left, Correct by turning left)
    Left();
    // We are doing something other than stopping, reset marker.
    N = 0;
  }; // Close 2nd Test. Left Turn.
  //
  // 3rd test. Line is seen only in Right sensor.  Remember False is the line.
  if ((Leftdetect == true) && (Rightdetect == false)) {;
    // (Tracking into line from right, Correct by turning right)
    Right();
    // We are doing something other than stopping, reset marker.
    N = 0;
  }; // Close 3rd test. Right Turn. Line 64
  //
  // 4th test, No motion. Line sensed in both sensors, call Stop motion with Platorm tilt
  //
  if ((Leftdetect == true) && (Rightdetect == true)) {;
    // Only proceed with No-movement/ Tilt Platform if marker has been reset
    // to 0 saying we are coming from a movement function call.
    if (N = 0) ; 
      // Proceed with No Movement function call and set the marker!
      No_movement();
      N=1 ;                                                                          
      //
  } // Close 4th test. No Movement/platform tilt. Line 72 
  // ************ End Of Loop/main-program function. Line 41 *****************
  //
  }
//************************ FUNCTION DEFINITIONS ************************
  // Movement Functions: Left side 90 counting up. Right side 90 counting down
  // Straight function defination 0000000000
   void Straight ()
   {
   LeftWheel.write(95); delay(50);
   RightWheel.write(85); delay(50);
   // Left - Stop position plus movement (90 + 5)
   // Right - Stop position minus movement (90 - 5)
   }; // Close straight function definition Line 85 0000000000
   //
   void Left () 
   { // Open Left function defination 0000000000 
   LeftWheel.write(90);delay(50);
   RightWheel.write(85);delay(50);
   // Left servo. No movement just Stop position (90)
   // Right servo. Stop position minus movement (90 - 5)
   }; // Close Left function definition Line 92 0000000000
   void Right ()
   {
   LeftWheel.write(95);delay(50);
   RightWheel.write(90);delay(50); 
   // Left servo. Stop position plus movement (90 + 5)
   // Right servo No movement just Stop position (90)
   };  // Close Right function definition Line 99 
   //
   void No_movement () 
   {
   LeftWheel.write(90);delay(500);
   RightWheel.write(90);delay(500);
   // Left servo. Stop position (90)
   // Right servo. Stop position (90) 
   // Tilt platform by activating the platform tilt servo
   // Turn builtin LED on while platform is active.
   // Platform.write(90);
   digitalWrite(LED_BUILTIN, HIGH);
   delay(5000);
   // Lower the platform by again activating platform tilt servo
   // Platform.write(0);
   // delay(100);
   // Turn builtin LED off after platform is lowered
   digitalWrite(LED_BUILTIN, LOW);
   } // Close No_movement 
};

No. setup and loop are sibling functions. The differences are:

  • setup runs once and first
  • loop runs repeatedly after

In effect, the (hidden) "main" function that calls both is

void main() {
  setup();
  for (;;) {
    loop();
  }
}

So there is nothing magic about setup. If you want a variable to be visible to both, it must be declared globally, outside of any function.

Thanks! So I never want to declare any variable in setup?

what is Leftdetect ? is it you beloved word? you put it everywhere

As with any other function, you can and should declare variables in setup if they are used only there; including being passed to other "child" functions as arguments. In that case, those called functions get their own copy of the variable's value, or a reference/pointer to the original.

You need to study more and start simple, work your way up like everyone else.

// END OF SETUP
Leftdetect; 
};
//  ************************ LØOP **************************
Leftdetect;    ?????????????*****What The
//
void loop(){};
leftdetect
  // 
  // LOOP to read sensors and call appropriate movement functions.
  //
  //
  

```  with this no wonder it doesn't compile

Do you see a difference between "Leftdetect" and "leftdetect"?

The compiler certainly does.

If a variable will ONLY be used in setup, then put it there but yes it's not common.

indent your code, you'll see you made a mess

//  ************************ LØOP **************************
Leftdetect; // <=== WHAT'S THIS ???
//
void loop() {};  // <<== THE LOOP IS EMPTY ?? (and there is no need for the semi colon after a function definition)
leftdetect// <=== WHAT'S THIS ???
//
// LOOP to read sensors and call appropriate movement functions.
//
//
// Read the Sensors.
Leftdetect = digitalRead(6);// <=== THIS IS NOT IN ANY FUNCTION ???
Rightdetect = digitalRead(7);// <=== THIS IS NOT IN ANY FUNCTION ???

Also it's time to read about scope

there are numeous problems


in many cases there are misplaced semicolons, ';'

there's no need for ; after pre-processor directives

a semicolon following an if state represents the body of the statement

braces delimit the body of if statements requiring multiple statements, but there shouldn't be semicolons after the braces

here's an example of what i believe was intended

and what i believe was intended

    if (N == 0)   {
        No_movement();
        N=1 ;
    }

including the common mistake of using "=" instead of "=="


  • similarly, braces delimit the body of a function.

the function should start

void loop()
{
    Leftdetect=digitalRead(6);
     Rightdetect = digitalRead(7);

with statements in the function indented.

the code also had "leftDetect" which is not only not Capitalized, but would have had no effect


as already mentioned, these variables which are properly defined are defined within a function, setup(), which means their 'scope' is within the function and they can't be referenced outside the function.

so they should be defined outside the functions

Servo LeftWheel;
Servo RightWheel;
bool Leftdetect = true;
bool Rightdetect = true;

void setup()
{ 
  pinMode(LED_BUILTIN, OUTPUT); 

the following compiles
it's stripped of comments, making it easier to read
problem code is commented out to contrast it with more correct code

#include <Servo.h>

Servo LeftWheel;
Servo RightWheel;

bool Leftdetect = true;
bool Rightdetect = true;

Servo Platform;
int N = 0;

void setup()
{
    Servo LeftWheel;
    Servo RightWheel;
    pinMode(LED_BUILTIN, OUTPUT);

    pinMode(9, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(11, OUTPUT);

    pinMode(6, INPUT);
    pinMode(7, INPUT);

    LeftWheel.attach(9);
    RightWheel.attach(10);

 // int N = 0;
 // Leftdetect;
};


// Leftdetect;

// void loop(){};
void
loop ()
{

    Leftdetect  = digitalRead(6);
    Rightdetect = digitalRead(7);

 // if ((Leftdetect = false) && (Rightdetect == false)) ;
 // {;

    if ((Leftdetect = false) && (Rightdetect == false))  {
        N = 0;
        Straight();
    }

    if ((Leftdetect == false) && (Rightdetect == true)) {
        Left();
        N = 0;
    }

    if ((Leftdetect == true) && (Rightdetect == false)) {
        Right();
        N = 0;
    }

    if ((Leftdetect == true) && (Rightdetect == true)) {
 //     if (N = 0) ;
 //     No_movement();
 //     N=1 ;
        if (N == 0) {
            No_movement();
            N=1 ;
        }
    }
}

void Straight ()
{
    LeftWheel.write(95);
    delay(50);
    RightWheel.write(85);
    delay(50);
};

void Left ()
{
    LeftWheel.write(90);
    delay(50);
    RightWheel.write(85);
    delay(50);
};

void Right ()
{
    LeftWheel.write(95);
    delay(50);
    RightWheel.write(90);
    delay(50);

};

void No_movement ()
{
    LeftWheel.write(90);
    delay(500);
    RightWheel.write(90);
    delay(500);

    digitalWrite(LED_BUILTIN, HIGH);
    delay(5000);

    digitalWrite(LED_BUILTIN, LOW);
}

// };

there are other issues, to make the code more readable such as avoiding hardcoded pin #s.

and there's no suggestion that the code will work as intended


you may find the C Programming Language helpful. It's co-written by the inventor of the language, not verbose and with very good examples.

As I said in the write-up to the original question those apparently random occurrences of Leftdetect were place in the code simply so the IDE would show me type and value of the varable. A debug help, as it were.

Thanks for you help and a lead on the C programming book.

I am a retired engineer from Texas Instruments. I have programmed in IBM 360 machine language, PL1, FORTRAN, COBAL, and when micros became a thing, BASIC and assembler for lots of different micro's. Sometimes circumstances force me into a "large" project with a no chance for "Hello World" start. I am writing code that will probably be used as an example for middle and high schoolers. This language is not friendly for a person new to it.

where did you read such suggestion?

void setup()
{
    pinMode(LED_BUILTIN, OUTPUT);

    pinMode(11, OUTPUT);

    pinMode(6, INPUT);
    pinMode(7, INPUT);

    LeftWheel.attach(9);
    RightWheel.attach(10);

 // int N = 0;
 // Leftdetect;
};

As I explained, when I am writing the code and put the cursor over the variable a box pops up which contains its type, value, and the line that typed it. That box will also pop up on other names and identify them as variables or functions. With that information I could see a variable change types as I left the setup function. Knowing it would produce that information I would sprinkle the variable name around my code and watch it retain, or not retain, type assignments I thought I had made.

you are informed well about IDE function, not about scope visibility, but now you are aware of it, right?

Of those, I think only PL/1 has a notion of "variable scope." (Maybe COBOL? It was too ... wordy ... for me to get into very much.) So your confusion is somewhat understandable...

On advice of this column I have been reading about "scope" of variables. Tell me this, theoretically if a language has no "scope" and all variables have a unique name does that not solve the problem allowing all variables to be global? I'm not saying to make variables "global" in present day C because it has "scope" but if C had no "scope" would not this unique name feature be a viable solution? I accept "scope" but I'm just not seeing what "scope" buys you in a programming language.