Go Down

Topic: function-definition not allowed? (Read 77 times) previous topic - next topic

coffeeowl88

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.

Code: [Select]
#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();
}

J-M-L

#1
Jan 24, 2021, 12:59 am Last Edit: Jan 24, 2021, 01:02 am by J-M-L
You've got the setup() function declaration  twice...
Code: [Select]
void setup() {
  // Start Wire library for I2C
  Wire.begin();
void setup() { <=== OOOOPSSS
 

one is enough ;)
Code: [Select]
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();
}
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

coffeeowl88

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

J-M-L

Cut and paste is sometimes the enemy of productivity 8)
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

Go Up