It looks like it, but why is it that all in the code?
You're the only one who can tell if the output is wrong. We don't know what condition you were testing.
Get it to print its inputs and intermediate calculations. Try to "exercise" it to produce all possible outputs in your tests.
I simulated the operation of this code based on the application - as you can see everything works as it should. It is very strange why Arduino does not work on the platrum.
app.zip (794 KB)
Automatyk:
I simulated the operation of this code based on the application - as you can see everything works as it should. It is very strange why Arduino does not work on the platrum.
It sounds like reality is not producing the same data as your simulation. Could your input sensor be broken?
#define LED_INFO_DEAD_ZONE 1
Depending on the board, Pin 1 may be used for the serial output. Using it in conjunction with the serial monitor will cause problems.
It sounds like reality is not producing the same data as your simulation. Could your input sensor be broken?
The sensor definitely works well because in the same program, but without the hysteresis in the code (with the dead zone itself), everything works as it should.
#define LED_INFO_DEAD_ZONE 1
Depending on the board, Pin 1 may be used for the serial output. Using it in conjunction with the serial monitor will cause problems.
I changed the PIN - unfortunately, the diode from the dead zone is still on.
Automatyk:
I changed the PIN - unfortunately, the diode from the dead zone is still on.
Down in your code, in the getNewState section, you have this:
if (value >= stateXLeftMin && value <= stateXLeftMax)
return stateX;
if (value >= stateXRightMin && value <= stateXRightMax)
return stateX;
In your getNewState, you return StateX for the dead zones, but never use it in the switch statement.
switch (newState) {
case stateA:
setStateA();
break;
case stateB:
setStateB();
break;
case stateC:
setStateC();
break;
}
Not sure if this helps ...
In your getNewState, you return StateX for the dead zones, but never use it in the switch statement.
Code: [Select]
switch (newState) {
case stateA:
setStateA();
break;
case stateB:
setStateB();
break;
case stateC:
setStateC();
break;
}Not sure if this helps ...
You have provided a piece of my code - what should I change? Strange is Arduino - why it can not work ![]()
If the goal is to have the dead zone include X-B-X, then change
if (value >= stateXLeftMin && value <= stateXLeftMax)
return stateX;
if (value >= stateXRightMin && value <= stateXRightMax)
return stateX;
to
if (value >= stateXLeftMin && value <= stateXLeftMax)
return stateC;
if (value >= stateXRightMin && value <= stateXRightMax)
return stateC;
Somewhere in here you have your states crossed ... if A is too dry, then increase humidity. If C is too humid, then decrease humidity. If B or X, then, do nothing (dead zone).
This is corrected:
int getNewState()
{
int value = potentiometerMeasurement();
int stateXLeftMin = humidity - sizeRangeB - sizeRangeX;
int stateXLeftMax = humidity - sizeRangeB;
int stateXRightMin = humidity + sizeRangeB;
int stateXRightMax = humidity + sizeRangeB + sizeRangeX;
if (value >= stateXLeftMin && value <= stateXLeftMax)
return stateB;
if (value >= stateXRightMin && value <= stateXRightMax)
return stateB;
if (value < stateXLeftMin)
return stateA;
if (value > stateXRightMax)
return stateC;
return stateB;
}
void setStateA()
{
if (state == stateA)
return;
u8g.firstPage();
do {
increaseHumidity();
} while (u8g.nextPage());
conditionIncreaseHumidity();
}
void setStateB()
{
if (state == stateB)
return;
u8g.firstPage();
do {
deadZone();
} while (u8g.nextPage());
conditionDeadZone();
}
void setStateC()
{
if (state == stateC)
return;
do {
decreaseHumidity();
} while (u8g.nextPage());
conditionDecreaseHumidity();
}
Now the display does not work at all - it only shows "Zwiekszanie Wilgotnosci". It is suspended because it does not respond to the potentiometer and the diode from the dead zone is still lit.
potentiometerMeasurement() is declared as int but it returns no value.
potentiometerMeasurement() is declared as int but it returns no value.
Now it looks like this:
- the relay has already started working
- the display shows "Zmniejszanie wilgotnosci" even though the value set by the potentiometer is higher than the value indicated by the sensor, eg the sensor indicates 80%, the potentiometer sets the value 85% and the display shows "Zmniejszanie wilgotnosci" instead of "Zwiekszanie wilgotnosci"
- Assuming that the sensor indicates humidity of 80%, I set the potentiometer value 84% and appears on the display of the insecurity zone - means as it should be. However, when I set the value with a potentiometer, for example 50%, the display constantly displays the words "Strefa Nieczulosci" - as if there was no function from reducing humidity.
Post your current code.
Post your current code.
#include "DHT.h"
#include "U8glib.h"
#define LED_INFO_RADIATOR 4
#define LED_INFO_VAPORGENERATOR 13
#define LED_INFO_DEAD_ZONE 5
#define DHTPIN 2
#define DHTTYPE DHT22
const int stateX = 0;
const int stateA = 1;
const int stateB = 2;
const int stateC = 3;
int state = stateA;
const int sizeRangeX = 2;
const int sizeRangeB = 2;
int humidity;
float temperature;
unsigned long currentTime = 0;
unsigned long rememberedTime = 0;
int relayPin = 8;
int relayPin2 = 12;
int relayPin3 = 7;
int relayPin4 = 9;
int potentiometer = A0;
int valuePotentiometer = 0;
int data[5];
int i = 0;
int average;
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE | U8G_I2C_OPT_DEV_0); // I2C(TWI)
DHT dht(DHTPIN, DHTTYPE, 6);
char str[10];
void setup()
{
Serial.begin(9600);
dht.begin();
pinMode(potentiometer, INPUT);
pinMode(relayPin, OUTPUT);
pinMode(relayPin2, OUTPUT);
pinMode(relayPin3, OUTPUT);
pinMode(relayPin4, OUTPUT);
digitalWrite(relayPin, HIGH);
digitalWrite(relayPin2, HIGH);
digitalWrite(relayPin3, HIGH);
digitalWrite(relayPin4, HIGH);
pinMode(LED_INFO_RADIATOR, OUTPUT);
pinMode(LED_INFO_VAPORGENERATOR, OUTPUT);
pinMode(LED_INFO_DEAD_ZONE, OUTPUT);
}
void loop()
{
humidity = dht.readHumidity();
temperature = dht.readTemperature();
timing();
potentiometerMeasurement();
int newState = getNewState();
switch (newState) {
case stateA:
setStateA();
break;
case stateB:
setStateB();
break;
case stateC:
setStateC();
break;
}
}
void decreaseHumidity(void)
{
Serial.println("ZMniejszanie wilgotnosci funkcja");
u8g.firstPage();
do {
u8g.setFont(u8g_font_helvB08);
u8g.drawStr(2, 27, "Zmniejszanie wilgotnosci!");
u8g.drawStr(2, 42, "Wilgotnosc :");
u8g.drawStr(80, 42, dtostrf(humidity, 5, 2, str));
u8g.drawStr(110, 42, "%");
u8g.drawStr(2, 57, "Wilg. zad.:");
u8g.drawStr(80, 57, dtostrf(valuePotentiometer, 5, 2, str));
u8g.drawStr(110, 57, "%");
} while (u8g.nextPage()) ;
}
void increaseHumidity(void)
{
Serial.println("Zwiekszanie wilgotnosci funkcja");
u8g.firstPage();
do {
u8g.setFont(u8g_font_helvB08);
u8g.drawStr(2, 27, "Zwiekszanie wilgotnosci!");
u8g.drawStr(2, 42, "Wilgotnosc :");
u8g.drawStr(80, 42, dtostrf(humidity, 5, 2, str));
u8g.drawStr(110, 42, "%");
u8g.drawStr(2, 57, "Wilg. zad.:");
u8g.drawStr(80, 57, dtostrf(valuePotentiometer, 5, 2, str));
u8g.drawStr(110, 57, "%");
}while (u8g.nextPage());
}
void deadZone(void)
{
Serial.println("Strefa nieczulosci funkcja");
u8g.firstPage();
do {
u8g.setFont(u8g_font_helvB08);
u8g.drawStr(2, 27, "Strefa Nieczulosci");
u8g.drawStr(2, 42, "Wilgotnosc :");
u8g.drawStr(80, 42, dtostrf(humidity, 5, 2, str));
u8g.drawStr(110, 42, "%");
u8g.drawStr(2, 57, "Wilg. zad.:");
u8g.drawStr(80, 57, dtostrf(valuePotentiometer, 5, 2, str));
u8g.drawStr(110, 57, "%");
}while (u8g.nextPage());
}
static void turnOnRadiator()
{
Serial.println("Wlacz Wentylatory funkcja");
digitalWrite(relayPin2, LOW);
digitalWrite(LED_INFO_RADIATOR, HIGH);
}
static void turnOffRadiator()
{
Serial.println("Wylacz Wentylatory funkcja");
digitalWrite(relayPin2, HIGH);
digitalWrite(LED_INFO_RADIATOR, LOW);
}
static void turnOnFan()
{
digitalWrite(relayPin3, LOW);
digitalWrite(relayPin4, LOW);
}
static void turnOffFan()
{
digitalWrite(relayPin3, HIGH);
digitalWrite(relayPin4, HIGH);
}
static void turnOnVaporGenerator()
{
Serial.println("punkt 6 programu");
digitalWrite(relayPin, LOW);
digitalWrite(LED_INFO_VAPORGENERATOR, HIGH);
}
static void turnOffVaporGenerator()
{
digitalWrite(relayPin, HIGH);
digitalWrite(LED_INFO_VAPORGENERATOR, LOW);
}
static void turnOffDeadZone()
{
Serial.println("Wylacz Strefe nieczulosci funkcja");
digitalWrite(LED_INFO_DEAD_ZONE, LOW);
}
static void turnOnDeadZone()
{
Serial.println("Wlacz Strefe nieczulosci funkcja");
digitalWrite(LED_INFO_DEAD_ZONE, HIGH);
}
int potentiometerMeasurement()
{
data[i] = analogRead(A0);
valuePotentiometer = map(data[i], 0, 1023, 0, 100);
i = (i + 1) % 5;
average = 0;
for (int j = 0; j < 5; j++)
average += data[j];
average /= 5.0;
return valuePotentiometer;
}
void timing()
{
currentTime = millis();
if (currentTime - rememberedTime >= 50UL) {
rememberedTime = currentTime;
}
}
void conditionIncreaseHumidity()
{
Serial.println("Warunek zwiekszania wilgotnosci");
turnOffFan();
turnOffRadiator();
turnOnVaporGenerator();
turnOffDeadZone();
}
void conditionDecreaseHumidity()
{
Serial.println("Warunek zmniejszania wilgotnosci");
turnOnFan();
turnOnRadiator();
turnOffVaporGenerator();
turnOffDeadZone();
}
void conditionDeadZone()
{
Serial.println("Warunek strefy nieczulosci");
turnOffVaporGenerator();
turnOffRadiator();
turnOffFan();
turnOnDeadZone();
}
int getNewState()
{
int value = potentiometerMeasurement();
int stateXLeftMin = humidity - sizeRangeB - sizeRangeX;
int stateXLeftMax = humidity - sizeRangeB;
int stateXRightMin = humidity + sizeRangeB;
int stateXRightMax = humidity + sizeRangeB + sizeRangeX;
if (value >= stateXLeftMin && value <= stateXLeftMax)
return stateB;
if (value >= stateXRightMin && value <= stateXRightMax)
return stateB;
if (value < stateXLeftMin)
return stateA;
if (value > stateXRightMax)
return stateC;
return stateB;
}
void setStateA()
{
if (state == stateA)
return;
u8g.firstPage();
do {
increaseHumidity();
} while (u8g.nextPage());
conditionIncreaseHumidity();
}
void setStateB()
{
if (state == stateB)
return;
u8g.firstPage();
do {
deadZone();
} while (u8g.nextPage());
conditionDeadZone();
}
void setStateC()
{
if (state == stateC)
return;
do {
decreaseHumidity();
} while (u8g.nextPage());
conditionDecreaseHumidity();
}
It looks to me like you have 'humidity' and 'value' switched in getNewState(). Doesn't it make more sense to compare the current humidity to the setpoint (value)? This is what I think you intended to write:
int getNewState()
{
int setpoint = potentiometerMeasurement();
int stateXLeftMin = setpoint - sizeRangeB - sizeRangeX;
int stateXLeftMax = setpoint - sizeRangeB;
int stateXRightMin = setpoint + sizeRangeB;
int stateXRightMax = setpoint + sizeRangeB + sizeRangeX;
if (humidity >= stateXLeftMin && humidity <= stateXLeftMax)
return stateB;
if (humidity >= stateXRightMin && humidity <= stateXRightMax)
return stateB;
if (humidity < stateXLeftMin)
return stateA;
if (humidity > stateXRightMax)
return stateC;
return stateB;
}
And that can be reduced to:
int getNewState()
{
int setpoint = potentiometerMeasurement();
int stateXLeftMin = setpoint - sizeRangeB - sizeRangeX;
int stateXRightMax = setpoint + sizeRangeB + sizeRangeX;
if (humidity < stateXLeftMin)
return stateA; // Humidity too low
if (humidity > stateXRightMax)
return stateC; // Humidity too high
return stateB; // Humidity just right
}
johnwasser:
It looks to me like you have 'humidity' and 'value' switched in getNewState(). Doesn't it make more sense to compare the current humidity to the setpoint (value)? This is what I think you intended to write:int getNewState()
{
int setpoint = potentiometerMeasurement();
int stateXLeftMin = setpoint - sizeRangeB - sizeRangeX;
int stateXLeftMax = setpoint - sizeRangeB;
int stateXRightMin = setpoint + sizeRangeB;
int stateXRightMax = setpoint + sizeRangeB + sizeRangeX;
if (humidity >= stateXLeftMin && humidity <= stateXLeftMax)
return stateB;
if (humidity >= stateXRightMin && humidity <= stateXRightMax)
return stateB;
if (humidity < stateXLeftMin)
return stateA;
if (humidity > stateXRightMax)
return stateC;
return stateB;
}
And that can be reduced to:int getNewState()
{
int setpoint = potentiometerMeasurement();
int stateXLeftMin = setpoint - sizeRangeB - sizeRangeX;
int stateXRightMax = setpoint + sizeRangeB + sizeRangeX;
if (humidity < stateXLeftMin)
return stateA; // Humidity too low
if (humidity > stateXRightMax)
return stateC; // Humidity too high
return stateB; // Humidity just right
}
Now the function of increasing humidity does not work at all.
"Does not work" is the least-helpful thing you can say.
Tell us something like: "I set the setpoint to 27 and the humidity measured was 38 and I expected to see StateA but it didn't return StateA from getNewState()." We need real numbers to work with. We can't see where you turned the pot.
MorganS:
"Does not work" is the least-helpful thing you can say.Tell us something like: "I set the setpoint to 27 and the humidity measured was 38 and I expected to see StateA but it didn't return StateA from getNewState()." We need real numbers to work with. We can't see where you turned the pot.
For example: after uploading the program, the humidity measured by the sensor was 75%, the humidity set by the 40% potentiometer - everything worked properly, the appropriate function was displayed. When you reach the dead zone, everything still works. However, if you want to set the setpoint with a potentiometer above the dead band (above 79%), the display suddenly freezes and does not respond - the function for increasing the humidity is not displayed.
Is anyone able to help me? ![]()