Hi guys, I'm doing a project in which I have to control both a motor and a thermistor (for PID control) via bluetooth. The 2 programs work separately but when I put them together I really don't know why they won't work. I attach the code
#include <SoftwareSerial.h>
#include <NTC_Thermistor.h>
#include <SmoothThermistor.h>
#include <PID_v1.h>
SoftwareSerial miBT(10,11);
char DATO;
int der = 8;
int izq = 6;
int speed = 190;
#define KP 17.07
#define KI 0.75
#define KD 96.57
#define SMOOTHING_WINDOW 5
#define MOSFET_GATE_PIN 3
#define THERMISTOR_PIN A5
NTC_Thermistor* thermistor = new NTC_Thermistor(THERMISTOR_PIN, 10000, 100000, 25, 3950);
SmoothThermistor* sthermistor = new SmoothThermistor(thermistor, SMOOTHING_WINDOW);
bool pOnM = false;
double pwmOut, curTemp, setTemp = 180;
PID pid(&curTemp, &pwmOut, &setTemp, KP, KI, KD, P_ON_E, DIRECT);
void setup(void) {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(MOSFET_GATE_PIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
pid.SetMode(AUTOMATIC);
}
void loop(void) {
curTemp = sthermistor->readCelsius();
digitalWrite(LED_BUILTIN, abs(curTemp-setTemp)/setTemp <= 0.01);
if (!pOnM && abs(curTemp-setTemp) <= max(curTemp, setTemp)*0.2) {
pOnM = true;
pid.SetTunings(KP, KI, KD, P_ON_M);
Serial.println("P_ON_M activated.");
}
pid.Compute();
if (curTemp < 300) {
analogWrite(MOSFET_GATE_PIN, pwmOut);
}
else {
Serial.println("Thermal overrun detected!");
analogWrite(MOSFET_GATE_PIN, 0);
}
Serial.print("pwmOut = "); Serial.print(pwmOut); Serial.print(", ");
Serial.print("diff = "); Serial.print(setTemp - curTemp); Serial.print(", ");
Serial.print("curTemp = "); Serial.print(round(curTemp));
Serial.print(" / "); Serial.println(round(setTemp));
delay(500);
if (miBT.available()){
DATO = miBT.read();
if (DATO == 'A'){
setTemp = 215;
}
if (DATO == 'B'){
digitalWrite(der,LOW);
analogWrite(izq, speed);
}
if (DATO == 'C'){
setTemp = 215;
}
if (DATO == 'D'){
digitalWrite(der, LOW);
analogWrite(izq, speed);
}
if (DATO == 'E'){
digitalWrite(der, LOW);
analogWrite(izq, LOW);
}
if (DATO == 'F'){
setTemp = 0;
}
}
}
Won't work... What does it look like? Is there smoke coming, fire or explosion?
Hi @thiagolamoglia ,
welcome to the forum.
Well done translating your posting to english and posting it as a code-section.
Please post the two separate codes that work.
Post links to the libraries that you are using
Post which exact type of microcontroller you are using.
Post a description of what the program is doing.
You start the program and then you should see some output in the serial monitor.
Post what you get in the serial monitor
What details can be observed?
What do you expect the code to do = what should the code do?
As you did not provide links to the libraries I can't compile your code
Here is a code-version with more serial debug-output to make visible what is going on in your code
#include <SoftwareSerial.h>
#include <NTC_Thermistor.h>
#include <SmoothThermistor.h>
#include <PID_v1.h>
SoftwareSerial miBT(10, 11);
char DATO;
int der = 8;
int izq = 6;
int speed = 190;
#define KP 17.07
#define KI 0.75
#define KD 96.57
#define SMOOTHING_WINDOW 5
#define MOSFET_GATE_PIN 3
#define THERMISTOR_PIN A5
NTC_Thermistor* thermistor = new NTC_Thermistor(THERMISTOR_PIN, 10000, 100000, 25, 3950);
SmoothThermistor* sthermistor = new SmoothThermistor(thermistor, SMOOTHING_WINDOW);
bool pOnM = false;
double pwmOut, curTemp, setTemp = 180;
PID pid(&curTemp, &pwmOut, &setTemp, KP, KI, KD, P_ON_E, DIRECT);
void setup(void) {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(MOSFET_GATE_PIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
pid.SetMode(AUTOMATIC);
}
void loop(void) {
curTemp = sthermistor->readCelsius();
digitalWrite(LED_BUILTIN, abs(curTemp - setTemp) / setTemp <= 0.01);
if (!pOnM && abs(curTemp - setTemp) <= max(curTemp, setTemp) * 0.2) {
pOnM = true;
pid.SetTunings(KP, KI, KD, P_ON_M);
Serial.println("P_ON_M activated.");
}
pid.Compute();
if (curTemp < 300) {
analogWrite(MOSFET_GATE_PIN, pwmOut);
}
else {
Serial.println("Thermal overrun detected!");
analogWrite(MOSFET_GATE_PIN, 0);
}
Serial.print("pwmOut = ");
Serial.print(pwmOut); Serial.print(", ");
Serial.print("diff = ");
Serial.print(setTemp - curTemp);
Serial.print(", ");
Serial.print("curTemp = ");
Serial.print(round(curTemp));
Serial.print(" / ");
Serial.println(round(setTemp));
delay(500);
if (miBT.available()) {
DATO = miBT.read();
Serial.print("byte received DATO=");
Serial.println(DATO);
if (DATO == 'A') {
setTemp = 215;
Serial.print("A received setTemp to");
Serial.println(setTemp);
}
if (DATO == 'B') {
digitalWrite(der, LOW);
analogWrite(izq, speed);
Serial.print("B received digitalWrite(der, LOW) analogWrite(izq, speed)=");
Serial.println(speed);
}
if (DATO == 'C') {
setTemp = 215;
Serial.print("A received setTemp to ");
Serial.println(setTemp);
}
if (DATO == 'D') {
digitalWrite(der, LOW);
analogWrite(izq, speed);
Serial.print("D received digitalWrite(der, LOW) analogWrite(izq, speed)=");
Serial.println(speed);
}
if (DATO == 'E') {
digitalWrite(der, LOW);
analogWrite(izq, LOW);
Serial.print("E received digitalWrite(der, LOW) analogWrite(izq, LOW)");
}
if (DATO == 'F') {
setTemp = 0;
Serial.print("F received setTemp to ");
Serial.println(setTemp);
}
}
}
best regards Stefan
i don't see code driving a motor.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.