'LED' was not declared in this scope

I'm creating a code for a school project where when the ultrasonic sensor detect a certain range, the number of LEDs will light up and a buzzer will sound. However, when users press on a PushButton, the code will temporarily stop, and the code will continue when PushButton is released. However, I'm unable to figure of the pushbutton part of the code. Please help me to see where the error was.

const int trig = 9;
const int echo = 10;

const int LED1 = A1;
const int LED2 = A2;
const int LED3 = A3;
const int LED4 = A4;

const int PushButton = 11;

const int buzzPin = 12;

long duration;
int distanceCm;
int buttonState;

void setup()
{
pinMode(trig , OUTPUT);
pinMode(echo , INPUT);

pinMode(LED1 , OUTPUT);
pinMode(LED2 , OUTPUT);
pinMode(LED3 , OUTPUT);
pinMode(LED4 , OUTPUT);

pinMode(PushButton , INPUT_PULLUP);

pinMode(buzzPin, OUTPUT);

Serial.begin(9600);
}

void loop()
{
static byte toggle_sw_memmory=0;

//Clears trig
digitalWrite(trig, LOW);
delayMicroseconds(2);
//Sets the trig on HIGH State for 10ms
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
//Read the echo, returns the sound wave travel time in ms
duration = pulseIn(echo, HIGH);
//Caclulating the distance
distanceCm = duration*0.034/2 ;
//Prints the distance on the Serial Monitor
Serial.println("Distance:");
Serial.println(distanceCm);

if(!digitalRead(PushButton))
{
toggle_sw_memmory = !toggle_sw_memmory;

   if(toggle_sw_memmory)
     digitalWrite(LED(), HIGH);
   else
      digitalWrite(LED(), LOW);
   
   while(!digitalRead(PushButton));  

}

void LED()
{
if ( distanceCm <= 35 )
{
digitalWrite(LED1, HIGH);
}
else
{
digitalWrite(LED1, LOW);
}
if ( distanceCm <= 40 )
{
digitalWrite(LED2, HIGH);
}
else
{
digitalWrite(LED2, LOW);
}
if ( distanceCm <= 45 )
{
digitalWrite(LED3, HIGH);
}
else
{
digitalWrite(LED3, LOW);
}
if ( distanceCm <= 50 )
{
digitalWrite(LED4, HIGH);
digitalWrite(buzzPin, HIGH);
}
else
{
digitalWrite(LED4, LOW);
digitalWrite(buzzPin, LOW);
}
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

      digitalWrite(LED(), LOW);

What is this line of code intended to do ?

Another problem.
If you Auto Format your code in the IDE you will see that the LED() function does not start on the left margin

    while (!digitalRead(PushButton));
  }
  void LED()
  {

This is usually an indication of missing (or extra) braces somewhere in the code. For instance, where is the loop() function supposed to end ?

Your LED() function doesn't return a value:

So you can't use it as a value passed to digitalWrite():

Just as a service for you, dear TO (and the others in this thread), here the code in code tags:

const int trig = 9;
const int echo = 10;

const int LED1 = A1;
const int LED2 = A2;
const int LED3 = A3;
const int LED4 = A4;

const int PushButton = 11;

const int buzzPin = 12;

long duration;
int distanceCm;
int buttonState;

void setup()
{
  pinMode(trig , OUTPUT);
  pinMode(echo , INPUT);

  pinMode(LED1 , OUTPUT);
  pinMode(LED2 , OUTPUT);
  pinMode(LED3 , OUTPUT);
  pinMode(LED4 , OUTPUT);

  pinMode(PushButton , INPUT_PULLUP);

  pinMode(buzzPin, OUTPUT);

  Serial.begin(9600);
}

void loop()
{
  static byte toggle_sw_memmory = 0;

  //Clears trig
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  //Sets the trig on HIGH State for 10ms
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  //Read the echo, returns the sound wave travel time in ms
  duration = pulseIn(echo, HIGH);
  //Caclulating the distance
  distanceCm = duration * 0.034 / 2 ;
  //Prints the distance on the Serial Monitor
  Serial.println("Distance:");
  Serial.println(distanceCm);

  if (!digitalRead(PushButton))
  {
    toggle_sw_memmory = !toggle_sw_memmory;

    if (toggle_sw_memmory)
      digitalWrite(LED(), HIGH);
    else
      digitalWrite(LED(), LOW);

    while (!digitalRead(PushButton));
  }

  void LED()
  {
    if ( distanceCm <= 35 )
    {
      digitalWrite(LED1, HIGH);
    }
    else
    {
      digitalWrite(LED1, LOW);
    }
    if ( distanceCm <= 40 )
    {
      digitalWrite(LED2, HIGH);
    }
    else
    {
      digitalWrite(LED2, LOW);
    }
    if ( distanceCm <= 45 )
    {
      digitalWrite(LED3, HIGH);
    }
    else
    {
      digitalWrite(LED3, LOW);
    }
    if ( distanceCm <= 50 )
    {
      digitalWrite(LED4, HIGH);
      digitalWrite(buzzPin, HIGH);
    }
    else
    {
      digitalWrite(LED4, LOW);
      digitalWrite(buzzPin, LOW);
    }
  }

And in addition the closing bracket } for loop() is missing here

(!digitalRead(PushButton))
{
toggle_sw_memmory = !toggle_sw_memmory;

   if(toggle_sw_memmory)
     digitalWrite(LED(), HIGH);
   else
      digitalWrite(LED(), LOW);
   
   while(!digitalRead(PushButton));  
 }
} // <-  This closing bracket for loop() is missing in the original code!

void LED()
{
if ( distanceCm <= 35 )

P.S.: Once you add it and change the digitalWrite(Led(),..) to e.g. LED(); which is not what is intended; it allows to compile the sketch ... So we are one step forward .. :wink:

Ok, that's what you want to do with

 while (!digitalRead(PushButton));

Stop the sketch and go again when the button has been released.

Could you explain what you want to achieve with the following part?
(It is clear that the reaction shall toggle everytime the button is pressed next.)

if (!digitalRead(PushButton))
  {
    toggle_sw_memmory = !toggle_sw_memmory;

    if (toggle_sw_memmory)
      digitalWrite(LED(), HIGH);     // What should really happen here?
    else
      digitalWrite(LED(), LOW);     // And what should really happen here?

    while (!digitalRead(PushButton));  // Wait for button release
  }

So basically the target is if I'm pressing and holding the pushbutton, the ultrasonic would stop detecting thus the LEDs would not light up. However, if the Pushbutton is not pressed, the circuit would continue

After making the changes, it will only detect the range when pushbutton is toggled. currently, my LEDs are not affected by the pushbutton.

Please post your revised code in a new post

A @UKHeliBob already posted, please post your new sketch (in the right format)!

Here's the updated code. After making the changes, it will only detect the range when pushbutton is toggled. currently, my LEDs are not affected by the pushbutton.

const int trig = 9;
const int echo = 10;

const int LED1 = A1;
const int LED2 = A2;
const int LED3 = A3;
const int LED4 = A4;

const int PushButton = 11;

const int buzzPin = 12;

long duration;
int distanceCm;
int buttonState;


void setup() 
{
  pinMode(trig , OUTPUT);
  pinMode(echo , INPUT);
  
  pinMode(LED1 , OUTPUT);
  pinMode(LED2 , OUTPUT);
  pinMode(LED3 , OUTPUT);
  pinMode(LED4 , OUTPUT);
  
  pinMode(PushButton , INPUT_PULLUP);

  pinMode(buzzPin, OUTPUT);
  
  Serial.begin(9600);
}

void loop()
{
  static byte toggle_sw_memmory=0;
  
   //Clears trig
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  //Sets the trig on HIGH State for 10ms
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  //Read the echo, returns the sound wave travel time in ms
  duration = pulseIn(echo, HIGH);
  //Caclulating the distance
  distanceCm = duration*0.034/2 ;
  //Prints the distance on the Serial Monitor
  Serial.println("Distance:");
  Serial.println(distanceCm);
  
  
  if (!digitalRead(PushButton))
  {
       toggle_sw_memmory = !toggle_sw_memmory;
       
       if(toggle_sw_memmory)
         LED();       
       
       while(!digitalRead(PushButton));  //stop the sketch and go again when the button has been released.
  }
}


void LED()
{
   if ( distanceCm <= 35 )
   {
    digitalWrite(LED1, HIGH);
   }
   else
   {
    digitalWrite(LED1, LOW);
   }
   if ( distanceCm <= 40 )
   {
    digitalWrite(LED2, HIGH);
   }
   else
   {
    digitalWrite(LED2, LOW);
   }
   if ( distanceCm <= 45 )
   {
    digitalWrite(LED3, HIGH);
   }
   else
   {
    digitalWrite(LED3, LOW);
   }
   if ( distanceCm <= 50 )
   {
    digitalWrite(LED4, HIGH);
    digitalWrite(buzzPin, HIGH); 
   }
   else
   {
    digitalWrite(LED4, LOW);
    digitalWrite(buzzPin, LOW);
   }
}

Thanks, that is already a good step forward :wink:

I have not corrected your code (because I assume you want to learn rather than having someone else solving your task, don't you? I have restructured it without changing the program logic to look like this (only real change: I made toggle_sw_memmory a global boolean variable that fits better to how you use it):

const int trig = 9;
const int echo = 10;

const int LED1 = A1;
const int LED2 = A2;
const int LED3 = A3;
const int LED4 = A4;

const int PushButton = 11;

const int buzzPin = 12;

long duration;
int distanceCm;
int buttonState;
boolean toggle_sw_memmory = false;


void setup() 
{
  pinMode(trig , OUTPUT);
  pinMode(echo , INPUT);
  
  pinMode(LED1 , OUTPUT);
  pinMode(LED2 , OUTPUT);
  pinMode(LED3 , OUTPUT);
  pinMode(LED4 , OUTPUT);
  
  pinMode(PushButton , INPUT_PULLUP);

  pinMode(buzzPin, OUTPUT);
  
  Serial.begin(9600);
}

void GetDistance(){
   //Clears trig
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  //Sets the trig on HIGH State for 10ms
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  //Read the echo, returns the sound wave travel time in ms
  duration = pulseIn(echo, HIGH);
  //Caclulating the distance
  distanceCm = duration*0.034/2 ;
  //Prints the distance on the Serial Monitor
  Serial.println("Distance:");
  Serial.println(distanceCm);
}

void LED()
{
   if ( distanceCm <= 35 ) { digitalWrite(LED1, HIGH);}
					  else { digitalWrite(LED1, LOW); }
   
   if ( distanceCm <= 40 ){ digitalWrite(LED2, HIGH);} 
					 else { digitalWrite(LED2, LOW); }
   
   if ( distanceCm <= 45 ) { digitalWrite(LED3, HIGH);} 
					  else { digitalWrite(LED3, LOW); }
   
   if ( distanceCm <= 50 ) { digitalWrite(LED4, HIGH);     
							 digitalWrite(buzzPin, HIGH); }
					  else { digitalWrite(LED4, LOW);
							 digitalWrite(buzzPin, LOW);}
}

void HandleButton(){
  if (!digitalRead(PushButton))
  {
       toggle_sw_memmory = !toggle_sw_memmory;
       
       if(toggle_sw_memmory) LED();       
       
       while(!digitalRead(PushButton));  //stop the sketch and go again when the button has been released.
 }
}


void loop()
{
  GetDistance();  
  HandleButton();
}

As you may see the code is broken up into four more simple to read routines:

  • setup()
  • GetDistance()
  • LED() (which I would prefer to name HandleLEDs() because that is naming what it does, but for the sake of not changing too much ...)
  • HandleButton()

One step of Debugging could be to check the routines separately:

Does HandleButton() do what you want? You may for example replace the LED() routine by a routine that just prints on Serial and make sure that HandleButton() works correctly.

Does LED() do what you intend? You may change setup() to call LED() for example like this


void setup() 
{
  pinMode(trig , OUTPUT);
  pinMode(echo , INPUT);
  
  pinMode(LED1 , OUTPUT);
  pinMode(LED2 , OUTPUT);
  pinMode(LED3 , OUTPUT);
  pinMode(LED4 , OUTPUT);
  
  pinMode(PushButton , INPUT_PULLUP);

  pinMode(buzzPin, OUTPUT);
  
  Serial.begin(9600);
  
// ------------------------------------------------------
  Serial.println("Start of my Test");
  for (distanceCm = 100; distanceCm > 10; distanceCm -= 10){ 
    Serial.println(distanceCm);
    LED();
    Serial.println("Wait for Enter");
    char c = ' ';
    while (c != char(13)) {
       if (Serial.available()) c = Serial.read();
      };
   }
  Serial.println("End of my Test");
  while(1);  // Your sketch stops here 
// ------------------------------------------------------
}

Check it out and good luck :wink:

Here I have updated the toggle_sw_memmory a global boolean variable. This lead to a 'toggle_sw_memmory" was not declared in this scope.

const int trig = 9;
const int echo = 10;

const int LED1 = A1;
const int LED2 = A2;
const int LED3 = A3;
const int LED4 = A4;

const int PushButton = 11;

const int buzzPin = 12;

long duration;
int distanceCm;
int buttonState;
boolean toggele_sw_memmory =false;


void setup() 
{
  pinMode(trig , OUTPUT);
  pinMode(echo , INPUT);
  
  pinMode(LED1 , OUTPUT);
  pinMode(LED2 , OUTPUT);
  pinMode(LED3 , OUTPUT);
  pinMode(LED4 , OUTPUT);
  
  pinMode(PushButton , INPUT_PULLUP);

  pinMode(buzzPin, OUTPUT);
  
  Serial.begin(9600);
}

void loop()
{
  GetDistance();
  HandleButton();
  
}


void LED()
{
   if ( distanceCm <= 35 )
   {
    digitalWrite(LED1, HIGH);
   }
   else
   {
    digitalWrite(LED1, LOW);
   }
   if ( distanceCm <= 40 )
   {
    digitalWrite(LED2, HIGH);
   }
   else
   {
    digitalWrite(LED2, LOW);
   }
   if ( distanceCm <= 45 )
   {
    digitalWrite(LED3, HIGH);
   }
   else
   {
    digitalWrite(LED3, LOW);
   }
   if ( distanceCm <= 50 )
   {
    digitalWrite(LED4, HIGH);
    digitalWrite(buzzPin, HIGH); 
   }
   else
   {
    digitalWrite(LED4, LOW);
    digitalWrite(buzzPin, LOW);
   }
}

void GetDistance ()
{
  //Clears trig
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  //Sets the trig on HIGH State for 10ms
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  //Read the echo, returns the sound wave travel time in ms
  duration = pulseIn(echo, HIGH);
  //Caclulating the distance
  distanceCm = duration*0.034/2 ;
  //Prints the distance on the Serial Monitor
  Serial.println("Distance:");
  Serial.println(distanceCm);
}

void HandleButton()
{
   if (!digitalRead(PushButton))
  {
       toggle_sw_memmory = !toggle_sw_memmory;
       
       if(toggle_sw_memmory)
       {
         LED();       
         Serial.println("Program running"); //test if code is running
       }

       else
       {
          Serial.println("Program not running"); //test if code is not running
       }       
       
       while(!digitalRead(PushButton));  //stop the sketch and go again when the button has been released.
  }
}

boolean toggele_sw_memmory = false;

Check the spelling of the variable name

If you get an error like this, carefully highlight the entity name in the error message (in this case "toggle_sw_memmory") and copy it (do not retype it) and paste it into the search tool.

If you can't find it in your source, you have a simple spelling error.
If you can find it, then you really do have some sort of scope error.

Hi @jokaikai, you have got already the hints in the posts from @UKHeliBob and @TheMemberFormerlyKnownAsAWOL !

If you copy the sketch I have provided, it should run without this problem :wink: However it will still not do what you want ... As a next step after the correction of the typo in the variable name I recommend to have a look at the alternative setup() routine I posted also.

Testing routines separately is a need for beginners and the "magic" of experienced programmers who create highly complex software: They break things down to readable and easy testable parts, make them work, put them together step by step (sometimes called the "integration phase") and solve the remaining integration problems.

Just check it out ...