Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1.  
  2. const five = require('johnny-five');
  3. const fs = require('fs');
  4. const math = require('math');
  5. //const opencv = require('opencv');
  6.  
  7.  
  8. let board = new five.Board(),
  9. led,
  10. toggleState = false;
  11.  
  12. let ledToBeControlled = 0;
  13.  
  14. let fsr, metalTouch;
  15.  
  16. let currentForceDiff = 0;
  17. let currentMetalDiff = 0;
  18.  
  19.  
  20.  
  21. board.on("ready", function() {
  22.  
  23. console.log('Board Ready');
  24.  
  25. this.pinMode(12, this.MODES.OUTPUT);
  26. this.pinMode(13, this.MODES.OUTPUT);
  27. this.digitalWrite(12 + ledToBeControlled,0);
  28.  
  29. const self = this;
  30.  
  31. this.loop(500, function () {
  32. self.digitalWrite(12 + ledToBeControlled, toggleState? 1 : 0);
  33. });
  34.  
  35.  
  36. // Create a new `fsr` hardware instance.
  37. fsr = new five.Sensor({
  38. pin: "A0",
  39. freq: 25
  40. });
  41.  
  42. fsr.scale([0, 255]).on("data", function() {
  43.  
  44. // set the led's brightness based on force
  45. // applied to force sensitive resistor
  46.  
  47. if (this.scaled) {
  48. console.log(this.scaled);
  49. console.log(this.value);
  50. //console.log('\n');
  51. }
  52.  
  53. currentForceDiff = this.scaled;
  54.  
  55. //led.brightness(this.scaled);
  56. });
  57.  
  58.  
  59. // create new metal touch
  60. metalTouch = new five.Sensor({
  61. pin:'A1',
  62. freq: 25,
  63. threshold: 1
  64. });
  65.  
  66. metalTouch.on('data', function () {
  67. console.log(Math.abs(metalTouch.fscaleTo(0,180) - 179));
  68. //fs.appendFile('metal-readings1.txt', Math.abs(metalTouch.scaleTo(0,180) - 179) + '\n' ,(err) => {
  69. // if (err) throw err;
  70. //});
  71.  
  72. currentMetalDiff = Math.abs(metalTouch.fscaleTo(0,180) - 179);
  73. });
  74. });
  75.  
  76. function turnLEDOn(whichLED) {
  77. ledToBeControlled = (parseInt(whichLED) === 0? 0 : 1);
  78. toggleState = true;
  79. }
  80.  
  81. function turnLEDOff(whichLED) {
  82. ledToBeControlled = (parseInt(whichLED) === 0? 0 : 1);
  83. toggleState = false;
  84. }
  85.  
  86. function getForceReading() {
  87. return currentForceDiff;
  88. }
  89.  
  90. function getMetalReadings() {
  91. return currentMetalDiff;
  92. }
  93.  
  94.  
  95. turnLEDOff(0);
  96. turnLEDOff(1);
  97.  
  98. //turnLEDOn(0);
  99.  
  100.  
  101.  
  102. exports.Board = board;
  103. exports.turnLEDOn = turnLEDOn;
  104. exports.turnLEDOff = turnLEDOff;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement