...was not declared in this scope

I'm very new to coding and trying to finish a project for my engineering class but im having issues with the code. I used a basic pir sensor connected to a servo motor but the code wasn't working at first. After attempting to fix it I still haven't been able to figure out what to do... Could someone help?

This is the original code

void setup() {
  // put your setup code here, to run once:
/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-motion-sensor-servo-motor
 */
 
#include <Servo.h>
 
// constants won't change
const int MOTION_SENSOR_PIN = 7; // Arduino pin connected to motion sensor's pin
const int SERVO_PIN         = 9; // Arduino pin connected to servo motor's pin
 
Servo servo; // create servo object to control a servo
 
// variables will change:
int angle = 0;          // the current angle of servo motor
int lastMotionState;    // the previous state of motion sensor
int currentMotionState; // the current state of motion sensor
 
void setup() {
  Serial.begin(9600);                // initialize serial
  pinMode(MOTION_SENSOR_PIN, INPUT); // set arduino pin to input mode
  servo.attach(SERVO_PIN);           // attaches the servo on pin 9 to the servo object
 
  servo.write(angle);
  currentMotionState = digitalRead(MOTION_SENSOR_PIN);
}
 
void loop() {
  lastMotionState    = currentMotionState;             // save the last state
  currentMotionState = digitalRead(MOTION_SENSOR_PIN); // read new state
 
  if (currentMotionState == LOW && lastMotionState == HIGH) { // pin state change: LOW -> HIGH
    Serial.println("Motion detected!");
    servo.write(90);
  }
  else
  if (currentMotionState == HIGH && lastMotionState == LOW) { // pin state change: HIGH -> LOW
    Serial.println("Motion stopped!");
    servo.write(0);
  }
}
 
 


And this is my current code.

void setup() {
  // put your setup code here, to run once:
/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-motion-sensor-servo-motor
 */
 
 
#include <Servo.h>
 
 
// constants won't change
const int MOTION_SENSOR_PIN = 7; // Arduino pin connected to motion sensor's pin
const int SERVO_PIN         = 9; // Arduino pin connected to servo motor's pin
 
 
Servo servo; // create servo object to control a servo
 
 
// variables will change:
int angle = 0;          // the current angle of servo motor
int lastMotionState;    // the previous state of motion sensor
int currentMotionState; // the current state of motion sensor
 
 
 setup() ;{
  Serial.begin(9600);                // initialize serial
  pinMode(MOTION_SENSOR_PIN, INPUT); // set arduino pin to input mode
  servo.attach(SERVO_PIN);           // attaches the servo on pin 9 to the servo object
 
 
  servo.write(angle);
  currentMotionState = digitalRead(MOTION_SENSOR_PIN);
}
 


Hi and welcome,

It would help a lot if you can provide the full error.

Please post your complete sketch rather than just a part of it

setup() ;{

What is that semicolon doing there ?

Not only the semicolon I'm afraid.

1 Like

Im sorry, im not really sure how to use this but ill fix it once i find out. that was one of the error messages

Compilation error: expected ' ; ' before ' { ' token

Somehow you have put the above both in the example and (I guess) you copied the example so the error got duplicated into your own code. The first line with void setup should be removed, and the other line with just setup etc should be replaced with void setup() {

Please post the full sketch that caused the error in the first place before you tried to fix it by inserting the semicolon. Are you trying to use 2 functions named setup() in the sketch ?

This is the full error message, sorry I'm kinda clueless.

C:\Users\nsyoung\AppData\Local\Temp\.arduinoIDE-unsaved2023926-19272-1rwwmce.fw5\sketch_oct26a\sketch_oct26a.ino: In function 'void loop()':
C:\Users\nsyoung\AppData\Local\Temp\.arduinoIDE-unsaved2023926-19272-1rwwmce.fw5\sketch_oct26a\sketch_oct26a.ino:49:3: error: 'lastMotionState' was not declared in this scope
   lastMotionState    = currentMotionState;             // save the last state
   ^~~~~~~~~~~~~~~
C:\Users\nsyoung\AppData\Local\Temp\.arduinoIDE-unsaved2023926-19272-1rwwmce.fw5\sketch_oct26a\sketch_oct26a.ino:49:24: error: 'currentMotionState' was not declared in this scope
   lastMotionState    = currentMotionState;             // save the last state
                        ^~~~~~~~~~~~~~~~~~
C:\Users\nsyoung\AppData\Local\Temp\.arduinoIDE-unsaved2023926-19272-1rwwmce.fw5\sketch_oct26a\sketch_oct26a.ino:50:36: error: 'MOTION_SENSOR_PIN' was not declared in this scope
   currentMotionState = digitalRead(MOTION_SENSOR_PIN); // read new state
                                    ^~~~~~~~~~~~~~~~~
C:\Users\nsyoung\AppData\Local\Temp\.arduinoIDE-unsaved2023926-19272-1rwwmce.fw5\sketch_oct26a\sketch_oct26a.ino:55:5: error: 'servo' was not declared in this scope
     servo.write(90);
     ^~~~~
C:\Users\nsyoung\AppData\Local\Temp\.arduinoIDE-unsaved2023926-19272-1rwwmce.fw5\sketch_oct26a\sketch_oct26a.ino:55:5: note: suggested alternative: 'perror'
     servo.write(90);
     ^~~~~
     perror
C:\Users\nsyoung\AppData\Local\Temp\.arduinoIDE-unsaved2023926-19272-1rwwmce.fw5\sketch_oct26a\sketch_oct26a.ino:60:5: error: 'servo' was not declared in this scope
     servo.write(0);
     ^~~~~
C:\Users\nsyoung\AppData\Local\Temp\.arduinoIDE-unsaved2023926-19272-1rwwmce.fw5\sketch_oct26a\sketch_oct26a.ino:60:5: note: suggested alternative: 'perror'
     servo.write(0);
     ^~~~~
     perror
C:\Users\nsyoung\AppData\Local\Temp\.arduinoIDE-unsaved2023926-19272-1rwwmce.fw5\sketch_oct26a\sketch_oct26a.ino: At global scope:
C:\Users\nsyoung\AppData\Local\Temp\.arduinoIDE-unsaved2023926-19272-1rwwmce.fw5\sketch_oct26a\sketch_oct26a.ino:64:1: error: expected declaration before '}' token
 }
 ^

exit status 1

Compilation error: 'lastMotionState' was not declared in this scope


Have you posted the complete sketch that caused those errors ?

Yes, the second sketch.

This is the problem tho:

I am not convinced. Apart from any other problems, of which there are many, the second sketch has a missing terminating curly brace so errors in a different way

Copied and edited the sketch:

  // put your setup code here, to run once:
/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-motion-sensor-servo-motor
 */
 
 
#include <Servo.h>
 
 
// constants won't change
const int MOTION_SENSOR_PIN = 7; // Arduino pin connected to motion sensor's pin
const int SERVO_PIN         = 9; // Arduino pin connected to servo motor's pin
 
 
Servo servo; // create servo object to control a servo
 
 
// variables will change:
int angle = 0;          // the current angle of servo motor
int lastMotionState;    // the previous state of motion sensor
int currentMotionState; // the current state of motion sensor
 
 
void setup() {
  Serial.begin(9600);                // initialize serial
  pinMode(MOTION_SENSOR_PIN, INPUT); // set arduino pin to input mode
  servo.attach(SERVO_PIN);           // attaches the servo on pin 9 to the servo object
 
 
  servo.write(angle);
  currentMotionState = digitalRead(MOTION_SENSOR_PIN);
}

Now the error(s) are down to this:

/tmp/ccv0iLsL.ltrans0.ltrans.o: In function `main':
/home/mike/.arduino15/packages/ATTinyCore/hardware/avr/1.5.2/cores/tiny/main.cpp:10: undefined reference to `loop'
collect2: error: ld returned 1 exit status
Multiple libraries were found for "Servo.h"
  Used: /home/mike/.arduino15/packages/ATTinyCore/hardware/avr/1.5.2/libraries/Servo
  Not used: /home/mike/.arduino15/libraries/Servo
Using library Servo at version 1.1.2 in folder: /home/mike/.arduino15/packages/ATTinyCore/hardware/avr/1.5.2/libraries/Servo 
exit status 1

Compilation error: exit status 1

You have two "setup" functions which can't work.
In order for any sketch to work you need:

void setup(){*some code that happens at begining, after turning on*},

after this you need

void loop(){*code that runs repeadetly, after code from void setup() is done*}.

Also note that you need opening bracket "{" and closing bracket "}" after each function, you forgot to put closing bracket after first "setup" function.
You should watch some basic tutorials on how to write arduino code, it would help you faster.

I'm not entirely sure if this is what you're looking for, but I was able to make it compile by replacing:

 setup() ;{

with

void setup() {

You also need to add a loop function for it to compile succesfully.

In the original code

i dont think that it should be up here. try putting this at the end of code inside "void loop():"
there is also a tutorial at random nerds tutorial about the pir sensors, that could help.

And for the second code

it would be just "void setup(){//something here }"
also there isn't "void loop()".
write the main program in loop() . the part in setup() will run only once and will run as soon as board is powered.

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