Web Analytics Made Easy -
StatCounter Function with time event - CodingForum

Announcement

Collapse
No announcement yet.

Function with time event

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Function with time event

    Hi there...
    Probably you will find my question stupid but i dont have knowledge on javascript
    I found project for raspberry pi ,is a web server that controlling a relay.Just 2 buttons.. on,off .Nothing more,nothing less
    I need help with LightOn function.I want to give time event on that function.That relay controls the electric switch of my door and i would like while the on button is pressed,automatically after 3 seconds run light off function
    Thanks a lot



    function LightOn() {
    gpio.write(12, true, function(err) {
    if (err) console.log('Error writing to pin');
    console.log('Written to pin');
    });
    }

    function LightOff() {
    gpio.write(12, false, function(err) {
    if (err) console.log('Error writing to pin');
    console.log('Written to pin');
    });
    }

  • #2
    Add the line

    setTimeout(function(){LightOff}, 3000);
    at the end of the LightOn() function

    to trigger the LightOff function after 3 seconds.
    Last edited by Philip M; Sep 17, 2016, 03:48 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

    Comment


    • #3
      Thanks for the reply..but i cant make it work

      at the end where exactly?i placed it everywhere but nothing happened

      Comment


      • #4
        Before the closing brace } in the LightOn() function.


















        });
        }

        All the code given in this post has been tested and is intended to address the question asked.
        Unless stated otherwise it is not just a demonstration.

        Comment


        • #5
          i am very confused.it doesent work at all
          the js file is
          /*
          webgpio.js 1.00 Web GPIO switch
          ---------------------------------------------------------------------------------
          Visit projects.privateeyepi.com for full details

          J. Evans December 2013
          THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
          IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
          FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
          AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
          WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
          CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

          Revision History
          V1.00 - New
          V2 - Uses rpi-gpio library so we no longer need Quick2Wire
          ----------------------------------------------------------------------------------
          */

          var IPAddress;
          var http = require('http');
          var gpio = require("rpi-gpio");
          var connect = require('connect');
          serveStatic = require('serve-static');
          var os=require('os');
          var ifaces=os.networkInterfaces();
          var err;

          gpio.setup(12, gpio.DIR_OUT);

          function LightOn() {
          gpio.write(12, true, function(err) {
          if (err) console.log('Error writing to pin');
          console.log('Written to pin');
          });
          }


          function LightOff() {
          gpio.write(12, false, function(err) {
          if (err) console.log('Error writing to pin');
          console.log('Written to pin');
          });
          }

          for (var dev in ifaces) {
          ifaces[dev].forEach(function(details){
          if (details.family=='IPv4') {
          if (details.address!="127.0.0.1"){
          IPAddress=details.address;
          }
          }
          });
          At the lighton function i placed the setTimeout function,but the result remain the same...notihng
          function LightOn() {
          gpio.write(12, true, function(err) {
          setTimeout(function(){LightOff}, 2000);
          });
          }

          Comment


          • #6
            This is some kind of proprietory software, of which I have no experience. Try asking the software author.

            All the code given in this post has been tested and is intended to address the question asked.
            Unless stated otherwise it is not just a demonstration.

            Comment


            • #7
              Thanks for your time Philip...i will search it more
              At this time im using html meta to trigger that after 2 secs but i think that with javascript will be better.The point is i dont have any clue about javascript

              Comment


              • #8
                At the lighton function i placed the setTimeout function,but the result remain the same...notihng
                Code:
                function LightOn() {
                    gpio.write(12, true, function(err) {
                        setTimeout(function(){LightOff}, 2000);
                    });
                }
                At the first glance this seems to be the correct approach. I would recommend checking whether this anonymous function is called (by inserting a console.log?).

                Comment


                • #9
                  Originally posted by dimitris Geo View Post
                  Hi there...
                  Probably you will find my question stupid but i dont have knowledge on javascript
                  I found project for raspberry pi ,is a web server that controlling a relay.Just 2 buttons.. on,off .Nothing more,nothing less
                  I need help with LightOn function.I want to give time event on that function.That relay controls the electric switch of my door and i would like while the on button is pressed,automatically after 3 seconds run light off function
                  Thanks a lot
                  no question is stupid my dear! it may acknowledge the others, so carry on please...

                  Comment


                  • #10
                    setTimeout(function(){LightOff}, 2000); Invokes the anonymous function, which refers to the Lightoff function but doesn't call it. setTimeout(function(){LightOff()}, 2000); Would work but is superfluous. setTimeout(LightOff, 2000); Should be sufficient.

                    Comment


                    • #11
                      Thanks alot all of you!!!!
                      Goos you are great!Working perfect..THANK YOU

                      Comment

                      Working...
                      X