Can't Run Two Infinite Loops At Once

I’m using a raspberry pi to scan for BLE beacons and perform an action (unlock/lock door). I’ve got the process of scanning the beacons and their information into my node JS file, but I can’t get it to work with blynk. For eaxmple, I have two virtual buttons set up. One to begin scanning/stop scanning of beacons and the other to manually control the motor. When I begin scanning by pressing virtual button v1, I have no output until I change the state of v0. And even then, it’s just blocks of the scan and not continuous. Am I missing something about processes in Node JS? Ultimately I want the app user to be able to choose: do I want beacons controlling the lock or do I want to manually press the button each time. So two states. The state of continuous scanning, which I will fine later on with an array or something. And the state of manual pressing of virtual pin v0.

v0.on('write', function(param) {
console.log('V0:', param);
if (param[0] === '0') { //unlocked
	unlockDoor()
} else if (param[0] === '1') { //locked
	lockDoor()
} else {
	blynk.notify("Door lock button was pressed with unknown parameter");
}
});

v1.on('write', function(param) {
console.log('V1:', param);
if (param[0] == '0') { //enable BLE return
	//Bleacon.startScanning(uuid);
	console.log('turning on scan..')
	Bleacon.on('discover', function(bleacon) {

		if (bleacon.uuid == uuid) {
			console.log("uuid matches");
				if (bleacon.proximity == 'immediate') {				
					//console.log('immediate proximity: ' + bleacon.rssi);
					console.log("unlock");
					unlockDoor();
				} else if (bleacon.proximity == 'near') {
					console.log('near proximity: ' + bleacon.rssi);
					console.log("lock");
					lockDoor();
					
				}
		}
	});
} else if (param[0] == '1') { //not scanning
		console.log('stop scanning');
		Bleacon.stopScanning();
} else {
	blynk.notify("Door lock button was pressed with unknown parameter");
}
//console.log('bleacon found: ' + JSON.stringify(bleacon));
});

blynk.on('connect', function() { 
//console.log("Blynk ready.");
 });

My Terminal Output:

login as: pi
pi@192.168.1.2's password:

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun Mar 19 16:50:07 2017

SSH is enabled and the default password for the 'pi' user has not been changed.
This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password.

pi@raspberrypi:~ $ cd /home/pi/DoorBeacon/node-bleacon/
pi@raspberrypi:~/DoorBeacon/node-bleacon $ sudo node doorlock.js
scanning...
OnOff mode
Connecting to: blynk-cloud.com 8441
locking door
SSL authorization...
Connected
Authorized
V0: [ '1' ]
V1: [ '0' ]
turning on scan..
V0: [ '0' ]
uuid matches
near proximity: -88
lock
V0: [ '1' ]
V0: [ '0' ]
uuid matches
near proximity: -93
lock
uuid matches
near proximity: -92
lock
uuid matches
near proximity: -96
lock
uuid matches
near proximity: -99
lock
uuid matches
near proximity: -84
lock
uuid matches
near proximity: -83
lock
uuid matches
near proximity: -85
lock
V0: [ '1' ]
V0: [ '0' ]
uuid matches
near proximity: -74
lock
uuid matches
near proximity: -89
lock
uuid matches
near proximity: -90
lock
uuid matches
near proximity: -89
lock
V0: [ '1' ]
V0: [ '0' ]
uuid matches
near proximity: -78
lock
uuid matches
near proximity: -88
lock
uuid matches
near proximity: -71
lock
uuid matches
near proximity: -89
lock
uuid matches
near proximity: -77
lock
uuid matches
near proximity: -72
lock
uuid matches
near proximity: -79
lock
uuid matches
near proximity: -75
lock
uuid matches
near proximity: -86
lock
V0: [ '1' ]
V0: [ '0' ]
uuid matches
near proximity: -78
lock
uuid matches
near proximity: -84
lock
uuid matches
near proximity: -74
lock
uuid matches
near proximity: -79
lock
uuid matches
near proximity: -85
lock
uuid matches
near proximity: -72
lock
uuid matches
near proximity: -70
lock
uuid matches
near proximity: -80
lock
uuid matches
near proximity: -79
lock
^C2017-03-19 16:57:27 sigHandler: Unhandled signal 2, terminating

Anyone have any clue what’s going on here? I’m new to node JS. It seems like there is some pause loop or pause going on in the blynk.connect() that won’t listen to my beacon events. When I make run the Beacon scanning code alone, it works. When I add the blynk code in, the output of the beacons shows only when I push v0 and not v1.

If there is any further information needed or an explanation, I’d be glad to offer it. I feel as though I’m not the only person experiencing difficulty adjusting to the realm of node JS. I’d really like to solve this problem.

You are most definitely not the only one :wink: However, you appear to already know more than I do :blush: … Is that the entire script you posted?? It seems a bit too… simple? No libraries, or whatever the equivalent is in Javascript?

As for running the same scanning code without Blynk, what do you mean, using something else besides Javascript?

That is just a snippet of the JS code. I can get the whole script but it isn’t much more than what’s above. As far as scanning goes, I’ve tried putting just the BLE scan code in another file (Python) and executing a child process from the Node JS to run it, but the output is still acting strange. That is, the scanning information is only outputted to the terminal if V0 is given a ‘0’. The above code I have both the BLE scanning and the Blynk connection happening in the same file. … I think I’m not understanding the way Node JS works or the way the Blynk is continously checking if a virtual pin was pressed. That loop must be interfering with my scanning loop. But I’m not certain and I’d really like to find a way around that if it is the case. Thanks for your response!