Ei, Wieso das?
Du kannst den Lüfter auch per PWM ausschalten.
Ansonsten mußt Du bevor Du den Lüfter ausschaltet den PWM Ausgang Ausschalten und Hochohmig (als Eingang) schalten.
Grüße Uwe
Ei, Wieso das?
Du kannst den Lüfter auch per PWM ausschalten.
Ansonsten mußt Du bevor Du den Lüfter ausschaltet den PWM Ausgang Ausschalten und Hochohmig (als Eingang) schalten.
Grüße Uwe
Ich war ja nie soweit das das PWM läuft. sonnst war es immer so das die Lüfter wenn sie abgeschaltet sind immer noch gefiept haben. Und ich deswegen noch einen Schalter mit eingebaut habe. da das System ja auf Akkubasis läuft. muss man also noch was ändern bezüglich des PWM Signals?
Also da ja nun alles passt von dem letzten Code wo ich Plumps sehr dankbar bin läuft so wie ich es mir vorgestellt habe. Die Hysterese möchte ich noch gern mit einstellen komme mit dem analogWrite nicht klar da ich dort immer einen Fehler bekomme.
Was du machst und welcher Fehler entsteht musst du schon zeigen.
Also ich bekomme den Fehler Arduino: 1.8.13 (Windows 10), Board: "Arduino Nano, ATmega328P"
C:\Users\Tomi\Documents\Arduino\L_fter_Wohnzimmer_SHT21_Sensor\Test_SHT_Sensor\Test_SHT_Sensor.ino: In function 'void loop()':
Test_SHT_Sensor:146:19: error: expected primary-expression before 'const'
analogWrite(const Pin &meinPin, 255);
^~~~~
exit status 1
expected primary-expression before 'const'
Dieser Bericht wäre detaillierter, wenn die Option
"Ausführliche Ausgabe während der Kompilierung"
in Datei -> Voreinstellungen aktiviert wäre.
Ist bestimmt nur ne Kleinigkeit sonnst würde der Lüfter bei der Temeperatur ständig an und ausgehen was leider Tierisch nervt.
Hier der Code `/**Ver.3 ***/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SHT21.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 4
SHT21 sht; //SHT on OLED entities
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
float Temp; //Here we store the temperature and humidity values
//float Humidity;
#include <Fonts/FreeMonoBold12pt7b.h>
//#include "DHT.h" // Include DHT11 library
//#define DATApin 12 // Defines Arduino pin which is connected to the sensor
//DHT
//#define DHTPIN 12 // DHT pin
//#define DHTTYPE DHT22
//dht(DHTPIN, DHTTYPE); // Creates a DHT object
struct Pin {
uint8_t pwmPin;
};
const Pin fan {9}; // fan is connect to Arduino Pin (PWM)
int GreenLed = 3; // Green LED is connect to Arduino Pin
int RedLed = 4; // Red LED is connect to Arduino Pin
//int HumidityMax = 95;
//int HumidityMin = 50;
float tempMin = 24.0; // the temperature to start the fan 0%
float tempMax = 28.0; // the maximum temperature when fan is at 100%
byte fanSpeed = 0;
int fanOut =0;
unsigned long previousMillis = 0; // speichert den Zeitpunkt an dem zuletzt geschalten wurde
const long intervall = 200; //600000; // Länge der Pause (hier 5 Min.) in Millisekunden 5 * 60 *
void analogWrite(const Pin &meinPin, uint8_t prozent) {
if (meinPin.pwmPin == 9) OCR1A = map(prozent, 0, 100, 0, ICR1);
}
void setup()
{
pinMode(fan.pwmPin, OUTPUT);
TCNT1 = 0; // Counter zurücksetzen
TCCR1A = 0; TCCR1B = 0; // Setzt TCCR1 Register auf 0
ICR1 = 320; // Setzt Obergrenze des Zählers, wo dann wieder runter gezählt wird
TCCR1B |= (1 << WGM13); TCCR1A |= (1 << WGM11); // Aktiviert Mode 10, PWM phase correct, ICR1 at TOP //Seite 109 Tabelle 15-5
TCCR1A |= (1 << COM1A1); // Modus Setzt OC1A bei Übereinstimmung beim Hochzählen auf LOW. Setzt OC1A bei Übereinstimmung beim Runterzählen auf HIGH. //Seite 109 Tabelle 15-4
TCCR1B |= (1 << CS10); // Vorteiler 1 //Seite 110 Tabelle 15-6
// ICR1 = 16.000.000/1/2/25000 = 320
// TOP = ProzessorHz/Vorteiler/hoch-runterzählen/25000Hz
//Sets the baud for serial data transmission between Arduino and your computer
Serial.begin(9600);
//dht.begin();
//sht.begin();
//initialize with the I2C addr 0x3C (128x64)
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
delay(10);
pinMode(RedLed, OUTPUT); // initialize digital pin RedLed as an output.
pinMode(GreenLed, OUTPUT); // initialize digital pin GreenLed as an output.
// Print text on display
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(25, 0);
display.println(F("ELECTRODUIN0")); // Print text
/*
display.setCursor(10, 20);
display.println(F("temperature Based"));
display.setCursor(3, 35);
display.println(F("fan Speed Controller"));
display.display();
*/
delay(2000);
}
void loop() {
float temp = sht.getTemperature();
float Humidity = sht.getHumidity();
//float temp = dht.readTemperature();
//float Humidity = dht.readHumidity();
//Print tempareture Value on Serial Monitor Window
Serial.print("temperature = ");
Serial.print(temp, 0); // temperature value in Degree Celsius
Serial.println("°C");
unsigned long currentMillis = millis();//Jetzige Millisekunden
if (currentMillis - previousMillis >= intervall) // Falls mehr als 300000 ms vergangen sind
{
previousMillis = currentMillis; // Zeitpunkt der letzten Schaltung wird festgehalten
//if temp is lower than tempMin
if (temp < tempMin) // if temp is lower than tempMin
{
fanSpeed = 255; // fan is not spinning
digitalWrite(GreenLed, HIGH); // turn off Green LED
digitalWrite(RedLed, LOW); // turn on Red LED
}
//if temperature is higher than tempMin and lower than tempMax
if ((temp >= tempMin) && (temp <= tempMax))
{
fanSpeed = map (temp, tempMin, tempMax, 100, 0); // the actual speed of fan//map(temp, tempMin, tempMax, 32, 255);
//fanSpeed = 1.5 * fanSpeed;
//fanLCD = map (temp, tempMin, tempMax, 100, 0); // fan speed Show on display
digitalWrite(GreenLed, HIGH); // turn on Green LED
digitalWrite(RedLed, LOW); // turn on Red LED
}
//if temp is higher than tempMax
if (temp > tempMax)
{
fanSpeed = 0;
digitalWrite(RedLed, HIGH); // turn on Red LED
digitalWrite(GreenLed, LOW); // turn off Green LED
}
if (fanSpeed > 100)
{
fanSpeed = 100;
}
// Hysterese
if (fanOut == 1)
{
fanSpeed = 0;
}
if(temp >= 31)
{
if(fanOut == 1)
{
fanOut = 0;
analogWrite(const Pin &meinPin, 255);
}
}
// PWM Wert auf 255 begerenzen
if (fanSpeed > 255)
{
fanSpeed = 255;
}
// Lüftergeschwindigkeit über den Seriellen Monitor ausgeben
//Serial.print("Lueftergeschwindigkeit: ");
//Serial.println(fanSpeed);
analogWrite(fan, fanSpeed); // Den Lüfter mit dem PWM Wert ansteuern
delay(500);
}
// Print text on display
display.clearDisplay();
// display.setTextSize(1);
display.setTextColor(WHITE, BLACK);
display.setCursor(0, 0);
display.print("H: "); // Print text
display.print(Humidity);
display.print(" %");
display.setTextSize(2);
display.setCursor(0, 20);
// Print temperature
display.print("Temp:");
display.setCursor(60, 20);
display.print(temp, 1); // temperature Value
display.setCursor(95, 20);
//display.print((char)247);
display.setCursor(110, 20);
display.print("C");
// Print fan Speen
display.setCursor(0, 40);
display.print("FAN:: ");
display.setCursor(60, 40);
display.print(fanSpeed); // fan Speed Value
display.setCursor(110, 40);
display.print("%");
display.display();
//delay(2000); // wait two seconds
}`
Hallo,
Tom, formatiere einmal den Sketch und setze ihn mit dem Code Tag Button neu rein. Das kann so niemand lesen. Du kannst auch in der IDE unter Bearbeiten "Für Forum kopieren" auswählen und so hier rein kopieren. Das alles kannst du nachträglich ändern ohne eine neue Antwort zu erstellen.
/**Ver.3 ***/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SHT21.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 4
SHT21 sht; //SHT on OLED entities
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
float Temp; //Here we store the temperature and humidity values
//float Humidity;
#include <Fonts/FreeMonoBold12pt7b.h>
//#include "DHT.h" // Include DHT11 library
//#define DATApin 12 // Defines Arduino pin which is connected to the sensor
//DHT
//#define DHTPIN 12 // DHT pin
//#define DHTTYPE DHT22
//dht(DHTPIN, DHTTYPE); // Creates a DHT object
struct Pin {
uint8_t pwmPin;
};
const Pin fan {9}; // fan is connect to Arduino Pin (PWM)
int GreenLed = 3; // Green LED is connect to Arduino Pin
int RedLed = 4; // Red LED is connect to Arduino Pin
//int HumidityMax = 95;
//int HumidityMin = 50;
float tempMin = 24.0; // the temperature to start the fan 0%
float tempMax = 28.0; // the maximum temperature when fan is at 100%
byte fanSpeed = 0;
int fanOut = 0;
unsigned long previousMillis = 0; // speichert den Zeitpunkt an dem zuletzt geschalten wurde
const long intervall = 200; //600000; // Länge der Pause (hier 5 Min.) in Millisekunden 5 * 60 *
void analogWrite(const Pin &meinPin, uint8_t prozent) {
if (meinPin.pwmPin == 9) OCR1A = map(prozent, 0, 100, 0, ICR1);
}
void setup()
{
pinMode(fan.pwmPin, OUTPUT);
TCNT1 = 0; // Counter zurücksetzen
TCCR1A = 0; TCCR1B = 0; // Setzt TCCR1 Register auf 0
ICR1 = 320; // Setzt Obergrenze des Zählers, wo dann wieder runter gezählt wird
TCCR1B |= (1 << WGM13); TCCR1A |= (1 << WGM11); // Aktiviert Mode 10, PWM phase correct, ICR1 at TOP //Seite 109 Tabelle 15-5
TCCR1A |= (1 << COM1A1); // Modus Setzt OC1A bei Übereinstimmung beim Hochzählen auf LOW. Setzt OC1A bei Übereinstimmung beim Runterzählen auf HIGH. //Seite 109 Tabelle 15-4
TCCR1B |= (1 << CS10); // Vorteiler 1 //Seite 110 Tabelle 15-6
// ICR1 = 16.000.000/1/2/25000 = 320
// TOP = ProzessorHz/Vorteiler/hoch-runterzählen/25000Hz
//Sets the baud for serial data transmission between Arduino and your computer
Serial.begin(9600);
//dht.begin();
//sht.begin();
//initialize with the I2C addr 0x3C (128x64)
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
delay(10);
pinMode(RedLed, OUTPUT); // initialize digital pin RedLed as an output.
pinMode(GreenLed, OUTPUT); // initialize digital pin GreenLed as an output.
// Print text on display
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(25, 0);
display.println(F("ELECTRODUIN0")); // Print text
/*
display.setCursor(10, 20);
display.println(F("temperature Based"));
display.setCursor(3, 35);
display.println(F("fan Speed Controller"));
display.display();
*/
delay(2000);
}
void loop() {
float temp = sht.getTemperature();
float Humidity = sht.getHumidity();
//float temp = dht.readTemperature();
//float Humidity = dht.readHumidity();
//Print tempareture Value on Serial Monitor Window
Serial.print("temperature = ");
Serial.print(temp, 0); // temperature value in Degree Celsius
Serial.println("°C");
unsigned long currentMillis = millis();//Jetzige Millisekunden
if (currentMillis - previousMillis >= intervall) // Falls mehr als 300000 ms vergangen sind
{
previousMillis = currentMillis; // Zeitpunkt der letzten Schaltung wird festgehalten
//if temp is lower than tempMin
if (temp < tempMin) // if temp is lower than tempMin
{
fanSpeed = 255; // fan is not spinning
digitalWrite(GreenLed, HIGH); // turn off Green LED
digitalWrite(RedLed, LOW); // turn on Red LED
}
//if temperature is higher than tempMin and lower than tempMax
if ((temp >= tempMin) && (temp <= tempMax))
{
fanSpeed = map (temp, tempMin, tempMax, 100, 0); // the actual speed of fan//map(temp, tempMin, tempMax, 32, 255);
//fanSpeed = 1.5 * fanSpeed;
//fanLCD = map (temp, tempMin, tempMax, 100, 0); // fan speed Show on display
digitalWrite(GreenLed, HIGH); // turn on Green LED
digitalWrite(RedLed, LOW); // turn on Red LED
}
//if temp is higher than tempMax
if (temp > tempMax)
{
fanSpeed = 0;
digitalWrite(RedLed, HIGH); // turn on Red LED
digitalWrite(GreenLed, LOW); // turn off Green LED
}
if (fanSpeed > 100)
{
fanSpeed = 100;
}
// Hysterese
if (fanOut == 1)
{
fanSpeed = 0;
}
if (temp >= 31)
{
if (fanOut == 1)
{
fanOut = 0;
analogWrite(const Pin & meinPin, 255);
}
}
// PWM Wert auf 255 begerenzen
if (fanSpeed > 255)
{
fanSpeed = 255;
}
// Lüftergeschwindigkeit über den Seriellen Monitor ausgeben
//Serial.print("Lueftergeschwindigkeit: ");
//Serial.println(fanSpeed);
analogWrite(fan, fanSpeed); // Den Lüfter mit dem PWM Wert ansteuern
delay(500);
}
// Print text on display
display.clearDisplay();
// display.setTextSize(1);
display.setTextColor(WHITE, BLACK);
display.setCursor(0, 0);
display.print("H: "); // Print text
display.print(Humidity);
display.print(" %");
display.setTextSize(2);
display.setCursor(0, 20);
// Print temperature
display.print("Temp:");
display.setCursor(60, 20);
display.print(temp, 1); // temperature Value
display.setCursor(95, 20);
//display.print((char)247);
display.setCursor(110, 20);
display.print("C");
// Print fan Speen
display.setCursor(0, 40);
display.print("FAN:: ");
display.setCursor(60, 40);
display.print(fanSpeed); // fan Speed Value
display.setCursor(110, 40);
display.print("%");
display.display();
//delay(2000); // wait two seconds
}
So sollte es besser sein. ![]()
Nicht so. analogWrite einfach wie gewohnt aufrufen. Außer das es im Moment 0-100 Prozent als Wert annimmt.
if (fanOut == 1)
{
fanOut = 0;
analogWrite(fan, 100);
}
Aber analogWrite wird eh am Ende vom Loop aufgerufen. Es reicht fanSpeed zu ändern.
Vielleicht solltest du deinen Ablauf überdenken.
Ah ok lief durch alles klar habe ich nicht gewusst. Jetzt passt es super ich danke euch. ist klasse hier das man so tolle Hilfe bekommt. Ihr hier im Forum seid echt die besten. läuft so wie es sein soll.