Timer not defined in scope

While compiling iam getting error which says the timer not defined help me


#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// WiFi credentials
char ssid[] = "your_SSID";
char password[] = "your_PASSWORD";

// Blynk auth token
char auth[] = "your_AUTH_TOKEN";


#define SENSOR_PIN   A0   // Proximity sensor pin
#define BUZZER_PIN   D8  // Buzzer pin

// Keep track of the state of the bin (empty or full)
bool binEmpty = true;

// Keep track of the last time the buzzer was triggered
unsigned long lastBuzzTime = 0;

// Timer object to trigger buzzer
BlynkTimer timer;


int rotor_PWM = 6;  // Digital/PWM pin 6 to the LPWM on the BTS7960

// Enable rotor movement
int rotor_EN = 7;  // connect Digital/PWM pin 7 to L_EN on the BTS7960


int stop1 = 0;

int stop2 = 1;

int back1 = 2;

int back2 = 3;

int pwm1 = 4;

int pwm2 = 5;




void setup() {
  // Initialize serial port for debugging
  Serial.begin(9600);

  // Connect to WiFi network
  Blynk.begin(auth, ssid, password);

 // Connect to WiFi network
  WiFi.begin("your_wifi_ssid", "your_wifi_password");
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  // Initialize the Blynk IoT platform with the auth token
  Blynk.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str());

  // Initialize the proximity sensor pin as an input
  pinMode(SENSOR_PIN, INPUT_PULLUP);

  // Initialize the buzzer pin as an output
  pinMode(BUZZER_PIN, OUTPUT);

  // Set up the timer to trigger every 5 seconds
  timer.setInterval(5000L, timerFunction);

pinMode(stop1, OUTPUT);

  pinMode(stop2, OUTPUT);

  pinMode(back1, OUTPUT);

  pinMode(back2, OUTPUT);

  pinMode(pwm1, OUTPUT);

  pinMode(pwm2, OUTPUT);
  
  
  pinMode(rotor_PWM, OUTPUT);//rotor pin setup

  pinMode(rotor_EN, OUTPUT);

  
}

void loop() {
  Blynk.run();
  timer.run();
}



BLYNK_WRITE(V0)//FORWARD

{

  if (param.asInt() == 1) {

    forward();

    Stop();
  }
}

BLYNK_WRITE(V1)//BACKWARD

{

  if (param.asInt() == 1) {

    backward();

    Stop();
  }
}

BLYNK_WRITE(V2)//LEFT

{

  if (param.asInt() == 1) {

    left();

    delay(2900);

    Stop();
  }
}

BLYNK_WRITE(V3)//RIGHT

{

  if (param.asInt() == 1) {

    right();

    delay(2900);

    Stop();
  }
}

BLYNK_WRITE(V4)//STOP

{

  if (param.asInt() == 1) {

    Stop();
  }
}

BLYNK_WRITE(V6)//SPEED MODE

{

  if (param.asInt() == 1) {

    Sforward();
  }



}

BLYNK_WRITE(V7) // 180 degree

{

  if (param.asInt() == 1){


   cright();

   delay(5900);

    Stop();
}
}


BLYNK_WRITE(V8) // ROTOR START

{

  if (param.asInt() == 1){
   rotor_start();

  
}
}

BLYNK_WRITE(V9) // ROTOR STOP

{

  if (param.asInt() == 1){

   rotor_stop() ; 
  
}
}




void Stop()

{

  analogWrite(pwm1, 0);

  analogWrite(pwm2, 0);
}



void left(){

  analogWrite(pwm1, 200);

  digitalWrite(back1, LOW);

  digitalWrite(back2, LOW);

  analogWrite(pwm2, 200);

  digitalWrite(back1, LOW);

  digitalWrite(back2, LOW);

}



void right() {

  analogWrite(pwm1, 200);

  digitalWrite(back1, HIGH);

  analogWrite(pwm2, 200);

  digitalWrite(back2, HIGH);


}

void cright() {             //180 degree

  analogWrite(pwm1, 200);

  digitalWrite(back1, HIGH);

  analogWrite(pwm2, 200);

  digitalWrite(back2, HIGH);
}





void backward() {

  analogWrite(pwm1, 200);

  digitalWrite(back1, LOW);

  digitalWrite(back2, LOW);

  analogWrite(pwm2, 200);

  digitalWrite(back2, HIGH);
}



void forward() {

  analogWrite(pwm2, 200);

  digitalWrite(back1, LOW);

  digitalWrite(back2, LOW);

  analogWrite(pwm1, 200);

  digitalWrite(back1, HIGH); 
}
void Sforward() {

  analogWrite(pwm2, 70);

  digitalWrite(back1, LOW);

  digitalWrite(back2, LOW);

  analogWrite(pwm1, 70);

  digitalWrite(back1, HIGH); 

  

void rotor_start() {

  analogWrite(rotor_PWM, 255);

  digitalWrite(rotor_EN, HIGH);
}

void rotor_stop() {

  analogWrite(rotor_PWM,0);

  digitalWrite(rotor_EN, LOW);

 
}


void timerFunction() {
  // Read the state of the proximity sensor
  int sensorState = digitalRead(SENSOR_PIN);

  // If the sensor is triggered (object in front of it)
  if (sensorState == LOW) {
    // If the bin was empty before, set the bin state to full, and send a message to the Blynk app
    if (binEmpty) {
      binEmpty = false;

      // Send the message to the Blynk app
      Blynk.virtualWrite(V0, "Bin inserted");
    }

    // Reset the buzzer trigger timer
    lastBuzzTime = 0;
  } else {
    // If the sensor is not triggered (no object in front of it)
    // If the bin was full before, set the bin state to empty, and send a message to the Blynk app
    if (!binEmpty) {
      binEmpty = true;

  
    }
}
 // Check if it's time to trigger the buzzer
  if (millis() - lastBuzzTime > 5000L && binEmpty) {
    // Trigger the buzzer twice and send a message to the Blynk app
    digitalWrite(BUZZER_PIN, HIGH);
    delay(100);
    digitalWrite(BUZZER_PIN, LOW);
    delay(100);
    digitalWrite(BUZZER_PIN, HIGH);
    delay(100);

    // Set the last buzzer trigger time to the current time
    lastBuzzTime = millis();
  }
}

Which line of code is highlighted when this happens?

Pete.

"’
timer.setInterval(5000L, timerFunction);
"’
This line shows error

} is missing

1 Like

Yes but the error comes on timer the timer have so connection with this void function

Yes but bracket is missing!
Your editor don’t know exactly where is the error.

β€˜β€™β€™
timer.setInterval(5000L, timerFunction)
β€˜β€™β€™

Ok you decide to ignore my advice , you can wait till your death :joy:

1 Like

The timer part is not compiling correct way I dont know what to do

Add the missing closing bracket!!

Pete.

2 Likes
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// WiFi credentials
char ssid[] = "your_SSID";
char password[] = "your_PASSWORD";

// Blynk auth token
char auth[] = "your_AUTH_TOKEN";


#define SENSOR_PIN   A0   // Proximity sensor pin
#define BUZZER_PIN   D8  // Buzzer pin

// Keep track of the state of the bin (empty or full)
bool binEmpty = true;

// Keep track of the last time the buzzer was triggered
unsigned long lastBuzzTime = 0;

// Timer object to trigger buzzer
BlynkTimer timer;


int rotor_PWM = 6;  // Digital/PWM pin 6 to the LPWM on the BTS7960

// Enable rotor movement
int rotor_EN = 7;  // connect Digital/PWM pin 7 to L_EN on the BTS7960


int stop1 = 0;

int stop2 = 1;

int back1 = 2;

int back2 = 3;

int pwm1 = 4;

int pwm2 = 5;




void setup() {
  // Initialize serial port for debugging
  Serial.begin(9600);

  // Connect to WiFi network
  Blynk.begin(auth, ssid, password);

 // Connect to WiFi network
  WiFi.begin("your_wifi_ssid", "your_wifi_password");
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  // Initialize the Blynk IoT platform with the auth token
  Blynk.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str());

  // Initialize the proximity sensor pin as an input
  pinMode(SENSOR_PIN, INPUT_PULLUP);

  // Initialize the buzzer pin as an output
  pinMode(BUZZER_PIN, OUTPUT);

  // Set up the timer to trigger every 5 seconds
  timer.setInterval(5000L, timerFunction);

pinMode(stop1, OUTPUT);

  pinMode(stop2, OUTPUT);

  pinMode(back1, OUTPUT);

  pinMode(back2, OUTPUT);

  pinMode(pwm1, OUTPUT);

  pinMode(pwm2, OUTPUT);
  
  
  pinMode(rotor_PWM, OUTPUT);//rotor pin setup

  pinMode(rotor_EN, OUTPUT);

  
}

void loop() {
  Blynk.run();
  timer.run();
}



BLYNK_WRITE(V0)//FORWARD

{

  if (param.asInt() == 1) {

    forward();

    Stop();
  }
}

BLYNK_WRITE(V1)//BACKWARD

{

  if (param.asInt() == 1) {

    backward();

    Stop();
  }
}

BLYNK_WRITE(V2)//LEFT

{

  if (param.asInt() == 1) {

    left();

    delay(2900);

    Stop();
  }
}

BLYNK_WRITE(V3)//RIGHT

{

  if (param.asInt() == 1) {

    right();

    delay(2900);

    Stop();
  }
}

BLYNK_WRITE(V4)//STOP

{

  if (param.asInt() == 1) {

    Stop();
  }
}

BLYNK_WRITE(V6)//SPEED MODE

{

  if (param.asInt() == 1) {

    Sforward();
  }



}

BLYNK_WRITE(V7) // 180 degree

{

  if (param.asInt() == 1){


   cright();

   delay(5900);

    Stop();
}
}


BLYNK_WRITE(V8) // ROTOR START

{

  if (param.asInt() == 1){
   rotor_start();

  
}
}

BLYNK_WRITE(V9) // ROTOR STOP

{

  if (param.asInt() == 1){

   rotor_stop() ; 
  
}
}




void Stop()

{

  analogWrite(pwm1, 0);

  analogWrite(pwm2, 0);
}



void left(){

  analogWrite(pwm1, 200);

  digitalWrite(back1, LOW);

  digitalWrite(back2, LOW);

  analogWrite(pwm2, 200);

  digitalWrite(back1, LOW);

  digitalWrite(back2, LOW);

}



void right() {

  analogWrite(pwm1, 200);

  digitalWrite(back1, HIGH);

  analogWrite(pwm2, 200);

  digitalWrite(back2, HIGH);


}

void cright() {             //180 degree

  analogWrite(pwm1, 200);

  digitalWrite(back1, HIGH);

  analogWrite(pwm2, 200);

  digitalWrite(back2, HIGH);
}





void backward() {

  analogWrite(pwm1, 200);

  digitalWrite(back1, LOW);

  digitalWrite(back2, LOW);

  analogWrite(pwm2, 200);

  digitalWrite(back2, HIGH);
}



void forward() {

  analogWrite(pwm2, 200);

  digitalWrite(back1, LOW);

  digitalWrite(back2, LOW);

  analogWrite(pwm1, 200);

  digitalWrite(back1, HIGH); 
}
void Sforward() {

  analogWrite(pwm2, 70);

  digitalWrite(back1, LOW);

  digitalWrite(back2, LOW);

  analogWrite(pwm1, 70);

  digitalWrite(back1, HIGH); 
}
  

void rotor_start() {

  analogWrite(rotor_PWM, 255);

  digitalWrite(rotor_EN, HIGH);
}

void rotor_stop() {

  analogWrite(rotor_PWM,0);

  digitalWrite(rotor_EN, LOW);

 
}


void timerFunction() {
  // Read the state of the proximity sensor
  int sensorState = digitalRead(SENSOR_PIN);

  // If the sensor is triggered (object in front of it)
  if (sensorState == LOW) {
    // If the bin was empty before, set the bin state to full, and send a message to the Blynk app
    if (binEmpty) {
      binEmpty = false;

      // Send the message to the Blynk app
      Blynk.virtualWrite(V0, "Bin inserted");
    }

    // Reset the buzzer trigger timer
    lastBuzzTime = 0;
  } else {
    // If the sensor is not triggered (no object in front of it)
    // If the bin was full before, set the bin state to empty, and send a message to the Blynk app
    if (!binEmpty) {
      binEmpty = true;

  
    }
}
 // Check if it's time to trigger the buzzer
  if (millis() - lastBuzzTime > 5000L && binEmpty) {
    // Trigger the buzzer twice and send a message to the Blynk app
    digitalWrite(BUZZER_PIN, HIGH);
    delay(100);
    digitalWrite(BUZZER_PIN, LOW);
    delay(100);
    digitalWrite(BUZZER_PIN, HIGH);
    delay(100);

    // Set the last buzzer trigger time to the current time
    lastBuzzTime = millis();
  }
}

This is what are they are suggesting to do. " } " missing bracket.

2 Likes