obniz developer team
Published

[for sleepyheads] An alarm clock RUNNING AWAY from you!

Haven't you overslept even with an alarm clock because you stopped it and slept again? We will give you a solution using distance sensor

BeginnerFull instructions provided534
[for sleepyheads] An alarm clock RUNNING AWAY from you!

Things used in this project

Hardware components

obniz
Cambrian Robotics obniz
×1
power supply
×1
Proximity Sensor
Proximity Sensor
×1
DC motor, 12V
×1
box, caster, and tires (for body)
×1

Story

Read more

Code

Untitled file

HTML
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Escaping Alarm Clock</title>
<script src="https://unpkg.com/obniz@3.0.0/obniz.js" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/wickedpicker@0.4.3/dist/wickedpicker.min.js"></script>
	
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link href="https://cdn.jsdelivr.net/npm/wickedpicker@0.4.3/dist/wickedpicker.min.css" rel="stylesheet">

</head>
<body>
	<div class="container">
	
		<div id="set-alarm" class="text-center">
			<h2 style="margin:40px"> Set Alarm </h2>
			
			<form id="set-time-form">
				<input type="text" id="set-time" name="timepicker" class="timepicker">
				<input type="submit" id="send-time" class="btn btn-outline-primary btn-sm" value="Set">
			</form>
		</div>
		
		<div id="display-time" class="text-center" style="display:none">
			<audio id="alarm-sound" preload="auto" src="./alarm_sound.mp3" type="audio/mp3" muted></audio>
			<h1 id="current-time" style="font-size:8em;margin-top:30px;margin-bottom:10px;"></h1>
			Alarm Time Set
			<div id="input-time" style="margin:12px"></div>
			<button id="return-set" class="btn btn-outline-warning btn-sm">Back to Setting</button>
			<button id="alarm-stop" class="btn btn-danger btn-lg" style="display:none">Stop</button>
		</div>
	
	</div>
<script>
$(function(){	
	const DIS_THOREHOLD = 150;
	
	let obniz = new Obniz('OBNIZ_ID_HERE');
	let isComparing = false;
	let isFirstMatch = true;
	let currentTime = null;
	let inputTime = null;
	let inverseInterval;
	let backgroundFlag = 0;
	let colors = ['white', 'black'];
	
	let options = {
		twentyFour: true,
		title: 'alarm time'
	}
	
	$('.timepicker').wickedpicker(options);
	
	obniz.onconnect = async () => {
		
		let disSensor = obniz.wired("GP2Y0A21YK0F", {vcc:7, gnd:8, signal:9});
	
		let pwmLeft = obniz.getFreePwm();
		let pwmRight = obniz.getFreePwm();
		
		//01:left DCmotor
		//1011:right DCmotor
		await obniz.io1.output(false);
		await obniz.io10.output(false);
		await pwmLeft.start({io:0});
		await pwmLeft.freq(1000);
		await pwmRight.start({io:11});
		await pwmRight.freq(1000);
		
		await console.log('init done');
		await obniz.wait(1000);
		
		$('#set-time-form').submit(async (e) => {
			await e.preventDefault();
			inputTime = $('.timepicker').wickedpicker('time');
			isComparing = true;
			$('#set-alarm').hide();
			$('#display-time').show();
			$('#alarm-stop').hide();
			$('#return-set').show();
			$('#input-time').text(inputTime);
			$('#alarm-sound')[0].play();
		});
		
		$('#return-set, #alarm-stop').click(async (e) => {
			await clearInterval(inverseInterval);
			$('#alarm-sound')[0].pause();
			$('body').css('background-color', colors[0]);
			$('body').css('color', colors[1]);
			$('#set-alarm').show();
			$('#display-time').hide();
			isFirstMatch = true;
			isComparing = false;
			inputTime = null;
		});
		
		$('#alarm-sound').on('ended', () => {
			$('#alarm-sound')[0].load();
			$('#alarm-sound')[0].play();
			$('#alarm-sound')[0].pause();
		});

		obniz.repeat(async () => {
			currentTime = await getCurrentTime();
			$('#current-time').text(currentTime);

			if(inputTime === null || currentTime === null){
				return;
			}
			if(!isComparing){
				return;
			}
			
			if(await isMatchingTime(inputTime, currentTime)){
				
				if(isFirstMatch){
					$('#return-set').hide();
					$('#alarm-stop').show();
					$('#alarm-sound').prop('muted', false);
					$('#alarm-sound')[0].play();
					isFirstMatch = false;
					
					inverseInterval = (setInterval(async () => {
						backgroundFlag = 1 - backgroundFlag;
						$('body').css('color', colors[1-backgroundFlag]);
						$('body').css('background-color', colors[backgroundFlag]);
					},500));
				}
				
				await escape();
				
			}else{
				$('body').css('background-color', colors[0]);
				$('body').css('color', colors[1]);
				$('#alarm-sound')[0].pause();
				clearInterval(inverseInterval);
				isFirstMatch = true;
				$('#alarm-stop').hide();
				$('#return-set').show();
			}
		}, 500);
		
		async function getCurrentTime(){
			let rawCurrentTime = new Date();
			let H = ("00" + rawCurrentTime.getHours()).slice(-2);
			let m = ("00" + rawCurrentTime.getMinutes()).slice(-2);
			
			return H + " : " + m;
		}
	
		async function isMatchingTime(_setTime, _currentTime){
			let setTimeHour = parseInt(_setTime.slice(0,2),10);
			let setTimeMinute = parseInt(_setTime.slice(-2),10);
			let currentTimeHour = parseInt(_currentTime.slice(0,2),10);
			let currentTimeMinute = parseInt(_currentTime.slice(-2),10);
			
			if(currentTimeHour !== setTimeHour){
				return false;
			}else if(currentTimeMinute !== setTimeMinute){
				return false;
			}else{
				return true;
			}
		}
	
		async function escape(){
			distance = await disSensor.getWait();

			if(distance >= DIS_THOREHOLD){
				return;
			}
			
			for(let i=0; i<=6; i++){
				await pwmLeft.duty(i*10);
				await pwmRight.duty(i*10);
				await obniz.wait(100);
			}
			await obniz.wait(1000);
			for(let i=6; i>=0; i--){
				await pwmLeft.duty(i*10);
				await pwmRight.duty(i*10);
				await obniz.wait(100);
			}
			await obniz.wait(1000);

		}		
	}

});
	
	
</script>	
</body>
</html>

Credits

obniz developer team

obniz developer team

80 projects • 35 followers
Development board "obniz" is controlled via the internet.

Comments