I am not able to stop the blinds motor once the first command is sent.



  • I trying to build mysensor node for window curtains. I am not very good with programming. Can somebody please help me and point out what am I doing wrong. My code is as below

    #include <SPI.h>
    #include <MySensor.h>
    
    
    int motor_forward = 5;
    int motor_reverse = 6;
    
    int LDRinput = 1;     //analog pin to which LDR is connected, here we set it to 0 so it means A0
    int LDRValue = 0;      //that�s a variable to store LDR values
    int light_sensitivity = 500;    //This is the approx value of light surrounding your LDR
    
    int stopSwLeft = 7;
    int stopSwRight = 8;
    int stopSwNow = 2;
    
    int CHILD_CURTAIN_ID =1;
    int lastState;
    MySensor gw;
    MyMessage msg_S_COVER_U(CHILD_CURTAIN_ID, V_UP);
    MyMessage msg_S_COVER_D(CHILD_CURTAIN_ID, V_DOWN);
    MyMessage msg_S_COVER_S(CHILD_CURTAIN_ID, V_STOP);
    //MyMessage msg(CHILD_CURTAIN_ID,V_UP);
    // the setup routine runs once when you press reset:
    void setup()
    {
    	Serial.begin(115200);
    	// initialize the digital pin as an output for L239D.
    	pinMode(motor_forward, OUTPUT);
    	pinMode(motor_reverse, OUTPUT);
    	
    	// initialize the digital pin as an output for Stop Switches.
    	pinMode(stopSwLeft, INPUT);
    	pinMode(stopSwRight, INPUT);
    	pinMode(stopSwNow, INPUT);
    	
    	gw.begin(incomingMessage, AUTO, true);
    	// Send the sketch version information to the gateway and Controller
    	gw.sendSketchInfo("Window_Curtain", "1.0");
    	gw.present(CHILD_CURTAIN_ID, S_COVER);
    	
    }
    
    void loop(){
    	Serial.print("I am in loop begin");
    	gw.process();
    	Serial.print("GW.process done.");
    	//cover();
    
    	}
    void cover(int coverVal){
    
    	//int coverVal = gw.loadState(CHILD_CURTAIN_ID);
    
    	Serial.print("Cover is : ");
    	lastState = coverVal;
    	switch (coverVal) {
    	case 0:
    		Serial.println("Opening");
    			while (lastState == coverVal)
    			{
    				m_left();
    				coverVal = gw.loadState(CHILD_CURTAIN_ID);
    			}
    			gw.send(msg_S_COVER_U.set(V_UP));
    		break;
    	case 1:
    		Serial.println("Closing");
    			while (lastState == coverVal)
    			{
    				m_right();
    				coverVal = gw.loadState(CHILD_CURTAIN_ID);
    			}
    			gw.send(msg_S_COVER_D.set(V_DOWN));
    		break;
    	default:
    		Serial.println("Idle");
    			while (lastState == coverVal)
    				{
    					m_stop();
    					coverVal = gw.loadState(CHILD_CURTAIN_ID);
    				}
    			gw.send(msg_S_COVER_S.set(V_STOP));
    	}
    
    }
    void incomingMessage(const MyMessage &message) {
    	// We only expect one type of message from controller. But we better check anyway.
    	Serial.println("recieved incomming message");
    	switch (message.type) {
    	case V_UP:
    		gw.saveState(CHILD_CURTAIN_ID, 0);
    		Serial.print("Incoming change for ID_S_COVER:");
    		Serial.print(message.sensor);
    		Serial.print(", New status: ");
    		Serial.println("V_UP");
    		cover(gw.loadState(CHILD_CURTAIN_ID));
    		Serial.print("Done cover procedure");
    		//m_right();
    		break;
    
    	case V_DOWN:
    		gw.saveState(CHILD_CURTAIN_ID, 1);
    		Serial.print("Incoming change for ID_S_COVER:");
    		Serial.print(message.sensor);
    		Serial.print(", New status: ");
    		Serial.println("V_DOWN");
    		cover(gw.loadState(CHILD_CURTAIN_ID));
    		Serial.print("Done cover procedure");
    		//m_left();
    		break;
    
    	case V_STOP:
    		gw.saveState(CHILD_CURTAIN_ID, 2);
    		Serial.print("Incoming change for ID_S_COVER:");
    		Serial.print(message.sensor);
    		Serial.print(", New status: ");
    		Serial.println("V_STOP");
    		cover(gw.loadState(CHILD_CURTAIN_ID));
    		Serial.print("Done cover procedure");
    		//m_stop();
    		break;
    	}
    	Serial.print("exiting incoming message");
    }
    // the loop routine runs over and over again forever:
    void m_right() {
    	digitalWrite(motor_forward, HIGH); //terminal D1 will be HIGH
    	digitalWrite(motor_reverse, LOW); //terminal D2 will be LOW
    	Serial.print("Set For:High and Rev: Low");
    	return;
    }
    
    void m_left() {
    	digitalWrite(motor_forward, LOW); //terminal D1 will be LOW
    	digitalWrite(motor_reverse, HIGH); //terminal D2 will be HIGH
    	Serial.print("Set For:Low and Rev: High");
    	return;
    }
    
    void m_stop() {
    	digitalWrite(motor_forward, LOW); //terminal D1 will be LOW
    	digitalWrite(motor_reverse, LOW); //terminal D2 will be HIGH
    	Serial.print("Set For:Low and Rev: Low");
    	return;
    }
    
    
    //---------------------------------------------------------------------------------------------
    //void senselight()
    //{
    //	LDRValue = analogRead(LDRinput);      //reads the ldr�s value through LDR
    //	Serial.println(LDRValue);       //prints the LDR values to serial monitor
    //	delay(50);        //This is the speed by which LDR sends value to arduino
    //	
    //	if (LDRValue < light_sensitivity)
    //	{
    //		digitalWrite(13, HIGH);
    //	}
    //	else
    //	{
    //		digitalWrite(13, LOW);
    //	}
    //}
    

    I am using Domoticz as controller. Whats happening is once I press Open, the motor keeps spinning in one direction and on sending command to stop it does not stop. BTW I have L293D to control the motor.

    Appreciate any help I can get.


  • Mod

    I think you need to call gw.process inside your while loops. Otherwise no messages will be received so saveState will not be called, causing the code to loop endlessly.

    Looking at the debug output should reveal the problem.

    Also, when do you expect the motors to stop? I can't see where you detect that the cover has reached the endpoint.

    Adding a few comments would make it easier to follow what the code is supposed to do.



  • Appreciate your response. Will try gw.process in while loop. I am yet to add the code for endpoint of curtain. I will be using reed switch to detect end and start.



  • @mfalkvidd Thanks for your suggestion. It works great.


  • Mod

    Great! Thanks for reporting back.



  • Here is the sketch for Curtain Node.
    WindowCurtainShield.ino

    I have managed to solve a lot of problems, but some still exist.
    Works great with Domoticz..manual Buttons are still WIP.
    Some pictures for my first version of Node.
    I have left EEPROM and ATSHA204 unpopulated. Will learn more about it and then add.
    I will be using Dual Optiboot to support OTA.

    0_1456723308133_IMG_20160229_103922.jpg
    0_1456723353041_IMG_20160229_103930.jpg
    0_1456723391691_IMG_20160229_104552.jpg



  • @Suresh-Mali
    I tried your sketch but i'm having problems with reed switch bouncing (stop sensors) ,do you have any suggestions how to avoid this ? I'm using an arduino mini pro ,with magnetic reed switches .



  • @oscarc You can add debouncer.

      debouncer.attach(BUTTON_PIN);
      debouncer.interval(5);
    

    Refer the binary switch sensor sketch here http://www.mysensors.org/build/binary



  • @Suresh-Mali
    I already tried but it just jumps from forward to backward without stooping ,without the stops it works fine ,I push up and it moves ,I push the stop and stops but I really want to use the stops to prevent damage, I'm using an arduino mini pro with an external motor driver.

    #include <SPI.h>
    #include <MySensor.h>
    #include <Bounce2.h>
    
    int motor_forward = 5;
    int motor_reverse = 6;
    
    
    #define stopSwLeft 2
    #define stopSwRight  3
    
    
    int CHILD_CURTAIN_ID =1;
    int lastState;
    MySensor gw;
    MyMessage msg_S_COVER_U(CHILD_CURTAIN_ID, V_UP);
    MyMessage msg_S_COVER_D(CHILD_CURTAIN_ID, V_DOWN);
    MyMessage msg_S_COVER_S(CHILD_CURTAIN_ID, V_STOP);
    Bounce debouncer = Bounce(); 
    Bounce debouncer1 = Bounce();  
    void setup()
    {
      Serial.begin(115200);
      // initialize the digital pin as an output for L239D.
      pinMode(motor_forward, OUTPUT);
      pinMode(motor_reverse, OUTPUT);
      
      // initialize the digital pin as an output for Stop Switches.
      pinMode(stopSwLeft, INPUT);
      pinMode(stopSwRight, INPUT);
    digitalWrite(stopSwLeft,HIGH);
    digitalWrite(stopSwRight,HIGH);
     debouncer.attach(stopSwLeft);
      debouncer.interval(50);
      debouncer1.attach(stopSwRight);
      debouncer1.interval(50);
    
      
      gw.begin(incomingMessage, AUTO, true);
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Window_Curtain", "1.0");
      gw.present(CHILD_CURTAIN_ID, S_COVER);
      
    }
    
    void loop(){
      
      gw.process();
      
      
     
      }
    void cover(int coverVal){
    
      //int coverVal = gw.loadState(CHILD_CURTAIN_ID);
    
      Serial.print("Cover is : ");
      lastState = coverVal;
      switch (coverVal) {
      case 0:
        Serial.println("Opening");
          while (lastState == coverVal)
          {
            m_left();
            gw.process();
            checkHWInnputs();
            coverVal = gw.loadState(CHILD_CURTAIN_ID);
          }
          gw.send(msg_S_COVER_U.set(V_UP));
        break;
      case 1:
        Serial.println("Closing");
          while (lastState == coverVal)
          {
            m_right();
            gw.process();
            checkHWInnputs();
            coverVal = gw.loadState(CHILD_CURTAIN_ID);
          }
          gw.send(msg_S_COVER_D.set(V_DOWN));
        break;
      case 2:
        Serial.println("Idle");
          while (lastState == coverVal)
            {
              m_stop();
              gw.process();
              //checkHWInnputs();
              coverVal = gw.loadState(CHILD_CURTAIN_ID);
            }
          gw.send(msg_S_COVER_S.set(V_STOP));
          break;
      }
      return;
    
    }
    void incomingMessage(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      Serial.println("recieved incomming message");
      switch (message.type) {
      case V_UP:
        gw.saveState(CHILD_CURTAIN_ID, 0);
        Serial.print("Incoming change for ID_S_COVER:");
        Serial.print(message.sensor);
        Serial.print(", New status: ");
        Serial.println("V_UP");
        cover(gw.loadState(CHILD_CURTAIN_ID));
        Serial.print("Done cover procedure");
      
        break;
    
      case V_DOWN:
        gw.saveState(CHILD_CURTAIN_ID, 1);
        Serial.print("Incoming change for ID_S_COVER:");
        Serial.print("message.sensor");
        Serial.print(", New status: ");
        Serial.println("V_DOWN");
        cover(gw.loadState(CHILD_CURTAIN_ID));
        Serial.print("Done cover procedure");
       
        break;
    
      case V_STOP:
        gw.saveState(CHILD_CURTAIN_ID, 2);
        Serial.print("Incoming change for ID_S_COVER:");
        Serial.print("message.sensor");
        Serial.print(", New status: ");
        Serial.println("V_STOP");
        cover(gw.loadState(CHILD_CURTAIN_ID));
        Serial.print("Done cover procedure");
      
        break;
      }
      Serial.print("exiting incoming message");
      return;
      
    }
    //the loop routine runs over and over again forever:
    void m_right() {
      digitalWrite(motor_forward, HIGH); //terminal D1 will be HIGH
      digitalWrite(motor_reverse, LOW); //terminal D2 will be LOW
    
    }
    
    void m_left() {
      digitalWrite(motor_forward, LOW); //terminal D1 will be LOW
      digitalWrite(motor_reverse, HIGH); //terminal D2 will be HIGH
    
    }
    
    void m_stop() {
      digitalWrite(motor_forward, LOW); //terminal D1 will be LOW
      digitalWrite(motor_reverse, LOW ); //terminal D2 will be HIGH
    
    }
    
    void checkHWInnputs()
      {
     debouncer.update();
       debouncer1.update();
      int value = debouncer.read();
     int value1 = debouncer1.read();
     
      
       if (value == LOW) {
    
     Serial.println("Detected stop button push. Stopping");
         cover(1);
    
      }
     
    
       {
       if (value1 == LOW)
      Serial.println("Detected stop button push. Stopping");
          cover(0);
        
        }
      }
    


  • @oscarc The routine below i see some problem.

    void checkHWInnputs()
      {
     debouncer.update();
       debouncer1.update();
      int value = debouncer.read();
     int value1 = debouncer1.read();
     
      
       if (value == LOW) {
    
     Serial.println("Detected stop button push. Stopping");
         cover(1);
      }
    
       {
       if (value1 == LOW)
      Serial.println("Detected stop button push. Stopping");
          cover(0);
        
        }
    

    Once you detect the button state, you are calling

    cover(0);
    

    If you look at the cover routine, the parameters are used in case statement.
    0=Open
    1=Close
    2=Idle.
    My suggestion would be to change cover(0)/cover(1) to cover(2) which would stop the motor.


  • Mod

    In addition to Suresh-Mali's suggestion, you could add

    #define OPEN 0
    #define CLOSE 1
    #define IDLE 2
    

    and use cover(OPEN) / cover(CLOSE) / cover(IDLE) instead. That would make it easier to understand the intention of the code and easier to spot small mistakes.



  • @Suresh-Mali
    @mfalkvidd
    Thank you for your help ,I'll be trying it tonight and I let you know
    Thanks


Log in to reply
 

Suggested Topics

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts