Engineer Jagat

IT & Engineering Stuff

Code Motion Sensor with C++ and JavaScript: Johnny-Five

October 3, 2020
Code Motion Sensor with C++ and JavaScript: Johnny-Five

C and C++ are the foundation of any programming language and mostly used by Embedded System.

But JavaScript has a huge area of development, it is everywhere nowadays from Web Development to the Internet of things(IoT).

In this post, I will show how C and C++ control the motion sensor and How JavaScript controls the motion sensor.

Step 1: Setup

Step 2: Code

int sensorPin = 8;
int led = 9;
int pir;

void setup()
{
   pinMode(led, OUTPUT);
   Serial.begin(9600);
}

void loop()
{
  pir = digitalRead(sensorPin);
  if(pir == 1)
  {
    digitalWrite(led, HIGH);
    Serial.println("Motion Detected");
  }
  else
  {
    digitalWrite(led, LOW);
    Serial.println("No Motion");
  }
  
}

Step 3: Wiring

Code for JavaScript: Johnny-Five

Step 1: Setup

You need to install some components before start Coding so follows these posts or google it for setup Johnny-Five.

Step 2: Code

var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function(){
	var motion = new five.Motion(8);
	var led = new five.Led(9);


	motion.on("calibrated", function(){
		console.log("calibrated");
	});

	motion.on("motionstart", function(){
		console.log("motionstart");
  		led.on();

	});

	motion.on("motionend", function(){
		console.log("motionend");
		led.off();
	})

});

Step 3: Wiring

Output

Share with Friends
Copyright © 2020-2021 Engineer Jagat All rights reserved. Developed by Ravindra Rana