In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to use Arduino+Nodejs to do a gesture recognition interactive system, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.
I have been in contact with Arduino for some time, so I want to make a gadget to check it out. Welcome to communicate with each other in inappropriate places.
This time I was ordered to make a system product of intelligent gesture recognition and interaction. The main hardware modules used are SeeedStudio, that is, silicon delivery technology company to develop an Arduino Uno development board figure 1, a PAJ sensor figure 2. In the aspect of software, it is mainly Web server set up by Arduino IDE and Nodejs. The expectation of the target product is to control the scrolling and switching of the Web interface through gestures.
Figure 1:
Figure 2:
After introducing the required material modules, let's start building blocks.
Because there are fewer modules, the expansion board is omitted.
Here is the Arduino IDE program:
The code is here:
/ / call two library functions # include # include "paj7620.h" # define GES_REACTION_TIME 800#define GES_QUIT_TIME 1000Universe / define a LED output pin for handshake testing const int ledPin = 13 / define an initial status character String ledStatus = "off"; / / to get information from the Nodejs client String inputString = "; boolean stringComplete = false / * * arduino board setup * * / void setup () {/ / set baud rate Serial.begin (115200); / / define LED pin pinMode (ledPin, OUTPUT); / / PAJ uint8_t error = 0; Serial.println ("\ nPAJ7620U2 TEST DEMO: Recognize 15 gestures."); error = paj7620Init (); / / initialize Paj7620 registers if (error) {Serial.print ("INIT ERROR,CODE:"); Serial.println (error) } else {Serial.println ("INIT OK");} Serial.println ("Please input your gestures:");} / * Default arduino loop function * it runs over and over again * * / void loop () {uint8_t data = 0, data1 = 0, error; error = paj7620ReadReg (0x43, 1, & data); / / Read Bank_0_Reg_0x43/0x44 for gesture result. If (! error) {switch (data) {case GES_RIGHT_FLAG: delay (GES_REACTION_TIME); paj7620ReadReg (0x43, 1, & data); if (data = = GES_LEFT_FLAG) {Serial.println ("); Serial.println (") } else if (data = = GES_FORWARD_FLAG) {Serial.println ("); Serial.println ("); delay (GES_QUIT_TIME);} else if (data = = GES_BACKWARD_FLAG) {Serial.println ("); Serial.println ("); delay (GES_QUIT_TIME) } else {Serial.println ("); Serial.println (");} break; case GES_LEFT_FLAG: delay (GES_REACTION_TIME); paj7620ReadReg (0x43, 1, & data); if (data = = GES_RIGHT_FLAG) {Serial.println ("") Serial.println ("");} else if (data = = GES_FORWARD_FLAG) {Serial.println ("); Serial.println ("); delay (GES_QUIT_TIME);} else if (data = = GES_BACKWARD_FLAG) {Serial.println ("); Serial.println (") Delay (GES_QUIT_TIME);} else {Serial.println ("); Serial.println (");} break; break; case GES_UP_FLAG: delay (GES_REACTION_TIME); paj7620ReadReg (0x43, 1, & data) If (data = = GES_DOWN_FLAG) {Serial.println ("); Serial.println (");} else if (data = = GES_FORWARD_FLAG) {Serial.println ("); Serial.println ("); delay (GES_QUIT_TIME) } else if (data = = GES_BACKWARD_FLAG) {Serial.println ("); Serial.println ("); delay (GES_QUIT_TIME);} else {Serial.println ("); Serial.println (");} break Case GES_DOWN_FLAG: delay (GES_REACTION_TIME); paj7620ReadReg (0x43, 1, & data); if (data = = GES_UP_FLAG) {Serial.println ("); Serial.println (");} else if (data = = GES_FORWARD_FLAG) {Serial.println ("") Serial.println (""); delay (GES_QUIT_TIME);} else if (data = = GES_BACKWARD_FLAG) {Serial.println ("); Serial.println ("); delay (GES_QUIT_TIME);} else {Serial.println ("") Serial.println ("");} break; case GES_FORWARD_FLAG: delay (GES_REACTION_TIME); paj7620ReadReg (0x43, 1, & data); if (data = = GES_BACKWARD_FLAG) {Serial.println ("); Serial.println ("); delay (GES_QUIT_TIME) } else {Serial.println ("); Serial.println ("); delay (GES_QUIT_TIME);} break; case GES_BACKWARD_FLAG: delay (GES_REACTION_TIME); paj7620ReadReg (0x43, 1, & data) If (data = = GES_FORWARD_FLAG) {Serial.println ("); Serial.println ("); delay (GES_QUIT_TIME);} else {Serial.println ("); Serial.println ("); delay (GES_QUIT_TIME);} break Case GES_CLOCKWISE_FLAG: Serial.println ("); Serial.println ("); break; case GES_COUNT_CLOCKWISE_FLAG: Serial.println ("); Serial.println ("); break; default: paj7620ReadReg (0x44, 1, & data1) If (data1 = = GES_WAVE_FLAG) {Serial.println ("); Serial.println (");} break;}} delay (100); updateLedStatus ();} void updateLedStatus () {/ / detect whether the LED status is fully received if (stringComplete) {if (inputString = = "on\ r") {ledStatus = "on" } if (inputString = = "off\ r") {ledStatus = "off";} / send the LED status to the server Serial.println (ledStatus); inputString = ""; stringComplete = false;} / / judge the behavior state by the current state digitalWrite (ledPin, ledStatus = = "on"? HIGH: LOW);} void serialEvent () {while (Serial.available ()) {/ / receive a new byte char inChar = (char) Serial.read (); inputString + = inChar; / / interrupt if if a newline character is received (inChar = ='\ r') {stringComplete = true;}
Then there is the construction of the Web server, which uses Nodejs.
Where server.js file:
Var app = require ('http') .createServer (handler), io = require (' socket.io') .createServer (app), fs = require ('fs'), url = require (' url'), SerialPort = require ('serialport'). SerialPort, / / initialize serialport using the COM5 serialport / / remember to change this string if your arduino is using a different serialport sp = new SerialPort (' COM5', {baudRate: 115200}), / / this var will contain the message string dispatched by arduino arduinoMessage ='' / * helper function to load any app file required by client.html * @ param {String} pathname: path of the file requested to the nodejs server * @ param {Object} res: http://nodejs.org/api/http.html#http_class_http_serverresponse * / readFile = function (pathname, res) {/ / an empty path returns client.html if (pathname = ='/') pathname = 'client.html' Fs.readFile ('htmlarduino/client/' + pathname, function (err, data) {if (err) {console.log (err); res.writeHead (500); return res.end (' Error loading client.html');} res.writeHead (data);}) }, / * This function is used as proxy to print the arduino messages into the nodejs console and on the page * @ param {Buffer} buffer: buffer data sent via serialport * @ param {Object} socket: it's the socket.io instance managing the connections with the client.html page * * / sendMessage = function (buffer, socket) {/ / concatenating the string buffers sent via usb port arduinoMessage + = buffer.toString () / / detecting the end of the string if (arduinoMessage.indexOf ('\ r') > = 0) {/ / log the message into the terminal / / console.log (arduinoMessage); / / send the message to the client socket.volatile.emit ('notification', arduinoMessage); / / reset the output string to an empty value arduinoMessage ='';}} / / creating a new websocketio.sockets.on ('connection', function (socket) {/ / listen all the serial port messages sent from arduino and passing them to the proxy function sendMessage sp.on (' data', function (data) {sendMessage (data, socket);}) / / listen all the websocket "lightStatus" messages coming from the client.html page socket.on ('lightStatus', function (lightStatus) {sp.write (lightStatus +'\ lightStatus, function () {/ / log the light status into the terminal console.log ('the light should be:' + lightStatus);}); / / just some debug listenerssp.on ('close', function (err) {console.log (' Port closures');}) Sp.on ('error', function (err) {console.error (' error', err);}); sp.on ('open', function () {console.log (' Port openedhands');}); / / L3T'S R0CKroomhammer racing / creating the server (localhost:8000) app.listen (8000); / / server handlerfunction handler (req, res) {readFile (url.parse (req.url). Pathname, res);}
It is also useful for the socket.io module of Nodejs to make foreground and background data calls. The serialport module communicates with Arduino serial port matching. You can install it through the npm command
The Web page is relatively simple:
Gesture recognition control Web center {font-size: 100px; font-family:arial; background:rgba (20, 20, 20, 20, 0.5); width:500px; margin:auto;} section {background:#333;margin:10px;width:100px;height:100px } Turn the light on the above content is how to use Arduino+Nodejs to do a gesture recognition interactive system, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.