Motor Control with S_cover



  • Could some one please show me an example of how to use the s_cover commands?
    My project is an window blind with a dc motor an arduino mini pro 2 reed switches and an external motor driver ,I have little to no experience with programming to be honest ,I already tested another user sketch but the motor just does't stop once it hits the reed switch and I already tried debouncing the switch , Help is really appreciate it
    Thank you

    #include <SPI.h>
    #include <MySensor.h>
    
    
    int motor_forward = 5;
    int motor_reverse = 6;
    
    int stopSwLeft = 3;
    int stopSwRight = 4;
    //int stopSwNow = 7;
    
    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_PULLUP);
       pinMode(stopSwRight, INPUT_PULLUP);
       //pinMode(stopSwNow, INPUT_PULLUP);
       
       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");
       	//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");
       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
       exit;
    }
    
    void m_left() {
       digitalWrite(motor_forward, LOW); //terminal D1 will be LOW
       digitalWrite(motor_reverse, HIGH); //terminal D2 will be HIGH
       exit;
    }
    
    void m_stop() {
       digitalWrite(motor_forward,HIGH ); //terminal D1 will be LOW
       digitalWrite(motor_reverse, HIGH); //terminal D2 will be HIGH
       exit;
    }
    
    void checkHWInnputs()
       {
    
       	if (digitalRead(stopSwLeft) == LOW)
       	{
       cover(1);
       m_stop() ;
       	
     
       		
       	}
       	if (digitalRead(stopSwRight) == LOW)
       	{
       cover(0);
       	    m_stop() ;
       	
       				}
       }
    
    

  • Contest Winner



  • @TheoL
    not really 😞 ,I think I'm going to use relays instead of the motor controller
    Thanks.


Log in to reply
 

Suggested Topics

  • 4
  • 9
  • 2
  • 17
  • 2
  • 4

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts