Skip to content

Pin direction change #23

@0xceb1d

Description

@0xceb1d

Apologies if I have miss understood your documentation, however, when implementing your library for my project I am having trouble with switching the direction of a pin from out to in after sending a signal to it (working with a proximity sensor).

This is my code:

// Distance change listener

var gpioProximity = gpio.export(18, {
    direction: 'in',
    ready: function() {
        logger.info('GPIO pin 18 set up as proximity sensor');
        distanceReady = true;
        distanceCheck();
        gpioProximity.on("change", function(val){
            logger.info('GPIO pin 18 seen change');
            if (pingSent == true){
                if (val == 1) {
                    endTime = Date.now();
                    logger.info('End ' + endTime);
                    logger.info('Ping received');
                }
            }
        })
   }
});  

// Distance check

function distanceCheck(){

    distanceChecking = setInterval(function(){
        gpioProximity.setDirection("out");
        setTimeout(gpioProximity.set(1),100);
        setTimeout(gpioProximity.reset(),100);
        startTime = Date.now();
        logger.info('Start ' + startTime);
        pingSent = true;
        gpioProximity.setDirection("in");
        setTimeout(pingSent = false, 500);
        // MATH
        // UPDATE DISTANCE
    }, 2000);
}

This is the console log:

info: GPIO pin 18 set up as proximity sensor
info: Start 1386161269665
EPERM write /sys/class/gpio/gpio18/value
info: Start 1386161271662
EPERM write /sys/class/gpio/gpio18/value
info: Start 1386161273660
EPERM write /sys/class/gpio/gpio18/value
info: Start 1386161275661
EPERM write /sys/class/gpio/gpio18/value
info: Start 1386161277660
EPERM write /sys/class/gpio/gpio18/value
info: Start 1386161279660
EPERM write /sys/class/gpio/gpio18/value
info: Start 1386161281660
EPERM write /sys/class/gpio/gpio18/value

For reference, this is the python script I am basing this on:

import time
import RPi.GPIO as GPIO

    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(12, GPIO.OUT)

    GPIO.output(12, 0)
    time.sleep(0.000002)

    GPIO.output(12, 1)
    time.sleep(0.000005)

    GPIO.output(12, 0)
    GPIO.setup(12, GPIO.IN)

    while GPIO.input(12)==0:
            starttime=time.time()

    while GPIO.input(12)==1: 
            endtime=time.time()

    duration=endtime-starttime
    distance=duration*34000/2
    print str(x + 1) + ": " + str(distance)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions