ERROR: Expected '}' before 'else'

Hello everyone. I am an Arduino beginner and am doing an assignment about using the HX711 load cell to control a WS2182 led to light up under different weights. Weird thing is, it shows that there is an error and I don't know how to fix this.
One more thing is, I manage to light the led up but idk how to turn it off while in other weight condition.
If anyone would like to help me, I would be grateful.

/*
   -------------------------------------------------------------------------------------
   HX711_ADC
   Arduino library for HX711 24-Bit Analog-to-Digital Converter for Weight Scales
   Olav Kallhovd sept2017
   -------------------------------------------------------------------------------------
*/

/*
   Settling time (number of samples) and data filtering can be adjusted in the config.h file
   For calibration and storing the calibration value in eeprom, see example file "Calibration.ino"

   The update() function checks for new data and starts the next conversion. In order to acheive maximum effective
   sample rate, update() should be called at least as often as the HX711 sample rate; >10Hz@10SPS, >80Hz@80SPS.
   If you have other time consuming code running (i.e. a graphical LCD), consider calling update() from an interrupt routine,
   see example file "Read_1x_load_cell_interrupt_driven.ino".

   This is an example sketch on how to use this library
*/

#include <HX711_ADC.h>
#if defined(ESP8266)|| defined(ESP32) || defined(AVR)
#include <EEPROM.h>
#endif

#include <FastLED.h>   //led library
#define LED_PIN     7
#define NUM_LEDS    20

//pins:
const int HX711_dout = 4; //mcu > HX711 dout pin
const int HX711_sck = 5; //mcu > HX711 sck pin

//HX711 constructor:
HX711_ADC LoadCell(HX711_dout, HX711_sck);

const int calVal_eepromAdress = 0;
unsigned long t = 0;

CRGB leds[NUM_LEDS];

void setup() {
  Serial.begin(57600); delay(10);
  Serial.println();
  Serial.println("Starting...");

  LoadCell.begin();
  //LoadCell.setReverseOutput(); //uncomment to turn a negative output value to positive
  float calibrationValue; // calibration value (see example file "Calibration.ino")
  calibrationValue = 696.0; // uncomment this if you want to set the calibration value in the sketch
#if defined(ESP8266)|| defined(ESP32)
  //EEPROM.begin(512); // uncomment this if you use ESP8266/ESP32 and want to fetch the calibration value from eeprom
#endif
  //EEPROM.get(calVal_eepromAdress, calibrationValue); // uncomment this if you want to fetch the calibration value from eeprom

  unsigned long stabilizingtime = 2000; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time
  boolean _tare = true; //set this to false if you don't want tare to be performed in the next step
  LoadCell.start(stabilizingtime, _tare);
  if (LoadCell.getTareTimeoutFlag()) {
    Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
    while (1);
  }
  else {
    LoadCell.setCalFactor(calibrationValue); // set calibration value (float)
    Serial.println("Startup is complete");
  }
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS); //led setup
}

void loop() {
  static boolean newDataReady = 0;
  const int serialPrintInterval = 0; //increase value to slow down serial print activity

  // check for new data/start next conversion:
  if (LoadCell.update()) { newDataReady = true; }

  // get smoothed value from the dataset:
  if (newDataReady) {}
    if (millis() > t + serialPrintInterval) {}
      float i = LoadCell.getData();
      Serial.print("A");
      Serial.println(i);
      newDataReady = 0;
      t = millis();

//control led with scale value:
  if (i < 100 && i >98) { //This is the weight condition i need led lights up
    for (int e = 0; e <= 19; e++) {
    leds[e] = CRGB ( 150, 0, 255);
    FastLED.show();
    delay(500);
    FastLED.clear();
    delay(40);
  }
} 

  // receive command from serial terminal, send 't' to initiate tare operation:
  if (Serial.available() > 0) {
   char inByte = Serial.read();
   if (inByte == 't') LoadCell.tareNoDelay(); {
 }
}

  // check if last tare operation is complete:
  if (LoadCell.getTareStatus() == true) {
    Serial.println("Tare complete");
  }
}


Welcome to the forum
If you have posted your complete sketch then your use of curly brace pairs is seriously messed up. Every opening brace should have a corresponding closing brace

Have you posted your complete sketch ?

Please post the full error message copied using the convenient button in the IDE

1 Like

I suggest that you use tools /autoformat in the IDE to properly indent your code; this will not solve your problem but will show you that you are missing 4 } and might make it easier to find where you made the mistake. Each { needs to have a matching }.

1 Like

Hi i am sort of a beginner too but i think i know what is wrong.

 if (Serial.available() > 0) {
    char inByte = Serial.read();
    if (inByte == 't') LoadCell.tareNoDelay(); {
  }

you need another } you have yo have a } for each { you have 2 { and 1 }
a fix would be.

if (Serial.available() > 0) {
   char inByte = Serial.read();
   if (inByte == 't') LoadCell.tareNoDelay(); {
 }
}

i am not sure if i am right but you might want to try it.

4 Likes

The solution by @zortpeq should solve the problem.
It may be easier to remove the last { instead of adding a } to complete an empty {}...

1 Like

I am not convinced that the full sketch has been posted but if it has then there are at least 6 missing closing braces

  1. then end of the loop() function
  2. after if (LoadCell.update())
  3. after if (newDataReady)
  4. after if (millis() > t + serialPrintInterval)
  5. after if (i = 100) NOTE this statement is wrong anyway
  6. after the else associated with 5. above
2 Likes

Hey Bob, that's the full sketch. Thank you for your correction.

Ty zort! I'll try it and tell you if it's work or not later.

Sry zort, I don't think it's a solution, the error still exist.

Bob, i was trying to correct them follow your guidance but the error still exist. I update the code, could you please check it again for me? Thank you!

Please do not post pictures of code, post it in code tags

try this:

/*
   -------------------------------------------------------------------------------------
   HX711_ADC
   Arduino library for HX711 24-Bit Analog-to-Digital Converter for Weight Scales
   Olav Kallhovd sept2017
   -------------------------------------------------------------------------------------
*/

/*
   Settling time (number of samples) and data filtering can be adjusted in the config.h file
   For calibration and storing the calibration value in eeprom, see example file "Calibration.ino"

   The update() function checks for new data and starts the next conversion. In order to acheive maximum effective
   sample rate, update() should be called at least as often as the HX711 sample rate; >10Hz@10SPS, >80Hz@80SPS.
   If you have other time consuming code running (i.e. a graphical LCD), consider calling update() from an interrupt routine,
   see example file "Read_1x_load_cell_interrupt_driven.ino".

   This is an example sketch on how to use this library
*/

#include <HX711_ADC.h>
#if defined(ESP8266)|| defined(ESP32) || defined(AVR)
#include <EEPROM.h>
#endif

#include <FastLED.h>   //led library
#define LED_PIN     7
#define NUM_LEDS    20

//pins:
const int HX711_dout = 4; //mcu > HX711 dout pin
const int HX711_sck = 5; //mcu > HX711 sck pin

//HX711 constructor:
HX711_ADC LoadCell(HX711_dout, HX711_sck);

const int calVal_eepromAdress = 0;
unsigned long t = 0;

CRGB leds[NUM_LEDS];

void setup() {
  Serial.begin(57600); delay(10);
  Serial.println();
  Serial.println("Starting...");

  LoadCell.begin();
  //LoadCell.setReverseOutput(); //uncomment to turn a negative output value to positive
  float calibrationValue; // calibration value (see example file "Calibration.ino")
  calibrationValue = 696.0; // uncomment this if you want to set the calibration value in the sketch
#if defined(ESP8266)|| defined(ESP32)
  //EEPROM.begin(512); // uncomment this if you use ESP8266/ESP32 and want to fetch the calibration value from eeprom
#endif
  EEPROM.get(calVal_eepromAdress, calibrationValue); // uncomment this if you want to fetch the calibration value from eeprom

  unsigned long stabilizingtime = 2000; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time
  boolean _tare = true; //set this to false if you don't want tare to be performed in the next step
  LoadCell.start(stabilizingtime, _tare);
  if (LoadCell.getTareTimeoutFlag()) {
    Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
    while (1);
  }
  else {
    LoadCell.setCalFactor(calibrationValue); // set calibration value (float)
    Serial.println("Startup is complete");
  }
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS); //led setup
}

void loop() {
  static boolean newDataReady = 0;
  const int serialPrintInterval = 0; //increase value to slow down serial print activity

  // check for new data/start next conversion:
  if (LoadCell.update()) {
    newDataReady = true;
  }

  // get smoothed value from the dataset:
  if (newDataReady) {}
  if (millis() > t + serialPrintInterval) {}
  float i = LoadCell.getData();
  Serial.print("A");
  Serial.println(i);
  newDataReady = 0;
  t = millis();

  //control led with scale value:
  if (i = 100) { //This is the weight condition i need led lights up
    for (int e = 0; e <= 19; e++) {
      leds[e] = CRGB ( 150, 0, 255);
      FastLED.show();
      delay(40);
      FastLED.clear();
    }


    // receive command from serial terminal, send 't' to initiate tare operation:
    if (Serial.available() > 0) {
      char inByte = Serial.read();
      if (inByte == 't') LoadCell.tareNoDelay(); {
      }
    }

    // check if last tare operation is complete:
    if (LoadCell.getTareStatus() == true) {
      Serial.println("Tare complete");
    }
  }
}
1 Like

Bob, i found the error and corrected it. Another question is, could you please tell me how can i constrain the 'i' with two conditions? Cuz i need the led lights up(last about 3s or 5s) when the load cell read 100 and then off.

Thank you sooooo much rilviana! Now i got anothe question is, could you please tell me how can i constrain the 'i' with two conditions? Cuz i need the led lights up(last about 3s or 5s) when the load cell read 100 and then off.

What do you want do do here?

if (newDataReady) {} (line 80)

and here?

" if (millis() > t + serialPrintInterval) {} " (line 81)

Those are code inside this library examples, i just left them there in case if i del something then it may go wrong.

To delete part of some code, you need to know what you are doing.

It seems like you have little experience with the C language.

I recommend that you study more about the C language and also understand what the libraries you intend to use in your projects do.

I'll try to modify your code to do what I assume you want it to do, but maybe only tomorrow.

1 Like

Thank you for your kindness!

Not unless you post your latest, error corrected sketch

I repost the latest code in post #1 already so you can check it now :slight_smile: