Unofficial Nextion/TJC User Forum

Using a slider to set the speed of a fan

I’m making a testing station for air purifier device and am using a couple of sensors to test the air quality. The station should have two fans which should be controlled using a Nextion slider.

The sliderPopCallBack function won’t work,I’ve tried couple of different methods but to no avail. I don’t know why the functions wont work since I copied them from the other project I found on the web. The functions are supposed to take the slider value and assing it to the speed of the fans. The whole code is in the github link I gave. Also I dont get any error messeges, the fans just won’t move when I move the sliders.

The code is down below

#include <SoftwareSerial.h>
#include <Wire.h> 
#include "Nextion.h"
#include "DHT.h"
#include "MQ135.h"
#include "Adafruit_SGP30.h"
#include <ArduinoJson.h>  
#define DHTPIN 12
#define PIN_MQ135 A0
MQ135 mq135_sensor(PIN_MQ135);
#define DHTTYPE DHT22  
Adafruit_SGP30 sgp;

SoftwareSerial HMISerial(10, 11);


float temperature;           //pocetne vrjednosti
float Airq;
float eCO2;
float TVOC;

int number;
int number2;

//fan 1 
#define fan1 9
#define motor1pin1  2
#define motor1pin2  3
// fan2
#define fan2 10
#define motor2pin1  4
#define motor2pin2  5

int MotorSpeed1;
int MotorSpeed2;

// Initialize DHT sensor.
 DHT dht(DHTPIN, DHTTYPE);

//DEKLALIRANJE NEXTION ELEMENATA//
 NexText CO2 = NexText(0, 6, "CO2");
 NexText AIRQ = NexText(0, 4, "AIRQ");
 NexText dust = NexText(0, 3, "dust");
 NexText temp =NexText(0, 2, "temp");
 NexText vents = NexText(1, 2, "vents");
 NexText vent1 = NexText(1, 4, "vent1");
 NexText vent2 = NexText(1, 3, "vent2");
 NexButton bnext1= NexButton(0,7,"bnext1");
 NexButton back= NexButton(1,7,"back");
 NexSlider slider1= NexSlider(1,5,"slider1");
 NexSlider slider2= NexSlider(1,6,"slider2");
 NexButton tvoc= NexButton(0,8,"tvoc");
 NexNumber n0=NexNumber(0,9,"n0");

NexTouch *nex_listen_list[] = { //lista elemenata osljetljivih na touch
  &slider1,
  &slider2,
  NULL
};

////kod za uoravljanje sa ventilatorima, triba za drugi slider isto ucinit////
void slider1PopCallback(void *ptr) 
{
  uint32_t number = 0;
  slider1.getValue(&number);
  MotorSpeed1=number;
 
}
void slider2PopCallback(void *ptr) {  ////////////DRUGI SLIDER/////////
  uint32_t number2 = 0;
  slider2.getValue(&number2);
  MotorSpeed2=number2;
  }


void setup(){
  slider1.attachPop(slider1PopCallback);      //ovdi san attacha popcallback funkcije na slajdere
  slider2.attachPop(slider2PopCallback);
  nexInit();
  dht.begin();                                //pocetak rada senzora, mog7u li samo stavit dht.begin(mogu lool)
  Serial.begin(9600);

  pinMode(motor1pin1, OUTPUT);
  pinMode(motor1pin2, OUTPUT);
  pinMode(motor2pin1, OUTPUT);
  pinMode(motor2pin2, OUTPUT);

  pinMode(9, OUTPUT); 
  pinMode(10, OUTPUT);
  
}
 void loop(void) {   

  
   nexLoop(nex_listen_list);
   
analogWrite(9, MotorSpeed1); //ENA pin
  analogWrite(10, MotorSpeed2); //ENB pin

  //Controlling spin direction of motors:
  digitalWrite(motor1pin1, HIGH);
  digitalWrite(motor1pin2, LOW);

  digitalWrite(motor2pin1, HIGH);
  digitalWrite(motor2pin2, LOW);
  delay(1000);
  

  //digitalWrite(motor1pin1, LOW);
  //digitalWrite(motor1pin2, HIGH);

  //digitalWrite(motor2pin1, LOW);
  //digitalWrite(motor2pin2HIGH);
  //delay(1000);
  
 float temperature = dht.readTemperature(); //OCITANJA SA SNZORA DHT22
 float Airq= mq135_sensor.getPPM();       //MQ
 float eCO2= sgp.eCO2;             //SGP
 float TVOC= sgp.TVOC;             //SGP

 sendTemperature();
 sendAirq();
 sendeCO2();
 sendTVOC();
}

void sendTemperature(){                                                         //funckije za slanje vrjednosti na displey
  String command = "tem.txt=\""+String(temperature,1)+"\"";  
  Serial.print(command);
  endNextionCommand();
  }

void sendAirq(){
  float Airq= mq135_sensor.getPPM();
  String command="qual.txt=\"" + String(Airq,1) + "\"";
  Serial.print(command);
  endNextionCommand();
  }

   
void sendeCO2(){
  String command="co2.txt=\""+String(eCO2,1)+"\"";
  Serial.print(command);
  endNextionCommand();
  }  

void sendTVOC(){
  String command="t0.txt=\""+String(TVOC,1)+"\"";
  Serial.print(command);
  endNextionCommand();
  }  


void endNextionCommand()
{
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
}

This forum is in no way affiliated with NEXTION®, ITEAD STUDIO®, TJC®, or anyone else really. All product names, logos, and brands are property of their respective owners. All company, product, and service names used in this website are for identification purposes only. Use of these names, logos, and brands does not imply endorsement from the respective rights holder(s).