• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Sterowanie Światłem
#4
(24-05-2019, 13:21)es2 napisał(a):
(24-05-2019, 11:28)AdamJab napisał(a): Nie mogę sobie proadzić z zaprogramowaniem sterowania oświetleniem. Chodzi o to, że światło ma być załączane sygnałem z czujki PIR i dodatkowo włącznikiem dzwonkowym w takim układzie, że jeśli włączymy włącznikiem to czujka nam tego nie wyłączy a jeśli wyłączymy włącznikiem to czujka będzie załączała oświetlenie.
Musisz reagować na zbocze sygnału a nie jego poziom.

Ok. Trochę pozmieniałem i teraz prekaźnik Relay_4 jest załączany i wyłączany odpowiednim zboczem sygnału ale pomimo tego nie działa mi opóźnienie wyłączenia po załączeniu czujką PIR.
Co ciekawe doskonale działa opóźnienie załączenia czzujką PIR ale nie o to mi chodzi. 


Kod:
// Enable debug prints to serial monitor
#define MY_DEBUG


// Enable and select radio type attached
//#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69

// Set LOW transmit power level as default, if you have an amplified NRF-module and
// power your radio separately with a good regulator you can turn up PA level.
//#define MY_RF24_PA_LEVEL RF24_PA_LOW

// Enable serial gateway
#define MY_GATEWAY_SERIAL

// Define a lower baud rate for Arduino's running on 8 MHz (Arduino Pro Mini 3.3V & SenseBender)
#if F_CPU == 8000000L
#define MY_BAUD_RATE 38400
#endif

// Flash leds on rx/tx/err
// #define MY_LEDS_BLINKING_FEATURE
// Set blinking period
// #define MY_DEFAULT_LED_BLINK_PERIOD 300

// Inverses the behavior of leds
// #define MY_WITH_LEDS_BLINKING_INVERSE

// Enable inclusion mode
#define MY_INCLUSION_MODE_FEATURE
// Enable Inclusion mode button on gateway
#define MY_INCLUSION_BUTTON_FEATURE

// Inverses behavior of inclusion button (if using external pullup)
//#define MY_INCLUSION_BUTTON_EXTERNAL_PULLUP

// Set inclusion mode duration (in seconds)
#define MY_INCLUSION_MODE_DURATION 60
// Digital pin used for inclusion mode button
#define MY_INCLUSION_MODE_BUTTON_PIN  3

// Uncomment to override default HW configurations
//#define MY_DEFAULT_ERR_LED_PIN 4  // Error led pin
//#define MY_DEFAULT_RX_LED_PIN  6  // Receive led pin
//#define MY_DEFAULT_TX_LED_PIN  5  // the PCB, on board LED

#include <SPI.h>
#include <MySensors.h>  
#include <Bounce2.h>

// Enable repeater functionality for this node
#define MY_REPEATER_FEATURE


#define RELAY_1  5  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define RELAY_2  6
#define RELAY_3  7
#define RELAY_4  8

#define NUMBER_OF_RELAYS 4 // Total number of attached relays
#define RELAY_ON 0  // GPIO value to write to turn on attached relay
#define RELAY_OFF 1 // GPIO value to write to turn off attached relay

#define BUTTON_PIN A1
#define PIR_PIN A2

boolean buttonFlag;
unsigned long pirTime = 0;
unsigned long pirTime2 = 0;

void before() {
 for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
   // Then set relay pins in output mode
   pinMode(pin, OUTPUT);  
   // Set relay to last known state (using eeprom storage)
   digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
 }
}
Bounce debouncer = Bounce();
Bounce debouncer2 = Bounce();

void setup() {
 // Setup locally attached sensors
 delay(10000);
  // Setup the button.
 pinMode(BUTTON_PIN, INPUT_PULLUP);
 pinMode(PIR_PIN, INPUT_PULLUP);
 // After setting up the button, setup debouncer.
 debouncer.attach(BUTTON_PIN);
 debouncer.interval(25);
 debouncer2.attach(PIR_PIN);
 debouncer2.interval(25);
 //presentation();
}
void presentation()  
{  
 // Send the sketch version information to the gateway and Controller
 sendSketchInfo("Relay", "1.0");

 for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
   // Register all sensors to gw (they will be created as child devices)
   present(sensor, S_LIGHT);
 }
}

MyMessage msg(4, V_LIGHT);

void loop() {
 // Send locally attached sensor data here
 
 debouncer.update();
 debouncer2.update();
 
   if ( debouncer.fell()&& loadState(4) == HIGH ) {  // Call code if button transitions from HIGH to LOW
        saveState(4, !loadState(4));
        digitalWrite(RELAY_4, RELAY_ON);
        send(msg.set(loadState(4)));
        buttonFlag = true;
  }
  else if( debouncer.fell()&& loadState(4) == LOW ){
        saveState(4, !loadState(4)); //Store new state in eeprom
        digitalWrite(RELAY_4, RELAY_OFF);
        send(msg.set(loadState(4))); // Send new state
        buttonFlag = false;
  }
   
 if(buttonFlag == false){
   if(debouncer2.rose()) {  // Call code if button transitions from LOW to HIGH
       digitalWrite(RELAY_4, RELAY_ON);
       saveState(4, !RELAY_ON);  
       send(msg.set(loadState(4)));
     }
   
   else if(debouncer2.fell()) {  // Call code if button transitions from HIGH to LOW
     
      if(millis() - pirTime >= 4000UL) {
       digitalWrite(RELAY_4, RELAY_OFF);
       saveState(4, !RELAY_OFF);  
       send(msg.set(loadState(4)));
       pirTime = millis();
    }          
   }
 }  
}

void receive(const MyMessage &message) {
 // We only expect one type of message from controller. But we better check anyway.
 if (message.type==V_LIGHT) {
    // Change relay state
    digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
    // Store state in eeprom
    saveState(message.sensor, message.getBool());
    // Write some debug info
    Serial.print("Incoming change for sensor:");
    Serial.print(message.sensor);
    Serial.print(", New status: ");
    Serial.println(message.getBool());
  }
}
Powyżej wkleiłem pełny kod ale błąd jest gdzieś tu:
Kod:
void loop() {
 // Send locally attached sensor data here
 
 debouncer.update();
 debouncer2.update();
 
   if ( debouncer.fell()&& loadState(4) == HIGH ) {  // Call code if button transitions from HIGH to LOW
        saveState(4, !loadState(4));
        digitalWrite(RELAY_4, RELAY_ON);
        send(msg.set(loadState(4)));
        buttonFlag = true;
  }
  else if( debouncer.fell()&& loadState(4) == LOW ){
        saveState(4, !loadState(4)); //Store new state in eeprom
        digitalWrite(RELAY_4, RELAY_OFF);
        send(msg.set(loadState(4))); // Send new state
        buttonFlag = false;
  }
   
 if(buttonFlag == false){
   if(debouncer2.rose()) {  // Call code if button transitions from LOW to HIGH
       digitalWrite(RELAY_4, RELAY_ON);
       saveState(4, !RELAY_ON);  
       send(msg.set(loadState(4)));
     }
   
   else if(debouncer2.fell()) {  // Call code if button transitions from HIGH to LOW
     
      if(millis() - pirTime >= 4000UL) {
       digitalWrite(RELAY_4, RELAY_OFF);
       saveState(4, !RELAY_OFF);  
       send(msg.set(loadState(4)));
       pirTime = millis();
    }          
   }
 }  
}
 
Odpowiedź
  


Wiadomości w tym wątku
Sterowanie Światłem - przez AdamJab - 24-05-2019, 11:28
RE: Sterowanie Światłem - przez es2 - 24-05-2019, 13:21
RE: Sterowanie Światłem - przez AdamJab - 25-05-2019, 13:49
RE: Sterowanie Światłem - przez es2 - 25-05-2019, 14:34
RE: Sterowanie Światłem - przez kaczakat - 25-05-2019, 22:06
RE: Sterowanie Światłem - przez AdamJab - 25-05-2019, 23:14
RE: Sterowanie Światłem - przez AdamJab - 25-05-2019, 23:16
RE: Sterowanie Światłem - przez kaczakat - 26-05-2019, 01:07
RE: Sterowanie Światłem - przez es2 - 26-05-2019, 10:38
RE: Sterowanie Światłem - przez AdamJab - 31-05-2019, 21:19

Skocz do:


Przeglądający: 1 gości