Rolluikschakeling met lichtsterkte

Geef hier suggesties voor extra onderwerpen of andere dingen die je graag gewijzigd zou willen zien
Berichten: 1
Geregistreerd: 17 Nov 2019, 20:05

Rolluikschakeling met lichtsterkte

Berichtdoor Rikkes » 17 Nov 2019, 20:16

Hallo, wie kan mij helpen?

Ik ben mijn domotica systeem aan het verbouwen met een arduino mega. Alles werkt perfect maar nu zit ik even met de handen in het (weinige) haar.

Ik wil mijn rolluiken omhoog doen gaan als een vooraf opgegeven lichtsterkte bereikt is en hetzelfde voor het naar beneden gaan. Ik gebruik een analoge ingang die ik deel tot 40 stappen (ipv 1024).

Ik moet bij het bereiken van de juiste lichtsterkte een puls van pakweg 1 sec kunnen geven (een variabele die 1 sec moet high staan). Probleem is dat er moet gedetecteerd worden wanneer de lichtsterkte daalt (rolluiken naar beneden) en wanneer deze stijgt (rolluiken naar boven) bij het bereiken van de schakelwaarde.

Wie kan mij helpen?

Hierbij mijn testschetsje. Opgelet, ik ben een beginner en nog maar 2 weken bezig met arduino. Suggesties over domme dingen in mijn schets mogen ook! :-)

/* Test domotica system
* Analog input depending on used resistors
* Rikkes 2019
*/
#include <RTClib.h>
#include <Wire.h>

RTC_DS3231 rtc;

char t[32];


const int analogInPin = A2;
const int analogInPinLight = A7;

const int ledPin1 = 8;
const int ledPin2 = 9;
const int ledPin3 = 11;
const int ledPin4 = 12;

int sensorValue = 0;
int sensorValueLight = 0;
int outputValue = 0;
int reading1;
int reading2;
int reading3;
int reading4;
int reading5;
int buttonState1;
int buttonState2;
int buttonState3;
int buttonState4;
int buttonState5;
int ledState1 = LOW;
int ledState2 = LOW;
int ledState22 = LOW;
int ledState3 = LOW;
int ledState33 = LOW;
int ledState4 = LOW;
int lastButtonState1 = LOW;
int lastButtonState2 = LOW;
int lastButtonState3 = LOW;
int lastButtonState4 = LOW;
int lastButtonState5 = LOW;
unsigned long lastDebounceTime = 0;
unsigned long lastDebounceTimeShutters = 0;
unsigned long debounceDelay = 50;
unsigned long debounceDelayLong = 2000;
unsigned long shuttertimer = 5000;
unsigned long shutterswitch = 0;
unsigned long groenTimer = 600000;
unsigned long shutteruptimehour = 21;
unsigned long shutteruptimeminutes = 53;
unsigned long shutteruptimeseconds = 10;
unsigned long shutterdowntimehour = 21;
unsigned long shutterdowntimeminutes = 53;
unsigned long shutterdowntimeseconds = 30;
long light = 0;
bool lightshuttersup;

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
Wire.begin();
rtc.begin();


// declare the ledPin as an OUTPUT:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);


// set initial LED state
digitalWrite(ledPin1, ledState1);
digitalWrite(ledPin2, ledState2);
digitalWrite(ledPin3, ledState3);
digitalWrite(ledPin4, ledState4);
}

void loop() {

// time

DateTime now = rtc.now();

sprintf(t, "%02d:%02d:%02d %02d/%02d/%02d", now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());



// read analoge value

sensorValue = analogRead(analogInPin);
sensorValueLight = analogRead(analogInPinLight);
light = sensorValueLight/25,6;





//-----------------------------Simple LED ON/OFF-------------------------------

// Toggle LED 1

{if (sensorValue > 699 && sensorValue < 709)
reading1 = HIGH;
else
reading1 = LOW;}

// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:

// If the switch changed, due to noise or pressing:
if (reading1 != lastButtonState1) {
// reset the debouncing timer
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:

// if the button state has changed:
if (reading1 != buttonState1) {
buttonState1 = reading1;

// only toggle the LED if the new button state is HIGH
if (buttonState1 == HIGH) {
ledState1 = !ledState1;
}
}
}

// set the LED:
digitalWrite(ledPin1, ledState1);

// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState1 = reading1;


/* //-----------------------------Simple switch LED ON/OFF ON DARKNESS-------------------------------

// Switch LED 1

{if (light > 25)
ledState1 = HIGH;
else
ledState1 = LOW;}

// set the LED:

digitalWrite(ledPin1, ledState1);
*/

//-----------------------------Simple LED ON/OFF with TIMER 10min-------------------------------

// Toggle LED 4


{if (sensorValue > 927 && sensorValue < 937)
reading4 = HIGH;
else
reading4 = LOW;}

// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:

// If the switch changed, due to noise or pressing:
if (reading4 != lastButtonState4) {
// reset the debouncing timer
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:

// if the button state has changed:
if (reading4 != buttonState4) {
buttonState4 = reading4;


// only toggle the LED if the new button state is HIGH
if (buttonState4 == HIGH) {
ledState4 = !ledState4;
}
}
}

// set timer

if (millis() > (groenTimer + lastDebounceTime)) {
ledState4 = LOW;
}


// set the LED:
digitalWrite(ledPin4, ledState4);

// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState4 = reading4;






//-----------------------------SHUTTERS UP-------------------------------

// Toggle LED 2 (shutters up)

// Toggle by button

{if (sensorValue > 783 && sensorValue < 793)
reading2 = HIGH;
else
reading2 = LOW;}

// Toggle by clock

{if (now.hour() == shutteruptimehour)
if (now.minute() == shutteruptimeminutes)
if (now.second() == shutteruptimeseconds)
reading2 = HIGH;
else reading2 = LOW;}



// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:

// If the switch changed, due to noise or pressing:
if (reading2 != lastButtonState2)
// reset the debouncing timer
{lastDebounceTimeShutters = millis();
}

if ((millis() - lastDebounceTimeShutters) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:

// if the button state has changed:
if (reading2 != buttonState2) {
buttonState2 = reading2;

// only toggle the LED if the new button state is HIGH
if (buttonState2 == HIGH)
{ledState2 = !ledState2;
}
}
}
// Motor protection (see if shutters down is activated, if so, desactivate it)
if (ledState2 == HIGH) {
ledState3 = LOW;

}

// set timer

if (millis() > (shuttertimer + lastDebounceTimeShutters)) {
ledState2 = LOW;
}


// set the LED:
digitalWrite(ledPin2, ledState2);


// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState2 = reading2;





//-----------------------------SHUTTERS DOWN-------------------------------
// Toggle LED 3 (shutters down)

// Toggle by button

{if (sensorValue > 509 && sensorValue < 519)
reading3 = HIGH;
else
reading3 = LOW;}

// Toggle by clock

{if (now.hour() == shutterdowntimehour)
if (now.minute() == shutterdowntimeminutes)
if (now.second() == shutterdowntimeseconds)
reading3 = HIGH;
else reading3 = LOW;}

// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:

// If the switch changed, due to noise or pressing:
if (reading3 != lastButtonState3) {
// reset the debouncing timer
lastDebounceTimeShutters = millis();
}

if ((millis() - lastDebounceTimeShutters) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:

// if the button state has changed:
if (reading3 != buttonState3) {
buttonState3 = reading3;

// only toggle the LED if the new button state is HIGH
if (buttonState3 == HIGH) {
ledState3 = !ledState3;
}
}
}


// Motor protection (see if shutters up is activated, if so, desactivate it)
if(ledState3 == HIGH){
ledState2 = LOW;

}



// set timer

if (millis() > (shuttertimer + lastDebounceTimeShutters)) {
ledState3 = LOW;

}
// set the LED:
digitalWrite(ledPin3, ledState3);

// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState3 = reading3;


//-----------------------------TURN OFF ALL SIMPLE LEDS-------------------------------


// switch off all LEDS

{if (sensorValue > 93 && sensorValue < 103
)
reading5 = HIGH;
else
reading5 = LOW;}

// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:

// If the switch changed, due to noise or pressing:
if (reading5 != lastButtonState5) {
// reset the debouncing timer
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelayLong) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:

// if the button state has changed:
if (reading5 != buttonState5) {
buttonState5 = reading5;}

// only switch off LEDS if the new button state is HIGH
if (buttonState5 == HIGH) {
ledState1 = LOW;
ledState4 = LOW;}


}

// set the LED:
digitalWrite(ledPin1, ledState1);
digitalWrite(ledPin4, ledState4);

// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState5 = reading5;






//-----------------------------PRINT RESULTS-------------------------------
// print the results to the Serial Monitor:
Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\t lightsensor = ");
Serial.print(sensorValueLight);
Serial.print("\t light = ");
Serial.print(light);
Serial.print("\t shutter up = ");
Serial.print(ledState2);
Serial.print("\t shutter dn = ");
Serial.print(ledState3);
Serial.print(F("\t Date/Time: "));
Serial.println(t);



delay(5);
}

Advertisement

Gebruikers-avatar
Berichten: 2655
Geregistreerd: 06 Aug 2016, 01:03

Re: Rolluikschakeling met lichtsterkte

Berichtdoor Koepel » 18 Nov 2019, 04:49

Het is natuurlijk wel mogelijk om te kijken of de lichtsterkte stijgt of daalt, maar het is nu te verwarrend voor me.

Kun je een functie maken om het denderen van een knop tegen te gaan. Nu staat die code er meerdere keren in.
Je kunt daar ook een library voor gebruiken: https://github.com/thomasfredericks/Bounce2.

Variabelen die je alleen lokaal gebruikt, kun je beter lokaal declareren.

In de jaren '80 werden tabs gebruikt om dingen onder elkaar te krijgen. Tegenwoordig is een tab 2 of 4 of 8 groot en soms zijn het spaties. Als je spaties gebruikt, dan weet je zeker dat het onder elkaar komt. Dus gewoon een paar spaties in plaats van \t heeft mijn voorkeur.
Sinds kort zou de hele Arduino IDE de UTF-8 tekens kunnen gebruiken. Je mag dus ook UTF-8 tekens gebruiken om een mooie tabel te maken: ┗ ┫━┓ ⛅ ⚽

Waarom is de 'light' variabele een 'long' ?
Waarom deel je die door 25 ? Je zou er een percentage van kunnen maken, van 0 tot en met 100.
Dan zou je er ook een 'float' van kunnen maken. Dat rekent gemakkelijker.

In de taal 'C' en 'C++' kun je soms rare dingen uithalen die toegestaan zijn. Maar je kunt beter duidelijke code schrijven.
Dit is een foutje: light = sensorValueLight / 25, 6;
Die ",6" achteraan is niet je bedoeling, maar dat is dus wel toegestaan in de taal 'C'. Het doet ook echt iets.

Deze extra haakjes zijn niet nodig:
Code: Alles selecteren
  // Toggle LED 1
  {
    if (sensorValue > 699 && sensorValue < 709)
      reading1 = HIGH;
    else
      reading1 = LOW;
  }


Dat kan gewoon zo:
Code: Alles selecteren
  // Toggle LED 1
  if (sensorValue > 699 && sensorValue < 709)
    reading1 = HIGH;
  else
    reading1 = LOW;


Maar ik geef er vaak de voorkeur aan om het uit te schrijven met haakjes:
Code: Alles selecteren
  // Toggle LED 1
  if (sensorValue > 699 && sensorValue < 709)
  {
    reading1 = HIGH;
  }
  else
  {
    reading1 = LOW;
  }



Gebruik millis() altijd op deze manier:
huidige_waarde_van_millis - een_vorige_waarde_van_millis >= een_interval.

Je mag niet een millis() waarde in de toekomst berekenen.
Dus dit is niet correct: if (millis() > (groenTimer + lastDebounceTime))

Wil je voortaan code-tags gebruiken ?
Op dit forum zie je boven het tekstveld een paar knoppen. De "Code" knop geeft code-tags. Je kunt je sketch binnen die code-tags zetten. Via "Select a Syntax" en dan "C++" zijn er andere code-tags met een andere opmaak.

Voor zo'n project zeggen wij altijd dat je hysteresis nodig hebt. Dit schreef ik (http://arduinoforum.nl/viewtopic.php?f=21&t=3990#p27000) twee dagen geleden:
Hysteresis:
Bijvoorbeeld bij een gebouw met zonneschermen. Dan de zonneschermen pas uit doen als de zonsterkte boven de 60% komt, en de zonnenschermen pas dicht doen als de zon onder de 40% komt. Op die manier wordt voorkomen dat de zonneschermen heel de tijd in- en uitgaan als het kantelpunt 50% zou zijn en de zonsterkte zo rond de 50% zou zitten.

Dat is misschien te eenvoudig. In werkelijkheid kun je er een laagdoorlaat filter aan toevoegen in software, zodat een wolkje geen effect heeft. Misschien met millis() slechts iedere minuut kijken. Ik zou millis() gebruiken samen met een laagdoorlaat filter en dan hysteresis er bij.

Als je dat hebt, dan hoef je niet meer te weten of de lichtsterkte stijgt of daalt. Heb ik nu per ongeluk je probleem opgelost :lol:

Terug naar Forum suggesties

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 4 gasten