Interrupt error

I am attempting to merge a couple of sketches for my weather station project that I found online.

Each of them now seem to run great on their own but upon merging them I keep getting an error upon compiling:

’isr_rotation’ was not declared in this scope

This is my first attempt with interrupts.

I have triple checked the position of each line and tried placing them in differing positions but I get the same result.

Any suggestions are appreciated, I’m still pretty low on the learning curve!

#include <Wire.h>
#include "Adafruit_HTU21DF.h"
#include "Adafruit_MCP9808.h"
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include "Arduino.h"
#include <SoftwareSerial.h>
#include <math.h>

unsigned long delayTime;

Adafruit_HTU21DF htu = Adafruit_HTU21DF(); // Create the HTU21DF temp-rh sensor object
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808(); // Create the MCP9808 temperature sensor object
Adafruit_BME280 bme; // I2C

#if defined(ARDUINO_ARCH_SAMD) || defined(__SAM3X8E__)
// use pin 18 with Due, pin 1 with Zero or M0 Pro
#define lcd Serial1
#else


SoftwareSerial lcd = SoftwareSerial(0, 2); // Creates a software serial port
#endif


#define WindSensorPin (18) // The pin location of the anemometer sensor 

volatile unsigned long Rotations; // cup rotation counter used in interrupt routine
volatile unsigned long ContactBounceTime; // Timer to avoid contact bounce in interrupt routine

float WindSpeed; // speed miles per hour

void setup() { //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 {
  Serial.begin(9600);
  lcd.begin(9600);

  pinMode(WindSensorPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(WindSensorPin), isr_rotation, FALLING);
 
  Serial.println("Vortex Wind Speed Test");
  Serial.println("Rotations\tMPH");
 }
  //-----------------------------------------------------

  // set the size of the display if it isn't 16x2 (you only have to do this once)
  lcd.write(0xFE);
  lcd.write(0xD1);
  lcd.write(16);  // 16 columns
  lcd.write(2);   // 2 rows
  delay(10);

  // set the contrast, 200 is a good place to start, adjust as desired
  lcd.write(0xFE);
  lcd.write(0x50);
  lcd.write(220);
  delay(10);

  // set the brightness - we'll max it (255 is max brightness)
  lcd.write(0xFE);
  lcd.write(0x99);
  lcd.write(255);
  delay(10);


  Serial.println("HTU21D-F  RH stated accuracy +- 2% between 5 and 95% of range = 1% @ 50% RH");
  Serial.println("MCP9808   Temperature stated accuracy +- .25% over range of -40C to +125C = .2C at 82.5C... yea right.");
  Serial.println("bme280 Pressure stated accuracy +- 1 hPa = 0.0002953 inHg");
  Serial.println("");

  bool status;

  if (!htu.begin()) {
    Serial.println("Couldn't find sensor!");
    while (1);
  }
  if (!tempsensor.begin()) {
    Serial.println("Couldn't find MCP9808!");
    while (1);
  }
  status = bme.begin();
  if (!status) {
    Serial.println("Could not find a valid BME280 sensor!");
    while (1);
  }
  delay(500); // let sensors boot up

  //----------------------------------------------------------


}

void loop() { //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  uint8_t red, green, blue;

  {
  Rotations = 0; // Set Rotations count to 0 ready for calculations

  sei(); // Enables interrupts
  delay (3000); // Wait 3 seconds to average
  cli(); // Disable interrupts

  // convert to mp/h using the formula V=P(2.25/T)
  // V = P(2.25/3) = P * 0.75

  WindSpeed = Rotations * 0.75;

  Serial.print(Rotations); Serial.print("\t\t");
  Serial.println(WindSpeed);

   }
// This is the function that the interrupt calls to increment the rotation count
  void isr_rotation () {

    if ((millis() - ContactBounceTime) > 15 ) { // debounce the switch contact.
      Rotations++;
      ContactBounceTime = millis();

    }

  Serial.print("htu21DF   RH:       ");
  Serial.print(htu.readHumidity());
  Serial.println(" % ");
  lcd.print(htu.readHumidity());
  lcd.print("% RH");
  delay(2000);
  lcd.write(0xFE);
  lcd.write(0x58); // Clear Screen


  // Read and print out the mcp9808 temperature, then convert to *F
  float c = tempsensor.readTempC();
  float f = c * 9.0 / 5.0 + 32;

  Serial.print("mcp9808 Temp:   ");
  Serial.print(c); Serial.print("*C\t    ");
  Serial.print(f); Serial.println("*F");
  lcd.print(f);
  lcd.print(" Deg F");
  delay(2000);
  lcd.write(0xFE);
  lcd.write(0x58); // Clear Screen


  // Pressure is returned in the SI units of Pascals. 100 Pascals = 1 hPa = 1 millibar.
  // For future reference 1 pascal =0.000295333727 inches of mercury, or 1 inch Hg = 3386.39 Pascal.

  Serial.print("bme280 Pressure:     ");
  Serial.print(bme.readPressure() * 0.000295333727);
  Serial.println(" inHg");
  lcd.print(bme.readPressure() * 0.000295333727);
  lcd.println(" inHg");
  delay(2000);
  lcd.write(0xFE);
  lcd.write(0x58); // Clear Screen

  
   

  Serial.println("");
  Serial.println(" == == == == == == = ");
  Serial.println("");

  
      
  }

It looks like a curly brace problem. Your ISR seems to be inside the loop.

As an additional comment, you shouldn't use delays or serial prints inside the ISR.

Thanks saximus. Do you mean the segment

{
  Rotations = 0; // Set Rotations count to 0 ready for calculations

  sei(); // Enables interrupts
  delay (3000); // Wait 3 seconds to average
  cli(); // Disable interrupts

  // convert to mp/h using the formula V=P(2.25/T)
  // V = P(2.25/3) = P * 0.75

  WindSpeed = Rotations * 0.75;

  Serial.print(Rotations); Serial.print("\t\t");
  Serial.println(WindSpeed);

   }

(containing noted delay) should be located it Setup?

OK I tried that to no avail but then I removed both brackets and that error is gone - Good, but I still don't understand why...

You have a mismatch between { and }.

In the IDE, use menu tools->autoformat. It will show you what goes wrong,

void loop()
{

  uint8_t red, green, blue;

  {

    ...
    ...

  }
  // This is the function that the interrupt calls to increment the rotation count
  void isr_rotation ()
  {
    ...
    ...
  }

After an autoformat, functions should always start at the beginning of a line. So the above shows that your isr_rotation() function is inside the loop() function. You seem to be missing a } at the end of loop(). If you remove the { after uint8_t red, green, blue; , your problem is (probably) solved.

Also in setup()

void setup()
{
  {
    Serial.begin(9600);
    lcd.begin(9600);

    pinMode(WindSensorPin, INPUT);
    attachInterrupt(digitalPinToInterrupt(WindSensorPin), isr_rotation, FALLING);

    Serial.println("Vortex Wind Speed Test");
    Serial.println("Rotations\tMPH");
  }
  //-----------------------------------------------------

  ...
  ...
}

Although the extra { } don't cause problems, the don't need to be there.