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:
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?
//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.