Motor and proximity sensors - continue for 1 second after sensor is tripped


hi all,

i have posted project before , i'm in final stages here.  (hopefully) last problem 1 hope simple fix i'll wait see advice may have.  in project i'm using arduino mega, wifi shield, , motor controller board large dc motor open , close gate.  motor cycles between 2 proximity sensors tell when gate open or closed.  i'm trying add code motor continue turning 1 second after closing proximity sensor triggered ensure gate shut tightly.  i've tried few different delay functions it's bit tricky because long proximity sensor still reading low, keeps looping , motor never stops, causing bear down super tight , try break itself. or stops reads proximity sensor.  advice?


code: [select]


#include <spi.h>
#include <wifi.h>

const int dir = 35;
const int pwm = 34;
const int proxypin = 40;
const int proxypin2 = 41;


char ssid[] = "";      //  ssid
char pass[] = "";   // password
int keyindex = 0;                 
int proxystate = 0;
int proxystate2 = 0;


int status = wl_idle_status;
wifiserver server(80);

void setup() {
  serial.begin(9600);     

  ////////////////////////////// hardware i/o /////////////////////////////////
 
  pinmode(pwm, output);     
  pinmode(dir, output);
  pinmode(proxypin, input);
  pinmode(proxypin2, input);
 
////////////////////////////////// wifi startup /////////////////////////////////

  // check presence of shield:
  if (wifi.status() == wl_no_shield) {
    serial.println("wifi shield not present");
    while (true);       // don't continue
  }

  string fv = wifi.firmwareversion();
  if (fv != "1.1.0") {
    serial.println("please upgrade firmware");
  }

  // attempt connect wifi network:
  while (status != wl_connected) {
    serial.print("attempting connect network named: ");
    serial.println(ssid);                   // print network name (ssid);

    // connect wpa/wpa2 network. change line if using open or wep network:
    status = wifi.begin(ssid, pass);
    // wait 10 seconds connection:
    delay(10000);
  }
  server.begin();                           
  printwifistatus();                       
}


void loop() {

/////////////////////////////////// wifi loop ////////////////////////////////////

  wificlient client = server.available();   // listen incoming clients

  if (client) {                             // if client,
    serial.println("new client");           // print message out serial port
    string currentline = "";                // make string hold incoming data client
    while (client.connected()) {            // loop while client's connected
      if (client.available()) {             // if there's bytes read client,
        char c = client.read();             // read byte, then
        serial.write(c);                    // print out serial monitor
        if (c == '\n') {                    // if byte newline character


    //////////////////////////////// interface design /////////////////////////////////////

          if (currentline.length() == 0) {
            // http headers start response code (e.g. http/1.1 200 ok)
            // , content-type client knows what's coming, blank line:
            client.println("http/1.1 200 ok");
            client.println("content-type:text/html");
            client.println();

           
            client.print("<title>");
               client.print("autovalve control page");
            client.print("</title>");
           
            client.print("<br><br>");
           
            client.print("<head>");
               client.print("<div style='font-size:60px'>");
                  client.print("<center> autovalve control page </center>");
               client.print("</div>");
            client.print("</head>");
           
            client.print("<br><br>");
           
            // content of http response follows header:
            client.print("<body>");
                 client.print("<div style='font-size:60px'>");
                    client.print("<center> <a href=\"/open\">open</a> </center>");
                    client.print("<br>");
                    client.print("<center> <a href=\"/close\">close</a> </center>");
                 client.print("</div>");
                 client.print("<br><br>");

             
                if (proxystate == low){
                  client.print("<div style='font-size:45px'>");
                     client.print("<center> current position: closed </center>");
                  client.print("</div>");
               }
               
               if (proxystate2 == low){
                  client.print("<div style='font-size:45px'>");
                      client.print("<center>  current position: open </center>");
                  client.print("</div>");
               }

           client.print("</body>");

             
            // http response ends blank line:
            client.println();
            // break out of while loop:
            break;
          } else {    // if got newline, clear currentline:
            currentline = "";
          }
        } else if (c != '\r') {  // if got else carriage return character,
          currentline += c;      // add end of currentline
        }



 ///////////////hardware control loop//////////////////////////

 digitalwrite(pwm, low);

 proxystate = digitalread(proxypin);
 proxystate2 = digitalread(proxypin2);
 
   if (currentline.endswith("get /open") && (proxystate == low)) {
     digitalwrite(pwm, high);  //turn motor on
     digitalwrite(dir, high);  //in opening direction
   
         while(digitalread(proxypin2) == high){     //as long proxy2 not triggered
            digitalwrite(pwm, high);                  //keep motor on
            digitalwrite(dir, high);                //in opening direction
               }
               }
       if(proxystate2 == low){
         digitalwrite(pwm, low);     //when proxy2 triggered, turn motor off
             }



             
  if (currentline.endswith("get /close") && (proxystate2 == low)) {
     digitalwrite(pwm, high);  //turn motor on
     digitalwrite(dir, low);  //in closing direction
   
      while(digitalread(proxypin) == high){     //as long proxy1 not triggered
         digitalwrite(pwm, high);                //keep motor on
         digitalwrite(dir, low);                //in closing direction
    }
    }
   if(proxystate == low){                 //***continue 1 second after proxypin reads low
    delay(1000);
    digitalwrite(pwm, low);                //***not working***
        }
      }
     
    }
 ///////////////////////////////////// wifi close loop //////////////////////////////////
    // close connection:
    client.stop();
    serial.println("client disonnected");
  }
}

void printwifistatus() {
  // print ssid of network you're attached to:
  serial.print("ssid: ");
  serial.println(wifi.ssid());

  // print wifi shield's ip address:
  ipaddress ip = wifi.localip();
  serial.print("ip address: ");
  serial.println(ip);

  // print received signal strength:
  long rssi = wifi.rssi();
  serial.print("signal strength (rssi):");
  serial.print(rssi);
  serial.println(" dbm");
  // print go in browser:
  serial.print("to see page in action, open browser http://");
  serial.println(ip);
}

quote
i've tried delay function it's bit tricky because long proximity sensor still reading low, keeps looping , motor never stops, causing bear down super tight , try break itself.
try statement again without using "it". have no idea "it" refers to.

code: [select]
const int proxypin = 40;
const int proxypin2 = 41;

so, have 2 proximity sensors numbered uh-huh , two. why? 1 , 2 make more sense.

code: [select]
    digitalwrite(pwm, low);                //***not working***
it working. next iteration of loop() find proximity sensor low, because gate closed, try run motor more.

you need variable keep track of state of gate - open, opening, closed, closing, etc. when gate closing, , proximity sensor becomes low, run motor 1 second longer, shut off , set state closed.

if gate closed, don't try close more.


Arduino Forum > Using Arduino > Programming Questions > Motor and proximity sensors - continue for 1 second after sensor is tripped


arduino

Comments

Popular posts from this blog

Error compiling for board Arduino/Genuino Uno.

Installation database is corrupt

esp8266 (nodemcu 0.9) client.write très lent ???