xbox.on('right:move', function(position){ if(ready) { if(position.y < 0) { position.y = ~position.y; board.analogWrite('D2', 0); board.analogWrite('D3', limit(position.y)); } else { board.analogWrite('D3', 0); board.analogWrite('D2', limit(position.y)); } } });
socket.on("sample", (c) => { //First, check if board is currently ready if(boardStatus().ready) { //Sets pinModes and activate them board.pinMode(redPin, five.Pin.PWM); board.pinMode(greenPin, five.Pin.PWM); board.pinMode(bluePin, five.Pin.PWM); board.analogWrite(redPin, c.red); board.analogWrite(greenPin, c.green); board.analogWrite(bluePin, c.blue); var data = c.red + "," + c.green + "," + c.blue + ","; var buffer = Buffer.from(data); var byteArray = Array.from(buffer); board.io.serialWrite(portId, byteArray); } });
new five.Sensor("I0").scale(0, 255).on("change", function() { var val = this.value; var pinValue; if (val < 128) { forward(); pinValue = (255 - (val * 2)); } else { backward(); pinValue = (val - 128) * 2; } console.log(this.value, pinValue); board.analogWrite(enablePin, pinValue); });
/** * Morse * * @description :: get an action (0, 1 or 2) and control the LED * */ function morse(action) { switch(action) { case 0: process.stdout.write(" "); break; case 1: process.stdout.write("."); led.on(); board.analogWrite(8, BUZZ_FREQ); setTimeout(function(){ led.off(); board.analogWrite(8, 0); }, SPEED); break; case 2: process.stdout.write("-"); led.on(); board.analogWrite(8, BUZZ_FREQ); setTimeout(function(){ led.off(); board.analogWrite(8, 0);}, SPEED*5); break; } }
var handleFrame = function(frame) { if (frame.hands && frame.hands.length > 0) { var hand = frame.hands[0], palmX = hand.palmPosition[0], min = -250, max = 250; if (palmX > min && palmX < max) { var pinValue; if (palmX < 0) { backward(); pinValue = -palmX; } else { forward(); pinValue = palmX; } console.log(pinValue); board.analogWrite(enablePin, pinValue); } } };
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]); } });
setTimeout(function(){ led.off(); board.analogWrite(8, 0);}, SPEED*5);