What does 1 and 0 in send(msg.set(value==HIGH ? 1 : 0)) really means?



  • Hi. This is a begginer question but I cannot find the answer, I use Mysensors for a while and I have some difficulties using PIN 0 and 1. In reed switches I use send(msg.set(value==HIGH ? 1 : 0)), and in SSR relays I use for example digitalWrite(RELAY_PIN_1, loadState(CHILD_ID_RELAY1)?RELAY_ON:RELAY_OFF); (where
    #define RELAY_ON 1
    #define RELAY_OFF 0
    Please tell me if this is the problem that PIN 0 and 1 doesnt work?What to do to use this pins and maintain my recent PIN usage...?



  • @pl_rekin can you post some more of your code so it is a bit easier to see what you are trying to do.



  • Sure. Sorry

    I send you modified code. I use relays, DHT sensors, reeds and motion sensors in one sketch (almast all pins are used).
    In PIN 0 and 1 I use Reed Switches. Normally for Reed switches I want to use all pins in Arduino Mega from 0 to 21 (all pins work except 0 and 1 here...)

    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // 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
    
    // 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 
    
    #include <SPI.h>
    #include <MySensors.h>  
    #include <Bounce2.h>
    #include <DHT.h> // modified DHT11, DHT22 sensors library. Check Warning info!
    
    // -------------- PINS DEFINITION (Definicja Pinow) --------------
    
    /** ------- RGB LED (dioda RGB LED doustalania stanow)
     * RGB LED shows active status (using colors)
    */
    
    int redRGBLEDPin = A0;
    int greenRGBLEDPin = A1;
    int blueRGBLEDPin = A2;
    
    /** ------- RELAYS (przekazniki SSR, fotek)
     * Relays for Rack Moung (przekaznik SSR i Fotek - szafka rack)
    */
    
    #define RELAY_PIN_1 52 // PINs for Relays
    
    #define BUTTON_PIN A4 // PINS Buttons for Relays - to use normal wall switches (Przyciski / laczniki scienne)
    
    #define NUMBER_OF_RELAYS 10 // Total number of attached relays UNUSED!
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    
    // ------- Relay for save state inside Arduino's EEPROM
    
    bool loadedState1 = false;
    bool initialValueSent = false;
    
    /** ------- REED SWTCHES (kontaktrony)
     * Relays for Rack Moung (Kontaktron w szafce Rack i w szufladzie z Arduino)
    */
    
    // ------- PINs for Reeds
    #define REED_PIN_1 0 ' PIN 0
    #define REED_PIN_2 1 ' PIN 1
    
    int oldValue=-1;
    
    // ------- VARIABLES for reed switch
    
    int oldValue1=-1;
    int oldValue2=-1;
    bool state = false;
    
    /** ------- PIR SENSORS (czujki ruchu)
     * PIR Sensors for Rack Moung (sensory ruchu na obudowie Rack)
    */
    
    // ------- PINs for PIR sensor
    #define DIGITAL_INPUT_SENSOR1 23   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define DIGITAL_INPUT_SENSOR2 25
    #define SLEEP_NODE true
    
    // ------- VARIABLES for PIR sensor
    uint32_t SLEEP_TIME = 1000; // 120000; // Sleep time between reports (in milliseconds)
    
    int oldValueA = 0;
    int oldValueB = 0;
    
    bool stateA = false;
    bool stateB = false;
    
    /** ------- TEMPERATURE / HUMIDITY SENSOR (czujnik temperatury i wilgotnosci)
     * DHT22 sensors for checking temp and hum inside / outside Rack Mount (czujniki DHT22)
    */
    
    // ------- PINs for DHTs
    #define DHTPIN 22
    #define DHTPIN2 24
    #define DHTPIN3 26
    
    // ------- Initialize DHT sensor
    
    DHT dht1;
    DHT dht2;
    DHT dht3;
    
    unsigned long timeDHT = 30000; // 30 seconds
    
    unsigned long actualTime = 0;
    unsigned long actualTime2 = 0;
    unsigned long actualTime3 = 0;
    
    unsigned long recentTimeHum1 = 0;
    unsigned long recentTimeHum2 = 0;
    unsigned long recentTimeHum3 = 0;
    
    float lastTemp1;
    float lastTemp2;
    float lastTemp3;
    
    float lastHum1;
    float lastHum2;
    float lastHum3;
    
    boolean metric = true;
    
    // -------------- CHILD IDs (ID wszystkich sensorow) --------------
    
    #define CHILD_ID_REED1 0 
    #define CHILD_ID_REED2 10 
    
    #define CHILD_ID_TEMP1 1
    #define CHILD_ID_HUM1 2
    
    #define CHILD_ID_MOTION1 3
    #define CHILD_ID_MOTION2 4
    
    #define CHILD_ID_RELAY1 5
    
    #define CHILD_ID_TEMP2 6
    #define CHILD_ID_HUM2 7
    
    #define CHILD_ID_TEMP3 8
    #define CHILD_ID_HUM3 9
    
    // ----------------------------------------------------------------
    
    void before() {
    	for (int sensor=1, pin=RELAY_PIN_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);
    	}
    }
    
    // Reed switches needs debouncers
    Bounce debouncer_reed1 = Bounce();
    Bounce debouncer_reed2 = Bounce();
    
    //Relays switched - don't need debouncers
    Bounce debouncer_relay1 = Bounce();
    Bounce debouncer = Bounce();
    
    // Debouncer for Motion sensor
    Bounce debouncerA = Bounce();
    Bounce debouncerB = Bounce();
    
    // -------------- CHILD Initialization (inicjalizacja czujnikow) --------------
    
    // Initialize Relay
    MyMessage msg(CHILD_ID_RELAY1, V_STATUS);
    
    // Initialize reed switches
    MyMessage msgReed1(CHILD_ID_REED1, V_TRIPPED);
    MyMessage msgReed2(CHILD_ID_REED2, V_TRIPPED);
    
    // Initialize DHT22
    MyMessage msgTemp1(CHILD_ID_TEMP1, V_TEMP);
    MyMessage msgHum1(CHILD_ID_HUM1, V_HUM);
    
    MyMessage msgTemp2(CHILD_ID_TEMP2, V_TEMP);
    MyMessage msgHum2(CHILD_ID_HUM2, V_HUM);
    
    MyMessage msgTemp3(CHILD_ID_TEMP3, V_TEMP);
    MyMessage msgHum3(CHILD_ID_HUM3, V_HUM);
    
    // Initialize motion message
    MyMessage msg_motion1(CHILD_ID_MOTION1, V_TRIPPED);
    MyMessage msg_motion2(CHILD_ID_MOTION2, V_TRIPPED);
    
    // ----------------------------------------------------------------
    
    void setup() {
    
       // Setup RGB Led pins 
      pinMode(redRGBLEDPin, OUTPUT);
      pinMode(greenRGBLEDPin, OUTPUT);
      pinMode(blueRGBLEDPin, OUTPUT);     
    
       // Setup locally attached sensors
    	delay(100); 
    
    	// Setup DHT sensors
    	dht1.setup(DHTPIN);
    	dht2.setup(DHTPIN2);
    	dht3.setup(DHTPIN3);
    
    	// Setup the button.
    	pinMode(BUTTON_PIN, INPUT_PULLUP);
    	// After setting up the button, setup debouncer.
    	debouncer.attach(BUTTON_PIN);
    	debouncer.interval(5);
    	debouncer_relay1.attach(RELAY_PIN_1);
    	debouncer_relay1.interval(5);
     
    	// Setup the reed PINs
    	pinMode(REED_PIN_1,INPUT_PULLUP);
    	debouncer_reed1.attach(REED_PIN_1);
    	debouncer_reed1.interval(10);
      pinMode(REED_PIN_2,INPUT_PULLUP);
      debouncer_reed2.attach(REED_PIN_2);
      debouncer_reed2.interval(10);
               
    
    	// Make sure relays are off when starting up
    	digitalWrite(RELAY_PIN_1, RELAY_OFF);
    	// Then set relay pins in output mode
    	pinMode(RELAY_PIN_1, OUTPUT);
    
    	// Set relay to last known state (using eeprom storage) 
    	loadedState1 = loadState(CHILD_ID_RELAY1);
    
    	// setup debouncer for Reed switch
    	debouncer_reed1.attach(REED_PIN_1);
    	debouncer_reed1.interval(5);
      debouncer_reed2.attach(REED_PIN_2);
      debouncer_reed2.interval(5);        
    
    	pinMode(REED_PIN_1,INPUT);
    	digitalWrite(REED_PIN_1,HIGH);
      pinMode(REED_PIN_2,INPUT);
      digitalWrite(REED_PIN_2,HIGH);
    
    	// Setup PIR Motion sensor "button"
    	pinMode(DIGITAL_INPUT_SENSOR1, INPUT_PULLUP); // Setup the button Activate internal pull-up
    	pinMode(DIGITAL_INPUT_SENSOR2, INPUT_PULLUP); //  sets the motion sensor digital pin as input / Setup the button Activate internal pull-up
    
    	// setup debouncer for motion sensor
    	debouncerA.attach(DIGITAL_INPUT_SENSOR1);
    	debouncerA.interval(5);
    
    	debouncerB.attach(DIGITAL_INPUT_SENSOR2);
    	debouncerB.interval(5);
    
    }
    
    // ----------------------------------------------------------------
    
    void presentation()  {
    	
    	sendSketchInfo("Arduino6", "1.0");
    
    	// Register all sensors to gw (gateway - they will be created as child devices)
    	present(CHILD_ID_RELAY1, S_BINARY);
    
    	present(CHILD_ID_REED1, S_DOOR);  
      present(CHILD_ID_REED2, S_DOOR);
    
    	present(CHILD_ID_TEMP1, S_TEMP);
    	present(CHILD_ID_HUM1, S_HUM);
    
    	present(CHILD_ID_TEMP2, S_TEMP);
    	present(CHILD_ID_HUM2, S_HUM);
    
    	present(CHILD_ID_TEMP3, S_TEMP);
    	present(CHILD_ID_HUM3, S_HUM);  
    
    	present(CHILD_ID_MOTION1, S_MOTION);
    	present(CHILD_ID_MOTION2, S_MOTION);
      
    }
    
    // ----------------------------------------------------------------
    
    void loop() {
    	actualTime = millis();
    
    	/**
    	 * ------- DHT22 SENSOR
    	*/
    
    	if (actualTime - recentTimeHum1 >= timeDHT) {	
    
    		recentTimeHum1 = actualTime;
    
    		float temperature1 = dht1.getTemperature();
    		if (isnan(temperature1)) {		
    		} else if (temperature1 != lastTemp1) {
    			lastTemp1 = temperature1;
    		if (!metric) {
    			temperature1 = dht1.toFahrenheit(temperature1);
    		}
    			send(msgTemp1.set(temperature1, 1));
    		}
    
    		float humidity1 = dht1.getHumidity();
    		if (isnan(humidity1)) {
    		} else if (humidity1 != lastHum1) {
    			lastHum1 = humidity1;
    			send(msgHum1.set(humidity1, 1));
    		}
    
    		float temperature2 = dht2.getTemperature();
    		if (isnan(temperature2)) {
    		} else if (temperature2 != lastTemp2) {
    			lastTemp2 = temperature2;
    		  if (!metric) {
    			temperature2 = dht2.toFahrenheit(temperature2);
    		  }
    			send(msgTemp2.set(temperature2, 1));
    		}
    
    		float humidity2 = dht2.getHumidity();
    		if (isnan(humidity2)) {
    		} else if (humidity2 != lastHum2) {
    			lastHum2 = humidity2;
    			send(msgHum2.set(humidity2, 1));
    		}
    
    		float temperature3 = dht3.getTemperature();
    		if (isnan(temperature3)) {
    		} else if (temperature3 != lastTemp3) {
    			lastTemp3 = temperature3;
    		  if (!metric) {
    			temperature3 = dht3.toFahrenheit(temperature3);
    		  }
    			send(msgTemp3.set(temperature3, 1));
    		}
    
    		float humidity3 = dht3.getHumidity();
    		if (isnan(humidity3)) {
    		} else if (humidity3 != lastHum3) {
    			lastHum3 = humidity3;
    			send(msgHum3.set(humidity3, 1));
    		}
    	}
    
    	/**
    	 * ------- MOTION SENSORS
    	*/
    
    	debouncerA.update(); // Get the update value
    	int valueA = debouncerA.read();
    
    	if (valueA != oldValueA) {
    		// Send in the new value
    		send(msg_motion1.set(valueA == HIGH ? 1 : 0)); // Send tripped value to gw
    		oldValueA = valueA;
        setColor(255, 0, 0);  // red
    		wait(5);
    	}    
    
    	debouncerB.update(); 
    	int valueB = debouncerB.read();
    
    	if (valueB != oldValueB) {
    		send(msg_motion2.set(valueB == HIGH ? 1 : 0));
    		oldValueB = valueB;
        setColor(255, 0, 0);  // red
    		wait(5);
    	}  
    	
    	/**
    	 * ------- SIGNAL-BASED SENSORS
    	*/
    	
    	if (!initialValueSent) {
    
    		// ------- RELAYS
    
    		send(msg.set(loadedState1?RELAY_ON:RELAY_OFF));
    
    		// -------  REED SWITCHES
    		
    		int value1 = debouncer_reed1.read();
    		send(msgReed1.set(value1==HIGH ? 1 : 0));
    		wait(5);
        int value2 = debouncer_reed2.read();
        send(msgReed2.set(value2==HIGH ? 1 : 0));
        wait(5);
                         
    
    		request(CHILD_ID_RELAY1, V_STATUS);
       
    		request(CHILD_ID_REED1, V_TRIPPED);
        request(CHILD_ID_REED2, V_TRIPPED);
                     
        
    		// -------  SHORT WAIT TIME
    		wait(2000, C_SET, V_STATUS);
    
    	}
    
    	// -------  Send locally attached sensor data here 
    	
    	if (debouncer.update()) {
    		// -------  Get the update value.
    		int value = debouncer.read();
    		
    		// -------  Send in the new value.
    		if(value != oldValue){
    			saveState(CHILD_ID_RELAY1, !loadState(CHILD_ID_RELAY1));
    			digitalWrite(RELAY_PIN_1, loadState(CHILD_ID_RELAY1)?RELAY_ON:RELAY_OFF);
    			send(msg.set(loadState(CHILD_ID_RELAY1)));
    			oldValue = value;
          //setColor(0, 0, 255);  // blue
    		}
    	}
    
    	if (debouncer_reed1.update()) {
    
    		int value1 = debouncer_reed1.read();
    		
    		if (value1 != oldValue1) {
    			send(msgReed1.set(value1==HIGH ? 1 : 0));
    			oldValue1 = value1;
          setColor(0, 255, 0);  // green
    		}
    	}
    
      if (debouncer_reed2.update()) {
    
        int value2 = debouncer_reed2.read();
        
        if (value2 != oldValue2) {
          send(msgReed2.set(value2==HIGH ? 1 : 0));
          oldValue2 = value2;
          setColor(0, 255, 0);  // green
        }
      }
    
        
    }
    
    // ----------------------------------------------------------------
    
    void receive(const MyMessage &message) {
    
    	if (message.type == V_STATUS) {
    		if (!initialValueSent) {
    		  initialValueSent = true; //Serial.println("Receiving initial value from controller");
    		}
    	
    		switch (message.sensor) {
    			case CHILD_ID_RELAY1:
    				loadedState1 =  message.getBool();
    				digitalWrite(RELAY_PIN_1, loadedState1 ? RELAY_ON : RELAY_OFF);
    				saveState(CHILD_ID_RELAY1, loadedState1);
    				send(msg.set(loadedState1?RELAY_ON:RELAY_OFF));
            setColor(0, 0, 255);  // blue
    			break;
    
    		}
    
    	}
    }
    
    // --- For RGB Led -------------------------------------------------------------
    void setColor(int red, int green, int blue)
    {
      #ifdef COMMON_ANODE
        red = 255 - red;
        green = 255 - green;
        blue = 255 - blue;
      #endif
      analogWrite(redRGBLEDPin, red);
      analogWrite(greenRGBLEDPin, green);
      analogWrite(blueRGBLEDPin, blue);  
    }
    


  • The code that you posted:
    send(msg.set(value==HIGH ? 1 : 0))
    Translates to this
    If ”value” is HIGH (true) then send 1, if not send 0

    It has nothing to do with PIN 0 or 1

    The same goes for
    digitalWrite(RELAY_PIN_1, loadState(CHILD_ID_RELAY1)?RELAY_ON:RELAY_OFF);
    It translates to
    On RELAY_PIN_1, if it’s state is 1 write HIGH to RELAY_PIN_1 else write LOW.

    If you use serial in your sketch you can’t use PIN 0 or 1 as those pins are TX and RX.



  • @mickecarlsson Thanks. You explained that great 🙂 I done some rework in my project and I am not using PIN 0 and 1 anymore.



  • @pl_rekin It's great when the 'penny drops' - Learning is not so bad when you can admit not knowing something and move on with enhanced knowledge! 🙂


Log in to reply
 

Suggested Topics

  • 3
  • 5
  • 6
  • 1
  • 2

7
Online

11.4k
Users

11.1k
Topics

112.7k
Posts