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

Hello, I'm using a Nextion display with a DC motor as a fan, with ON (set to 100/255) and OFF buttons, and 25%, 50%, 75% and 100% (of speed) buttons. Trying to get only the ON button the only button to be able to power on the fan. Currently, all but the OFF button can turn on the motor.

I tried this in my void functions, just trialing with the one void fn:

Help1

void bFMinus2PopCallback (void *ptr) {

if (fanSpeed == HIGH) {
  analogWrite(motorPin, 255 / 4); // 25% speed
}
else {
  analogWrite(motorPin, 0);
}`Preformatted text`

fanSpeed is in regards to the ON button speed.

But error message appeared: a function-definition is not allowed here before '{' token

So I guess no conditional statements allowed outside void loop?

Help3


//Nextion official library. Only using a selected few.

#include <doxygen.h>
#include <NexButton.h>
#include <NexConfig.h>
#include <NexCrop.h>
#include <NexGauge.h>
#include <NexHardware.h>
#include <NexHotspot.h>
#include <NexObject.h>
#include <NexPage.h>
#include <NexPicture.h>
#include <NexProgressBar.h>
#include <NexSlider.h>
#include <NexText.h>
#include <Nextion.h>
#include <NexTouch.h>
#include <NexWaveform.h>

//DC motor


int motorPin = 3;
int fanSpeed = 100;


//Declare Fan page buttons
NexButton bON = NexButton(3, 3, "bON");
NexButton bOFF = NexButton(3, 4, "bOFF");
NexButton bMenuF = NexButton(3, 6, "bMenuF");
NexButton bFMinus = NexButton(3, 7, "bFMinus");
NexButton bFPlus = NexButton(3, 8, "bFPlus");
NexButton bFMinus2 = NexButton(3, 9, "bFMinus2");
NexButton bFPlus2 = NexButton(3, 10, "bFPlus2");

NexTouch *nex_listen_list[] =
{
  //Declare Fan page buttons and slider
  &bON,
  &bOFF,
  &bMenuF,
  &bFMinus,
  &bFPlus,
  &bFMinus2,
  &bFPlus2,
  NULL
};

// Touch events

void bONPopCallback(void *ptr) {  // Release event for button
analogWrite(motorPin, fanSpeed);  // Turn ON fan
}
void bOFFPopCallback(void *ptr) { // Release event for button
analogWrite(motorPin, 0);  // Turn OFF fan
}
void bFMinus2PopCallback (void *ptr) {
analogWrite(motorPin, 255 / 4); // 25% speed
}
void bFMinusPopCallback (void *ptr) {
//fanSpeed = fanSpeed - 5; //Minus 5 to current value of counter - Couldn't get this to work.
analogWrite(motorPin, 255 / 2); // 50% speed
}
void bFPlusPopCallback (void *ptr) {
//fanSpeed = fanSpeed + 5; //Plus 5 to current value of counter - Couldn't get this to work.
  analogWrite(motorPin, 255 * 3 / 4); // 75% speed
}
void bFPlus2PopCallback (void *ptr) {

  analogWrite(motorPin, 255); // 100% speed
}



void setup() {
  // put your setup code here, to run once:
  pinMode(motorPin, OUTPUT);
  Serial.begin(9600);
  //Serial.println("Give a number from 50 to 255.");

  //analogWrite(motorPin, 0); //Fan always off when system powered on


  // Register the event callback fns of each touch event
  // Format for press events: <object name>.attachPush(<object name>PushCallback);
  // Format for release events: <object name>.attachPop(<object name>PopCallback);
  bON.attachPop(bONPopCallback);
  bOFF.attachPop(bOFFPopCallback);
  bFMinus.attachPop(bFMinusPopCallback);
  bFPlus.attachPop(bFPlusPopCallback);
  bFMinus2.attachPop(bFMinus2PopCallback);
  bFPlus2.attachPop(bFPlus2PopCallback);
}

void loop() {


  nexLoop (nex_listen_list); //Check for any touch event
}`Preformatted text`

Thanks in advance.
I know there are heaps on questions about the same error, but couldn't find another Nextion user with the same problem.

it's more no code outside a function.

PS/ don't post images of code or text.... do yourself a favour and please read How to get the best out of this forum

Hello

Your bracket count doesn't match. For each { there must be one corresponding }, not more, not less.

In my void function part? It looks fine.

Edit: Oh, like this?

void bFMinus2PopCallback (void *ptr) {
if (fanSpeed == HIGH) 
  analogWrite(motorPin, 255 / 4); // 25% speed
else 
  analogWrite(motorPin, 0);
}

The interpreter complains about the following lines:

I can imagine that he doesn't like the spelling (void * ptr). but I am not sure how it (if wrong) has to be spelled correctly.

// Touch events

void bONPopCallback(void *ptr) {  // Release event for button - Void pointer, allocates memory to previous callback fn.
  analogWrite(motorPin, fanSpeed);  // Turn ON fan
}
void bOFFPopCallback(void *ptr) { // Release event for button
  analogWrite(motorPin, 0);  // Turn OFF fan
}
void bFMinus2PopCallback (void *ptr) {
  if (fanSpeed == HIGH)
    analogWrite(motorPin, 255 / 4); // 25% speed
  else
    analogWrite(motorPin, 0);
}
void bFMinusPopCallback (void *ptr) {
  //fanSpeed = fanSpeed - 5; //Minus 5 to current value of counter - Couldn't get this to work.
  if (fanSpeed == HIGH)
    analogWrite(motorPin, 255 / 2); // 50% speed
  else
    analogWrite(motorPin, 0);
}
void bFPlusPopCallback (void *ptr) {
  //fanSpeed = fanSpeed + 5; //Plus 5 to current value of counter - Couldn't get this to work.
  if (fanSpeed == HIGH)
    analogWrite(motorPin, 255 * 3 / 4); // 75% speed
  else
    analogWrite(motorPin, 0);
}
void bFPlus2PopCallback (void *ptr) {
  if (fanSpeed == HIGH)
    analogWrite(motorPin, 255); // 100% speed
  else
    analogWrite(motorPin, 0);
}

If/else statements works now, but not as intended.
Pressing the 25 - 100% buttons turns the motor off.

hmm....

We could probably be more helpful if you post the whole thing.

there is no such thing as an interpreter. It's a compiler.

post the full code and we'll have a look.

Posted in my original post. I didn't change anything else apart from the { }s.

//Nextion official library. Only using a selected few.

#include <doxygen.h>
#include <NexButton.h>
#include <NexConfig.h>
#include <NexCrop.h>
#include <NexGauge.h>
#include <NexHardware.h>
#include <NexHotspot.h>
#include <NexObject.h>
#include <NexPage.h>
#include <NexPicture.h>
#include <NexProgressBar.h>
#include <NexSlider.h>
#include <NexText.h>
#include <Nextion.h>
#include <NexTouch.h>
#include <NexWaveform.h>

//DC motor


int motorPin = 3;
int fanSpeed = 100;


//Declare Fan page buttons
NexButton bON = NexButton(3, 3, "bON");
NexButton bOFF = NexButton(3, 4, "bOFF");
NexButton bMenuF = NexButton(3, 6, "bMenuF");
NexButton bFMinus = NexButton(3, 7, "bFMinus");
NexButton bFPlus = NexButton(3, 8, "bFPlus");
NexButton bFMinus2 = NexButton(3, 9, "bFMinus2");
NexButton bFPlus2 = NexButton(3, 10, "bFPlus2");

NexTouch *nex_listen_list[] =
{
  //Declare Fan page buttons
  &bON,
  &bOFF,
  &bMenuF,
  &bFMinus,
  &bFPlus,
  &bFMinus2,
  &bFPlus2,
  NULL
};

// Touch events

void bONPopCallback(void *ptr) {  // Release event for button - Void pointer, allocates memory to previous callback fn.
  analogWrite(motorPin, fanSpeed);  // Turn ON fan
}
void bOFFPopCallback(void *ptr) { // Release event for button
  analogWrite(motorPin, 0);  // Turn OFF fan
}
void bFMinus2PopCallback (void *ptr) {
  if (fanSpeed == HIGH)
    analogWrite(motorPin, 255 / 4); // 25% speed
  else
    analogWrite(motorPin, 0);
}
void bFMinusPopCallback (void *ptr) {
  //fanSpeed = fanSpeed - 5; //Minus 5 to current value of counter - Couldn't get this to work.
  if (fanSpeed == HIGH)
    analogWrite(motorPin, 255 / 2); // 50% speed
  else
    analogWrite(motorPin, 0);
}
void bFPlusPopCallback (void *ptr) {
  //fanSpeed = fanSpeed + 5; //Plus 5 to current value of counter - Couldn't get this to work.
  if (fanSpeed == HIGH)
    analogWrite(motorPin, 255 * 3 / 4); // 75% speed
  else
    analogWrite(motorPin, 0);
}
void bFPlus2PopCallback (void *ptr) {
  if (fanSpeed == HIGH)
    analogWrite(motorPin, 255); // 100% speed
  else
    analogWrite(motorPin, 0);
}

void setup() {
  // put your setup code here, to run once:
  pinMode(motorPin, OUTPUT);
  Serial.begin(9600);
  //Serial.println("Give a number from 50 to 255.");

  //analogWrite(motorPin, 0); //Fan always off when system powered on


  // Register the event callback fns of each touch event
  // Format for press events: <object name>.attachPush(<object name>PushCallback);
  // Format for release events: <object name>.attachPop(<object name>PopCallback);
  bON.attachPop(bONPopCallback);
  bOFF.attachPop(bOFFPopCallback);
  bFMinus.attachPop(bFMinusPopCallback);
  bFPlus.attachPop(bFPlusPopCallback);
  bFMinus2.attachPop(bFMinus2PopCallback);
  bFPlus2.attachPop(bFPlus2PopCallback);
}

void loop() {
  nexLoop (nex_listen_list); //Check for any touch event
}

Sorry, yes of course, I actually wanted to write. shame on me.

Fanspeed is 100. You're comparing it to HIGH (1). That is why the motor is being turned off.

I tried 100 too. Still doesn't work though.

Edit: Replacing with 100, the 25 - 100% doesn't turn motor off. But 25 - 100% buttons are able to turn on the motor, which I don't want.

and can you post (within code tags) the full compiler error?

That's not necessary any more since member guix figured out the problem.
Thanks anyways.

Working now on the 25 - 100% not turn on the motor.

So, if 100 == HIGH? Reply #11?

In light of your reply #12, what are you actually trying to do with 'fanspeed' there?

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