Lets get started.
You need to have NodeJs installed in your machine,if you don't have done yet check this for installation.
Now open up your terminal, follow the steps below,
1.$ cd ~/Desktop
2.$ mkdir arduino_nodejs
3.$ cd arduino_nodejs
4.$ npm init #to make nodejs project, continue pressing enter keep all settings default
5.$ npm install arduino-firmata --save
6.$ sudo rm -r node_modules/serialport #you need to delete as it uses latest serial port but arduino-firmata.js is not updated accordingly
7.$ npm install serialport@3.x #it is the correct version for this library
8.$ node
> var ArduinoFirmata = require('arduino-firmata'); //importing library
> var arduino = new ArduinoFirmata(); //making arduino object
> arduino.connect('/dev/cu.usbmodem1421'); //argumant passed here may be different for your machine,check tools tab of your arduino IDE
> arduino.digitalWrite(10, true); // writing high to pin 10 of arduino
> arduino.digitalWrite(13, false); // writing low to pin 10 of arduino
> arduino.pinMode(7, ArduinoFirmata.INPUT); //making pin 7 of arduino input
> console.log( arduino.digitalRead(7) ); //reading pin 7 and displaying it to console
> var analog_value = arduino.analogRead(0); //reading from channel 0 of analog port of arduino
> console.log(analog_value); //displaying read analog value(0~1023)
Enjoy coding arduino in NodeJs.Keep following the site.Another article named Arduino and Bash is coming soon.
No comments:
Post a Comment