function-definition not allowed?

I'm using a sketch provided by Dronebot Workshop and I get the following error message:

a function-definition is not allowed here before '{' token

I have the proper libraries installed and I'm using the sketch provided that seems to work for him in the video. Here's the sketch.

#include <Wire.h>

// Include Adafruit Graphics & OLED libraries
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// Include Adafruit AM2320 Temp Humid Library
#include <Adafruit_AM2320.h>

// Reset pin not used but needed for library
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

// Define object am2320
Adafruit_AM2320 am2320 = Adafruit_AM2320();

void setup() {
  // Start Wire library for I2C
  Wire.begin();
void setup() {
  
  // initialize OLED with I2C addr 0x3C
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  
  // Initialize Temp & Humid Sensor
  am2320.begin();
}

void displayTempHumid(){
  // Delay to allow sensor to stabalize
  delay(2000);

  // Read Humidity
  float h = am2320.readHumidity();
  // Read temperature as Celsius
  float t = am2320.readTemperature();

  // Clear the display
  display.clearDisplay();
  //Set the color - always use white despite actual display color
  display.setTextColor(WHITE);
  //Set the font size
  display.setTextSize(1);
  //Set the cursor coordinates
  display.setCursor(0,0);
  display.print("DroneBot Workshop");
  display.setCursor(0,10); 
  display.print("Humidity:    "); 
  display.print(h);
  display.print(" %");
  display.setCursor(0,20);
  display.print("Temperature: "); 
  display.print(t);
  display.print(" C");
}
void loop() {
  displayTempHumid();
  display.display();
}

You’ve got the setup() function declaration twice...

 void setup() {
  // Start Wire library for I2C
  Wire.begin();
void setup() { <=== OOOOPSSS

one is enough :wink:

 void setup() {
  // Start Wire library for I2C
  Wire.begin();

  // initialize OLED with I2C addr 0x3C
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  
  // Initialize Temp & Humid Sensor
  am2320.begin();
}

Thank you! Did not see that, though this is how he provided the sketch.

Cut and paste is sometimes the enemy of productivity 8)

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