Example #1
0
board.on("ready", function() {
    //Create new Ping and show distance on change
    var ping = new five.Ping(config.pins.paperLevelSensor);

    ping.on("data", function( err, value ) {
        console.log('Object is ' + this.cm + ' cm away');
    });
});
Example #2
0
function connectSensor(pin, sensorDistances, sensorId){
  var ping = new five.Ping(pin);
  ping.on('data', function(err) {
    //console.log("id: ", pin, "Distance: " + this.cm + " cm");
    if (!err && this.cm < MAX_DISTANCE){
      sensorDistances[sensorId] = (this.cm / 100).toFixed(2);
    }
  });
}
Example #3
0
board.on("ready", function() {

    //Create new Ping and show distance on change
    var ping = new five.Ping(7);

    ping.on("change", function( err, value ) {
        console.log('Object is ' + this.cm + ' cm away');
    });
});  
Example #4
0
board.on("ready", function() {

    var ping = new five.Ping(7);

    ping.on("change", function( err, value ) {
        if (this.cm < 5) {
            console.log("STOP!!!!");
        } else {
            console.log(".");
        }
    });
});  
board.on("ready", function() {

    console.log("yay! go nodedrone!");

    ping = new five.Ping(6);

    ping.on("data", function( err, value ) {
	process.stdout.write(".");
    });

    ping.on("change", getDistAndFly);
});
board.on("ready", function() {
 var visaBot = new Visabot(new Wheel(5,9), new Wheel(4, 10));

var minDistance = 30;
var currentDistance = -1;
var previousDistance = -1;
var adjuster = false;

var theServo = new five.Servo(11);

  board.repl.inject({
    servo: theServo
  });

theServo.center();
//theServo.stop();

//theServo.sweep();


 var ping = new five.Ping(7);

  ping.on("change", function() {
    console.log("Object is " + this.cm + " cm away");
    if (this.cm <= previousDistance || this.cm <= minDistance){
	previousDistance =  this.cm;

    	board.wait(500, function() {
		console.log("Looking for space");
		visaBot.turnLeft();
	    	board.wait(20, function() {
			console.log("stopping");
			visaBot.stop();
		});

	});
    }
    else {
    	board.wait(1000, function() {
		console.log("moving forward");
		visaBot.forward();
	    	board.wait(20, function() {
			console.log("stopping");
			visaBot.stop();
		});
	});
    }
  });
});
Example #7
0
board.on('ready', function() {
  console.log('ready');

  // Create a standard `led` hardware instance
  var yellow = new five.Led({ pin: 3 });
  var red    = new five.Led({ pin: 5 });
  var ping   = new five.Ping(13);

  var Output = function(ping, led, far, near) {
    this.ping = ping;
    this.led = led;
    this.near = near;
    this.far = far;
    this.distance = ping.cm;
  };

  Output.prototype.update = function() {
    this.distance = this.ping.cm;
    if (this.distance > this.far) {
      this.led.off();
    } else if (this.distance < this.far && this.distance > this.near) {

      var fraction = this.distance - this.near;
      var brightness = fraction / (this.far - this.near);
      this.led.brightness(255 * (1-brightness));
      this.led.on();
    } else {
      this.led.brightness(255);
      this.led.on();
    }
  };

  var outputs = [ new Output(ping, yellow, 50, 25),
                  new Output(ping, red,    25, 0)];

  ping.on('change', function(err, value) {
    _.each(outputs, function (output) {
      output.update();
    })
  });
  
});
Example #8
0
board.on("ready", function() {

  console.log('here');

  // Create a new `ping` hardware instance.
  ping = new five.Ping(9);

  console.log('after');
  // Properties

  // ping.microseconds
  //
  // Roundtrip distance in microseconds
  //

  // ping.inches
  //
  // Calculated distance to object in inches
  //

  // ping.cm
  //
  // Calculated distance to object in centimeters
  //


  // Ping Event API

  // "data" get the current reading from the ping
  ping.on("data", function(err, value) {
    console.log("data", value);
  });

  ping.on("change", function(err, value) {

    console.log(typeof this.inches);
    console.log("Object is " + this.inches + "inches away");
  });
});
Example #9
0
board.on("ready", function() {

  ping = new five.Ping(7);

  motor = new five.Pin(3);
  motor.high()

  on = false

  average = []

  ping.on("change", function( err, value ) {
    average.push(this.cm)
    if (average.length > 10){
      average.shift()
    }

    sum = average.reduce(function(a, b) { return a + b });
    distance = sum / average.length;

    console.log( "Object is " + distance + "cm away" );
    if (distance > 0 && distance < 30){
      if(!on){
        console.log('close!')
        motor.high()
        on = true
      }
    } else {
      if(on){
        console.log('far!')
        motor.low()
        on = false
      }
    }
  });
});
Example #10
0
board.on("ready", function() {
    var ping = new five.Ping(7);
    ping.on("change", function() {
        console.log('Detected object at ' + this.cm + ' cm away');
    });
});
Example #11
0
board.on("ready", function() {

	//var pin = new five.Pin(5);
	//pin.write(0);

	var speed = 0;
	var motorState = true;

	//var motorR = new five.Motor(5);
	//var motorL = new five.Motor(9);
	var motorL = new five.Motor({
		pins: {
			pwm: 3,
			dir: 9
		}
	});

	//var motorR = new five.Motor([5, 6]);
	var motorR = new five.Motor({
		pins: {
			pwm: 6,
			dir: 5
		}
	});

	var ping = new five.Ping(7);
	ping.on("data", function(err, value) {
		//console.log("data", value);
	});

	ping.on("change", function(err, value) {

		//console.log(typeof this.cm);
		//console.log("Object is " + this.cm + "cm away");
	});

	//----------------------------

	var servo = new five.Servo({
		pin: 12,
		range: [45, 135]
	});

	board.repl.inject({
		motorR: motorR,
		motorL: motorL,
		servo: servo
	});

	servo.center();
	//	servo.sweep();

	var increment = 32;

	function controller(ch, key) {
		var isThrottle = false;
		//console.log("Current speed=" + speed);
		//console.log("keyname=" + key.name);

		if (key.name === "up" || key.name === "down") {
			if (key.name === "up") {
				speed += increment;
				isThrottle = true;
			}

			if (key.name === "down") {
				speed -= increment;
				isThrottle = true;
			}

			if (isThrottle) {
				//console.log("Speed adjustment. speed=" + speed);
				if (speed == 0) {
					motorR.stop();
					motorL.stop();
				} else if (speed > 0) {
					motorR.forward(255 - speed);
					motorL.forward(255 - speed);
				} else {
					motorR.reverse(speed * -1);
					motorL.reverse(speed * -1);
				}
			}
		} else if (key.name === "left" || key.name === "right") {
			motorR.stop();
			motorL.stop();
			if (key.name === "left") {
				motorR.forward(200);
				motorL.reverse(55);
			}

			if (key.name === "right") {
				motorR.reverse(55);
				motorL.forward(200);
			}

		} else if (key.name === "space") {
			console.log("space pressed");
			speed = 0;
			motorR.reverse(1);
			motorL.reverse(1);
			servo.center();
		} else if (key.name === "pageup") {
			motorR.forward(1);
			motorL.forward(1);
		} else if (key.name === "pagedown") {
			motorR.reverse(255);
			motorL.reverse(255);
		} else if (key.name === "z") {
			servo.min();
		} else if (key.name === "x") {
			servo.max();
		}


	}

	keypress(process.stdin);

	process.stdin.on("keypress", controller);
	process.stdin.setRawMode(true);
	process.stdin.resume();

});
Example #12
0
board.on("ready", function() {

  toggleSwitch = new five.Switch(40);
  toggleSwitch.on("close", function() {
    goMental();
  });

  // "open" the switch is opened
  toggleSwitch.on("open", function() {
  });

  var ping = new five.Ping(3);

  output = new midi.output();
  output.openVirtualPort("TechnoTree");

  input = new midi.input();
  input.on('message', function(deltaTime, message) {
    console.log(message);
    setLed(2,message[2]);

    if (mentalMode) {
      try {
        setLed(1,message[2]);
        setLed(0,message[2]);
        setLed(3,message[2]);
      } catch (e) {}
    }
   
  });

  input.openVirtualPort("TechnoTree");

  leds = [
    new LedStrip(5),
    new LedStrip(6),
    new LedStrip(7),
    new LedStrip(8)
  ];

  // // Inject the `servo` hardware into
  // // the Repl instance's context;
  // // allows direct command line access
  // fadeLedIn(0);
  // delay(1000,fadeLedIn,1);
  // delay(2000,fadeLedIn,2);
  // delay(3000,fadeLedIn,3);


  var standardPing = 8000;
  var standardPingMin = 300;
 
  ping.on("data", function(err, value) {
    if (value && standardPing === 0) {
      standardPing = value;
    }
    ledMax = 255 * (value - standardPingMin) / (standardPing - standardPingMin);

    if (ledMax > 255) ledMax = 255;
    if (ledMax < 0) ledMax = 0;
    if (value) {
      sendNote([144,12,ledMax]);
    }
  });

  var baubles = [
    new Bauble('A0', 47),
    new Bauble('A1', 48),
    // new Bauble('A2', 49),
    new Bauble('A3', 50),
    new Bauble('A4', 51),
    new Bauble('A5', 52),
    new Bauble('A6', 53),
    new Bauble('A7', 54),
    // new Bauble('A8', 55),
    // new Bauble('A9', 56),
    // new Bauble('A10', 57)
  ];

  board.repl.inject({
    leds:leds,
    fadeLedIn:fadeLedIn,
    ledMax: ledMax,
    fadeLedOut: fadeLedOut,
    baubles:baubles,
    output:output
  });
});
Example #13
0
    io.sockets.on('connection', function (socket) {
        // IR detection calibrated
        if (irReady) {
            // Emit IR ready
            socket.emit('irReady');
        } else {
            console.log('IR motion was not calibrated');
        }

        // On socket key 'sweep'
        socket.on('sweep', function (obj) {
            // Servo sweep state
            switch (obj.state) {
                case 'start':
                    // Start sweeping
                    servo.sweep();
                    // Set servo flag to moving
                    isMoving = true;
                break;
                case 'stop':
                    // Stop servo
                    servo.stop();
                    // Set servo flag to not moving
                    isMoving = false;
                break;
            }
        });

        // On socket key 'pan'
        socket.on('pan', function (obj) {
            // Position request is left and servo position is above servo minumum range
            if (obj.direction === 'left' && servo.position >= (servo.range[0] + deg)) {
                // Pan left
                servo.to(servo.position - deg);
            // Position request is right and servo position is below servo maximum range
            } else if (obj.direction === 'right' && servo.position <= (servo.range[1] - deg)) {
                // Pan right
                servo.to(servo.position + deg);
            }
        });

        // On socket key 'position'
        socket.on('position', function (obj) {
            // Position is a number
            if (typeof obj.position === 'number') {
                // Set servo position
                servo.to(obj.position);
            }
        });

        // On socket key 'ir'
        socket.on('ir', function (bool) {
            // Set IR detection mode
            ir = bool;
            // IR detection is off
            if (!ir) {
                // Stop servo
                stopServo();
            }
        });

        // Ping))) data
        ping.on('data', function (err, value) {
            // Emit radar data
            socket.emit('radar', {
                degrees: servo.position,
                distance: this.cm
            });
        });

        // Movement started
        motion.on('motionstart', function (err, ts) {
            // Emit motion detected
            socket.emit('motion', true);
            // Clear motion interval
            clearInterval(motionTimer);
            // IR detection is on and servo is not moving
            if (ir && !isMoving) {
                // Start sweeping
                servo.sweep();
                // Set servo flag to moving
                isMoving = true;
            }
        });

        // Movement ended
        motion.on('motionend', function (err, ts) {
            // Emit motion stopped
            socket.emit('motion', false);
            // IR detection is on and servo is moving
            if (ir && isMoving) {
                // Tigger motion interval
                motionInterval();
            }
        });

    });
Example #14
0
board.on('ready', function() {

    // Servo
    servo = new five.Servo({
        pin: 4,
        range: [0, 180],    // Default: 0-180
        type: "standard",   // Default: "standard". Use "continuous" for continuous rotation servos
        startAt: 0,         // if you would like the servo to immediately move to a degree
        center: false       // overrides startAt if true and moves the servo to the center of the range
    });

    // Ping
    ping = new five.Ping(2);

    // IR motion
    motion = new five.IR.Motion(7);

    // Inject the `servo and motion` hardware into
    // the Repl instance's context;
    // allows direct command line access
    board.repl.inject({
        servo: servo,
        motion: motion
    });

    // IR calibration
    motion.on('calibrated', function (err, ts) {
        console.log('IR Motion Calibrated');
        // Set IR flag to ready
        irReady = true;
    });

    // On socket connection
    io.sockets.on('connection', function (socket) {
        // IR detection calibrated
        if (irReady) {
            // Emit IR ready
            socket.emit('irReady');
        } else {
            console.log('IR motion was not calibrated');
        }

        // On socket key 'sweep'
        socket.on('sweep', function (obj) {
            // Servo sweep state
            switch (obj.state) {
                case 'start':
                    // Start sweeping
                    servo.sweep();
                    // Set servo flag to moving
                    isMoving = true;
                break;
                case 'stop':
                    // Stop servo
                    servo.stop();
                    // Set servo flag to not moving
                    isMoving = false;
                break;
            }
        });

        // On socket key 'pan'
        socket.on('pan', function (obj) {
            // Position request is left and servo position is above servo minumum range
            if (obj.direction === 'left' && servo.position >= (servo.range[0] + deg)) {
                // Pan left
                servo.to(servo.position - deg);
            // Position request is right and servo position is below servo maximum range
            } else if (obj.direction === 'right' && servo.position <= (servo.range[1] - deg)) {
                // Pan right
                servo.to(servo.position + deg);
            }
        });

        // On socket key 'position'
        socket.on('position', function (obj) {
            // Position is a number
            if (typeof obj.position === 'number') {
                // Set servo position
                servo.to(obj.position);
            }
        });

        // On socket key 'ir'
        socket.on('ir', function (bool) {
            // Set IR detection mode
            ir = bool;
            // IR detection is off
            if (!ir) {
                // Stop servo
                stopServo();
            }
        });

        // Ping))) data
        ping.on('data', function (err, value) {
            // Emit radar data
            socket.emit('radar', {
                degrees: servo.position,
                distance: this.cm
            });
        });

        // Movement started
        motion.on('motionstart', function (err, ts) {
            // Emit motion detected
            socket.emit('motion', true);
            // Clear motion interval
            clearInterval(motionTimer);
            // IR detection is on and servo is not moving
            if (ir && !isMoving) {
                // Start sweeping
                servo.sweep();
                // Set servo flag to moving
                isMoving = true;
            }
        });

        // Movement ended
        motion.on('motionend', function (err, ts) {
            // Emit motion stopped
            socket.emit('motion', false);
            // IR detection is on and servo is moving
            if (ir && isMoving) {
                // Tigger motion interval
                motionInterval();
            }
        });

    });

});
Example #15
0
board.on("ready", function() {
  var ping = new five.Ping(7);
  ping.on("data", function( err, value ) {
    console.log("Distance: " + this.cm + " cm");
  });
});
Example #16
0
var Eyes = function (servoPin, sonarPin) {

  var servo = new five.Servo({
    pin: servoPin,
    range: [0, 180],
    startAt: 0
  }),
  sonar = new five.Ping({
    pin : sonarPin,
    pulse : '200'
  });


  
    sonar.on('change', function(){
      //console.log('distance in cm : ' + sonar.cm)
      if (this.cm < 5){
        console.log('****************')
        //self.stop();
      }
    });
  


  this.context = {
    servo : servo,
    sonar : sonar
  }

  var intervalToken = null;

  this.center = function (argument) {
    servo.center();  
  }

  this.look = function (orientation, cb) {
    switch(orientation){
      case 'left' :
        //cb('left', sonar.cm);
        servo.max();
        break;
      case 'right' :
        servo.min();
        break;
      case 'forward' :
        servo.center();
        break;
    }

    if (cb){
      setTimeout(function() {
          cb(sonar.cm)
      }, 750);
    }

  }

  this.monitor = function (cb) {
    intervalToken = setInterval(function() {
      cb(sonar.cm);
    }, 500);
  }

  this.getDistance = function(){
    return sonar.cm;
  }


  this.stopMonitoring = function () {
    clearInterval(intervalToken);
  }
}