function goArduino(){
    //console.log('got here')
    if(board){
        //console.log('and here?')
        board.digitalWrite(LEDPIN, 1);
        setTimeout(function(){board.digitalWrite(LEDPIN, 0)}, pulseDuration);
    }
}
Example #2
0
  this.loop(100, function() {
    board.digitalWrite( 36, ledValue = (ledValue ? 0 : 1));
    var distance = (sensors['left'].value + sensors['right'].value + sensors['up'].value + sensors['down'].value) / 4;

    //console.log(distance);
    if (distance > 300) {
      shakeHead();
      moveBackward();
      moveForward();
      /*console.log("------------------------------------------------");
      console.log("Left: " + sensors['left'].value);
      console.log("Right: " + sensors['right'].value);
      console.log("Up: " + sensors['up'].value);
      console.log("Down: " + sensors['down'].value);
      console.log("------------------------------------------------");*/
    }
  })
Example #3
0
myBoard.on("ready", function() {

    lcd = new j5.LCD({pins: [8, 9, 4, 5, 6, 7]});

    myBoard.digitalWrite(10, 1);
    setTimeout(function() {
        lcd.clear().cursor(0, 0);
        lcd.clear().print("Authorise");
        lcd.cursor(1, 0);
        lcd.print("your self");
        //printSeconds.call(this);
        //printString('This is very long text to display on the our small screen!', 400);
    }, 50);

  this.repl.inject({
  	lcd: lcd,
    board: myBoard
  });
  console.log("You can interact with the RGB LED via the variable 'led' e.g. led.on();\n Hit control-D to exit.\n >> ");
});
Example #4
0
 this.loop(100, function() {
   board.digitalWrite(LED1, led1Status? 1: 0);
   board.digitalWrite(LED2, led2Status? 1: 0);
 });
        connection.on('message', function (message) {
            var pin; // the pin number contained within a message
            var index; // an index for the devices array
            var deviceID; // the device identifier contained within a message

            if (debugLevel >= 1) {
                console.log('message received: ' + message.utf8Data);
            }

            // Messages components are delimited by '/'.
            // A message is split into its components and placed in msg array.
            // The message ID is always the first element
            // TODO: explore using JSON instead of current delimitation scheme
            var msg = message.utf8Data.split('/');

            // Process each message type received
            switch (msg[0]) {
                // handshake with client at start up of socket
                case 'Xi4sOnline':
                    if (debugLevel >= 1) {
                        console.log('Xi Server has a websocket to Xi4S');
                    }
                    break;

            /**********************************************************************************************
             *********************   PIN MODE COMMAND HANDLERS    *****************************************
             **********************************************************************************************/

                // set pin mode to analog in
                // create an an instance of an analog sensor and add it to the array
                case 'setAnalogIN':
                    // get the pin number and add the 'analog' designator
                    pin = msg[3];
                    if (debugLevel >= 1) {
                        console.log("setAnalogIn", msg);
                    }
                    if (validateAnalogSetMode(msg[1], pin) === false) {
                        break;
                    }
                    // specific to XiDuino - needs to see the 'A' designator
                    if (serverType === 'ard') {
                        pin = 'A' + pin;
                    }
                    index = devices.length;
                    deviceID = msg[1];

                    addSensor(index, pin, deviceID);
                    break;
                // set pin mode to digital in
                // create a switch object to handle a digital input pin and add it to the array
                case 'setDigitalIN':
                    pin = msg[2];
                    if (validateDigitalSetMode(msg[1], pin, five.Pin.INPUT) === false) {
                        break;
                    }
                    index = devices.length;
                    deviceID = msg[3];
                    if (debugLevel >= 2) {
                        console.log("adding switch", pin, deviceID);
                    }
                    addSwitch(index, pin, deviceID);
                    break;
                // set up pin as PWM - we do not create device objects for output pins, but access them directly
                case 'setAnalogOUT':
                    pin = msg[2];
                    // PWM is considered here to be a digital mode
                    if (validateDigitalSetMode(msg[1], pin, five.Pin.PWM) !== false) {
                        board.pinMode(pin, five.Pin.PWM);
                    }
                    break;
                // set up pin as digital output
                // we do not create device objects for output pins, but access them directly
                case 'setDigitalOUT':
                    pin = msg[2];
                    // just set the pin (msg[2], to the output mode.
                    if (validateDigitalSetMode(msg[1], pin, five.Pin.OUTPUT) !== false) {
                        board.pinMode(pin, five.Pin.OUTPUT);
                    }
                    break;
                // This will set a pin to servo mode and add a standard servo device
                case 'setStandardServoMode':
                    pin = msg[2];
                    if (debugLevel >= 3) {
                        console.log('setServoMode current mode === ' + board.io.pins[pin].mode);
                        console.log('pin = ' + pin);
                        console.log(msg);
                    }
                    if (validateDigitalSetMode(msg[1], pin, five.Pin.SERVO) !== false) {
                        board.pinMode(pin, five.Pin.SERVO);
                        if (debugLevel >= 2) {
                            console.log("adding servo", pin);
                        }
                        addServo(pin, 'standard', false);
                    }
                    break;
                // This will set a pin to Servo and add a continuous servo device
                case 'setContinuousServoMode':
                    inverted = false;
                    pin = msg[2];
                    if (debugLevel >= 3) {
                        console.log('setServoMode current mode === ' + board.io.pins[pin].mode);
                        console.log('pin = ' + pin);
                        console.log(msg);
                    }

                    if (validateDigitalSetMode(msg[1], pin, five.Pin.SERVO) !== false) {
                        board.pinMode(pin, five.Pin.SERVO);
                        if (debugLevel >= 2) {
                            console.log("adding servo", pin);
                        }
                        addServo(pin, 'continuous');
                    }
                    break;
                // Set a pin for HC-SR04 type support and a ping device
                // Requires a replacement sketch for StandardFirmata
                // https://github.com/rwaldron/johnny-five/wiki/Ping#setup
                case 'setSonarMode':
                    deviceID = msg[3];
                    pin = msg[2];
                    if (debugLevel >= 1) {
                        console.log("setSonarMode", msg);
                    }
                    if (validateDigitalSetMode(msg[1], pin, five.Pin.INPUT) === false) {
                        break;
                    }
                    index = devices.length;
                    addPing(index, pin, deviceID);
                    break;
                // set a pin for infrared distance sensing and add the device
                case 'setInfraRedDistanceMode':
                    deviceID = msg[3];
                    pin = msg[2];
                    if (debugLevel >= 1) {
                        console.log("setInfraRedDistanceMode", msg);
                    }
                    if (validateAnalogSetMode(msg[1], pin, five.Pin.ANALOG) === false) {
                        break;
                    }
                    index = devices.length;
                    addInfraRed(index, pin, deviceID);
                    break;
                // set a pin to piezo tone mode and add the device
                case 'setToneMode':
                    pin = msg[2];
                    if (validateDigitalSetMode(msg[1], pin, five.Pin.OUTPUT) !== false) {
                        addPiezo(pin);
                    }
                    break;
                // The stepper motor support requires a replacement sketch for StandardFirmata
                //  https://github.com/soundanalogous/AdvancedFirmata
                // set the pins for a 4 wire stepper and a 4 wire stepper device
                case 'fourWireStepperPins':
                    var pinA = parseInt(msg[2]);
                    var pinB = parseInt(msg[3]);
                    var pinC = parseInt(msg[4]);
                    var pinD = parseInt(msg[5]);
                    var revSteps = parseInt(msg[6]);
                    if (debugLevel >= 1) {
                        console.log("fourWireStepperPin", msg);
                    }
                    if (validateDigitalSetMode(msg[1], pinA, five.Pin.OUTPUT) === false) {
                        break;
                    }
                    if (validateDigitalSetMode(msg[1], pinB, five.Pin.OUTPUT) === false) {
                        break;
                    }
                    if (validateDigitalSetMode(msg[1], pinC, five.Pin.OUTPUT) === false) {
                        break;
                    }
                    if (validateDigitalSetMode(msg[1], pinD, five.Pin.OUTPUT) === false) {
                        break;
                    }
                    addFourWireStepper(pinA, pinB, pinC, pinD, revSteps);
                    break;
                // The stepper motor support requires a replacement sketch for StandardFirmata
                //  https://github.com/soundanalogous/AdvancedFirmata
                // set the pins for a stepper driver board and a driver stepper device
                case 'stepperDriverPins':
                    if (debugLevel >= 1) {
                        console.log("stepperDriverPins", msg);
                    }
                    pinA = parseInt(msg[2]);
                    pinB = parseInt(msg[3]);
                    revSteps = parseInt(msg[4]);
                    if (validateDigitalSetMode(msg[1], pinA, five.Pin.OUTPUT) === false) {
                        break;
                    }
                    if (validateDigitalSetMode(msg[1], pinB, five.Pin.OUTPUT) === false) {
                        break;
                    }
                    addDriverStepper(pinA, pinB, revSteps);
                    break;

            /**********************************************************************************************
             *********************   COMMAND BLOCK HANDLERS             **********************************
             **********************************************************************************************/

                // write out the digital value to the pin
                case 'digitalWrite':
                    if (debugLevel >= 3) {
                        console.log('digitalWrite: board ' + msg[1] + 'pin' + msg[2] + ' value ' + msg[3]);
                    }
                    // validate that this pin was initially set to correct mode
                    if (board.io.pins[msg[2]].mode !== five.Pin.OUTPUT) {
                        connection.send('invalidPinCommand/' + 2 + '/' + msg[1] + '/' + msg[2]);
                    }
                    else {
                        if (msg[3] === 'Off') {
                            if (debugLevel >= 3) {
                                console.log(' pin off');
                            }
                            board.digitalWrite(msg[2], 0);
                        }
                        else {
                            if (debugLevel >= 3) {
                                console.log(' pin on');
                            }
                            board.digitalWrite(msg[2], 1);
                        }
                    }
                    break;
                // write out the analog value to the PWM pin
                case 'analogWrite':
                    pin = msg[2];
                    if (debugLevel >= 3) {
                        console.log('analogWrite current mode === ' + board.io.pins[msg[2]].mode);
                        console.log('pin = ' + pin);
                        console.log(msg);
                    }
                    if (board.io.pins[msg[2]].mode !== five.Pin.PWM) {
                        // send alert string
                        connection.send('invalidPinCommand/' + 3 + '/' + msg[1] + '/' + pin);
                    }
                    else {
                        board.analogWrite(msg[2], msg[3]);
                    }
                    break;
                // play a tone
                case 'playTone':
                    pin = msg[2];
                    var frequency = msg[3];
                    var duration = msg[4];

                    if (debugLevel >= 3) {
                        console.log('tone pin current mode === ' + board.io.pins[pin].mode);
                        console.log('pin = ' + pin);
                        console.log(msg);
                    }
                    if (board.io.pins[pin].mode !== five.Pin.OUTPUT) {
                        // send alert string
                        connection.send('invalidPinCommand/' + 4 + '/' + msg[1] + '/' + pin);
                        break;
                    }
                    else {
                        // retrieve piezo from array
                        for (i = 0; i < piezoArray.length; i++) {
                            if (piezoArray[i].pin === pin) {
                                piezoArray[i].frequency(frequency, duration);
                                return;
                            }
                        }
                        if (debugLevel >= 3) {
                            console.log('playTone - piezo not found for pin ' + pin);
                        }
                    }
                    break;
                case 'noTone':
                    pin = msg[2];
                    if (debugLevel >= 3) {
                        console.log('notone for pin' + pin);
                    }
                    for (i = 0; i < piezoArray.length; i++) {
                        if (piezoArray[i].pin === pin) {
                            piezoArray[i].noTone();
                            return;
                        }
                    }
                    if (debugLevel >= 3) {
                        console.log('noTone - piezo not found for pin ' + pin);
                    }
                    break;
                // move the servo to position in degrees
                // message indices: 1= board, 2 = pin, 3 = degrees
                case 'moveStandardServo':
                    pin = parseInt(msg[2]);
                    var degrees = parseInt(msg[3]);
                    var inverted = msg[4];
                    if (debugLevel >= 3) {
                        console.log('servo current mode === ' + board.io.pins[pin].mode);
                        console.log('pin = ' + pin);
                        console.log(msg);
                    }
                    if (board.io.pins[pin].mode !== five.Pin.SERVO) {
                        // send alert string
                        connection.send('invalidPinCommand/' + 5 + '/' + msg[1] + '/' + pin);
                        break;
                    }
                    else {
                        // retrieve servo from array
                        for (var i = 0; i < servoArray.length; i++) {
                            if (servoArray[i].pin === pin) {
                                if (servoArray[i].type !== "standard") {
                                    connection.send('invalidPinCommand/' + 6 + '/' + msg[1] + '/' + pin);
                                    return;
                                }
                                servoArray[i].isInverted = inverted !== 'False';
                                servoArray[i].to(degrees);
                                return;
                            }
                        }
                        if (debugLevel >= 3) {
                            console.log('moveStandardServo - servo not found for pin ' + pin);
                        }
                    }
                    break;
                // move a continuous servo in the given direction and speed
                case 'moveContinuousServo':

                    pin = parseInt(msg[2]);
                    var direction = msg[3];
                    inverted = msg[4];
                    var speed = parseFloat(msg[5]);
                    speed = parseFloat(speed.toFixed(2));
                    if (debugLevel >= 3) {
                        console.log('servo current mode === ' + board.io.pins[pin].mode);
                        console.log('pin = ' + pin);
                        console.log(msg);
                    }
                    if (board.io.pins[pin].mode !== five.Pin.SERVO) {
                        // send alert string
                        connection.send('invalidPinCommand/' + 5 + '/' + msg[1] + '/' + pin);
                        break;
                    }
                    else {

                        // retrieve servo from array
                        for (i = 0; i < servoArray.length; i++) {
                            if (servoArray[i].pin === pin) {
                                if (servoArray[i].type !== "continuous") {
                                    connection.send('invalidPinCommand/' + 7 + '/' + msg[1] + '/' + pin);
                                    return;
                                }

                                if (speed >= 0.0 && speed <= 1.0) {
                                    servoArray[i].isInverted = inverted !== 'False';
                                    if (direction === 'CW') {
                                        servoArray[i].cw(speed);
                                    }
                                    else {
                                        servoArray[i].ccw(speed);
                                    }
                                    return;
                                }
                                else {
                                    connection.send('invalidSetMode/Board ' + msg[1] + ' Pin ' + pin +
                                    ' Speed must be in the range of 0.0 to 1.0');
                                }
                                return;
                            }
                        }
                        if (debugLevel >= 3) {
                            console.log('moveContinuousServo - servo not found for pin ' + pin);
                        }
                    }
                    break;
                // stop servo motion - used for both standard and continuous
                case 'stopServo':
                    pin = parseInt(msg[2]);
                    if (debugLevel >= 3) {
                        console.log('stopServo servo current mode === ' + board.io.pins[pin].mode);
                        console.log('pin = ' + pin);
                        console.log(msg);
                    }
                    if (board.io.pins[pin].mode !== five.Pin.SERVO) {
                        // send alert string
                        connection.send('invalidPinCommand/' + 5 + '/' + msg[1] + '/' + pin);
                        break;
                    }
                    else {

                        // retrieve servo from array
                        for (i = 0; i < servoArray.length; i++) {
                            if (servoArray[i].pin === pin) {
                                servoArray[i].stop();
                                return;
                            }
                        }
                        if (debugLevel >= 3) {
                            console.log('stopServo - servo not found for pin ' + pin);
                        }
                    }
                    break;
                // move a stepper motor - this works for both 4 wire and driver.
                case 'moveStepper':
                    pin = parseInt(msg[2]);
                    var rpms = parseInt(msg[3]);
                    var dir = msg[4];
                    var acc = parseInt(msg[5]);
                    var dec = parseInt(msg[6]);
                    var stp = parseInt(msg[7]);


                    if (debugLevel >= 3) {
                        console.log('moveStepper mode ');
                        console.log('pin = ' + pin);
                        console.log(msg);
                    }

                    if (dir === 'CW') {
                        dir = five.Stepper.DIRECTION.CW;
                    }
                    else {
                        dir = five.Stepper.DIRECTION.CCW;
                    }
                    if (board.io.pins[pin].mode !== board.io.MODES.STEPPER) {
                        // send alert string
                        connection.send('invalidPinCommand/' + 8 + '/' + msg[1] + '/' + pin);
                        break;
                    }
                    else {

                        // retrieve servo from array
                        for (i = 0; i < stepperArray.length; i++) {
                            if (stepperArray[i].pins.step === pin) {
                                stepperArray[i].step({
                                    steps: stp,
                                    rpm: rpms,
                                    direction: dir,
                                    accel: acc,
                                    decel: dec
                                }, function () {
                                    console.log("Done stepping!");
                                });
                                return;
                            }

                        }

                        if (debugLevel >= 3) {
                            console.log('moveStepper - stepper not found for pin ' + pin);
                        }
                    }
                    break;

                // sets number of steps to 0 to stop stepper movement
                case 'stopStepper':
                    pin = parseInt(msg[2]);

                    if (debugLevel >= 3) {
                        console.log('stopSStepper servo current mode === ' + board.io.pins[pin].mode);
                        console.log('pin = ' + pin);
                        console.log(msg);
                    }
                    if (board.io.pins[pin].mode !== board.io.MODES.STEPPER) {
                        // send alert string
                        connection.send('invalidPinCommand/' + 8 + '/' + msg[1] + '/' + pin);
                        break;
                    }
                    else {

                        // retrieve servo from array
                        for (i = 0; i < stepperArray.length; i++) {
                            if (stepperArray[i].pins.step === pin) {
                                stepperArray[i].step({
                                    steps: 0,
                                    direction: five.Stepper.DIRECTION.CW
                                }, function () {
                                    console.log("stopped stepper pin " + pin);
                                });
                                return;
                            }
                        }
                        if (debugLevel >= 3) {
                            console.log('stopStepper - stepper not found for pin ' + pin);
                        }
                    }

                    break;


                case 'resetBoard':
                    // reset the board
                    if (debugLevel >= 2) {
                        console.log('Client is shutting down');
                    }
                    boardReset();
                    break;
                default:
                    console.log('Xi Server unknown message received: ' + msg[0]);
            }
        });
Example #6
0
 board.loop(1000, function () {
   board.digitalWrite(13,board.pins[13].value ? 0 : 1);
 });
function Encender()
{
	board.digitalWrite(pin,1);
}
function Apagar()
{
	board.digitalWrite(pin,0);
}
 setTimeout(function(){board.digitalWrite(LEDPIN, 0)}, pulseDuration);