아두이노프로세싱 프로그래밍

뉴디자인 디지털 시계와 아날로그 시계가 포함된 HTML/JavaScript 계산기

coding art 2017. 4. 29. 16:09
728x90



디지털 시계와 아날로그 시계를 포함한 좌우로 넓게 버튼이 배치된 계산기를 새로이 디자인해보자.

물론 각각의 요소를 코딩하는데 시간이 다소 소모되었지만 각각의 요소가 이미 완성되어 있었으므로 전체 구성 원리만 파악하면 대단히 간단하게 작성되었다는 점을 쉽게 알 수 있을 듯하다.

 

아두이노 초보자 입장에서는 일독 후 코드를 복사하여 Notepad++에서 실행하여 결과를 보고 아래에 설명된 내용이나 파라메터들을 변경해보면 금방 그 내용을 이해할 수 있을 것이다.

 

우선 먼저 .box{⚫⚫⚫}에서 폭 width와 높이 height 값을 조절한다.

그에 맞춰 .display{⚫⚫⚫}.display input{⚫⚫⚫}의 길이도 늘려 보자. 디스플레이의 길이가 충분히 늘어나면 계산기가 다룰 수 있는 숫자도 적어도 20자리 정도로 자동으로 늘어난다.

아울러 Pi 키도 3.14에서 업그레이드하여 3.14159로 정밀함을 부여한다.

.keys{⚫⚫⚫}에서 버튼 키 보드의 거리를 조절한다.

 

버튼 도입을 위해 <input> 태그가 시작 되는 부근에서 8개의 버튼들을 대상으로 <table>태그를 적용하자. 버튼 설정은 각자 <input> 태그 문장 순서를 바꾸어 주면 된다. <table>태그는 <tr>태그 즉 테이블 로우 와 <td> 태그 즉 테이블 셀 사용을 수반한다.

여기까지 버튼들의 테이블화 작업을 마친다.

 

아날로그 시계는 버튼들과 크기가 완전히 다르므로 별도의 테이블을 시작한다. 그 위치는 버튼 키 끝나는 지점부터이다. 이 테이블은 아날로그 시계 하나와 두 개의 긴 버튼 즉 연월일 및 시간 정보를 보여 주는 버튼형 디스플레이를 하나의 테이블 로우 즉 <tr> 로 삼고 각각 세 개의 <td> 요소로 설정한다. 그렇게 되면 두 개의 </div>가 연속적으로 나타나는 지점 바로 앞에서 </table>로 끝맺는다.

HTML 언어 설정 후 저장과 함께 실행한다. 위는 쿠글 크롬의 실행 결과 이미지이다.

이 코드는 HTML CSS 명령 변경에 따른 효과와 HTML 구성 요소들의 배치 변경에 따른 계산기의 시각적 디자인 학습에 큰 도움을 줄 수 있다.

 

calculatorclockdigitaltable_02.html

 

<!DOCTYPE HTML>

<html>

<body>

<center><h1>Arduino HTML Magic Coding</h1></center>

</body>

<head>

<style>

body{

background-color:tan;}

 

.box{background-color:#3d4543;height:330px;width:520px; border-style:solid; border-color:lightblue;

border-radius:10px;position:relative;top:1px;left:1%;}

 

.display{background-color:#222;width:440px; position:relative;

left:2px;top:20px;height:40px;}

 

.display input{width:415px;position:relative;left:2px;top:2px;height:30px;

color:black;background-color:#bccd95;font-size:21px;text-align:right;}

 

.keys{position:relative;top:50px;}

 

.button{width:40px;height:30px;border-radius:8px; border-style:solid; border-color:green;

margin-left:12px;cursor:pointer;border-top:2px solid transparent;}

 

.button.gray{color:white;background-color:#6f6f6f;

border-bottom:black 2px solid;border-top:2px #6f6f6f solid;}

 

.button.pink{color:black;background-color:#ff4561;

border-bottom:black 2px solid;}

 

.button.black{color:white;background-color:#303030;

border-bottom:black 2px solid;border-top:2px #303030 solid;}

 

.button.orange{color:black;background-color:FF9933;

border-bottom:black 2px solid;border-top:2px FF9933 solid;}

 

.gray:active{border-top:black 2px solid;

border-bottom:2px #6f6f6f solid;}

 

.pink:active{border-top:black 2px solid;

border-bottom:#ff4561 2px solid;}

 

.black:active{border-top:black 2px solid;

border-bottom:#303030 2px solid;}

 

.orange:active{border-top:black 2px solid;

border-bottom:FF9933 2px solid;}

 

.buttondigiclock{width:125px;height:40px;border:none;border-radius:8px;margin-left:15px;

cursor:pointer;border-top:2px solid transparent;}

 

.buttondigiclock.black{color:white;background-color:;

border-bottom:lightblue 2px solid;border-top:2px 303030 solid;}

 

 

 

p{line-height:10px;}

 

</style>

</head>

 

<body>

<center>

<div class="box">

<div class="display">

<input type="text" readonly size="18" id="d">

</div>

 

<div class="keys">

<table>

<tbody>

<tr>

<td><input type="button" class="button gray" value="Pi" onclick='v("3.14159")'></td>

<td><input type="button" class="button gray" value="(" onclick='v("(")'></td>

<td><input type="button" class="button gray" value=")" onclick='v(")")'></td>

<td><input type="button" class="button black" value="7" onclick='v("7")'></td>

<td><input type="button" class="button black" value="8" onclick='v("8")'></td>

<td><input type="button" class="button black" value="9" onclick='v("9")'></td>

<td><input type="button" class="button pink" value="/" onclick='v("/")'></td>

<td><input type="button" class="button pink" value="*" onclick='v("*")'></td>

</tr>

</tbody>

<tr>

<td><input type="button" class="button black" value="4" onclick='v("4")'></td>

<td><input type="button" class="button black" value="5" onclick='v("5")'></td>

<td><input type="button" class="button black" value="6" onclick='v("6")'></td>

<td><input type="button" class="button black"value="1" onclick='v("1")'></td>

<td><input type="button" class="button black" value="2" onclick='v("2")'></td>

<td><input type="button" class="button black" value="3" onclick='v("3")'></td>

<td><input type="button" class="button pink" value="-" onclick='v("-")'></td>

<td><input type="button" class="button pink" value="+" onclick='v("+")'></td>

</tr>

<tr>

<td><input type="button" class="button black" value="0" onclick='v("0")'></td>

<td><input type="button" class="button black" value="." onclick='v(".")'></td>

<td><input type="button" class="button black" value="sin" onclick='v("Math.sin")'></td>

<td><input type="button" class="button black" value="cos" onclick='v("Math.cos")'></td>

<td><input type="button" class="button black" value="tan" onclick='v("Math.tan")'></td>

<td><input type="button" class="button black" value="log" onclick='v("Math.log")'></td>

<td><input type="button" class="button black" value="C" onclick='c("")'></td>

<td><input type="button" class="button orange" value="=" onclick='e()'></td>

</tr>

</tbody>

</table>

 

<table>

<tbody>

<tr>

<td>

<body onload="updateTime()">

<!-- viewBox is coordinate system, width and height are on-screen size -->

<svg id="clock" viewBox="0 0 100 100" width="120" height="120">

 

<circle id="face" cx="50" cy="50" r="45"/> <!-- the clock face -->

<circle id="smallface" cx="50" cy="32" r="10"/> <!-- the small clock face -->

<g id="ticks">

<!-- 12 hour tick marks -->

<line x1='50' y1='5.000' x2='50.00' y2='10.00'/>

<line x1='72.50' y1='11.03' x2='70.00' y2='15.36'/>

<line x1='88.97' y1='27.50' x2='84.64' y2='30.00'/>

<line x1='95.00' y1='50.00' x2='90.00' y2='50.00'/>

<line x1='88.97' y1='72.50' x2='84.64' y2='70.00'/>

<line x1='72.50' y1='88.97' x2='70.00' y2='84.64'/>

<line x1='50.00' y1='95.00' x2='50.00' y2='90.00'/>

<line x1='27.50' y1='88.97' x2='30.00' y2='84.64'/>

<line x1='11.03' y1='72.50' x2='15.36' y2='70.00'/>

<line x1='5.000' y1='50.00' x2='10.00' y2='50.00'/>

<line x1='11.03' y1='27.50' x2='15.36' y2='30.00'/>

<line x1='27.50' y1='11.03' x2='30.00' y2='15.36'/>

</g>

<g id="smallticks">

<!-- 12 hour tick marks -->

<line x1='50' y1='24' x2='50' y2='22'/>

<line x1='58' y1='32' x2='60' y2='32'/>

<line x1='50' y1='40' x2='50' y2='42'/>

<line x1='42' y1='32' x2='40' y2='32'/>

</g>

<g id="numbers">

<!-- Number the cardinal directions-->

<text x="50" y="18">12</text><text x="85" y="53">3</text>

<text x="50" y="88">6</text><text x="15" y="53">9</text>

</g>

<!-- Draw hands pointing straight up. We rotate them in the code. -->

<g id="hands"> <!-- Add shadows to the hands -->

<line id="hourhand" x1="50" y1="50" x2="50" y2="24"/>

<line id="minutehand" x1="50" y1="50" x2="50" y2="20"/>

<line id="secondhand" x1="50" y1="32" x2="50" y2="24"/>

</g>

</svg>

</body>

</td>

<td>

<form name="theClock">

<input type=text class="button buttondigiclock black" name="theDate" style="font-size:20px;text-align: center;">

</td>

<td>

<input type=text class="button buttondigiclock black" name="theTime" style="font-size:20px;text-align: center;">

</form>

</td>

</tr>

</tbody>

</table>

</div>

</div>

 

<script type='text/javascript'>

function c(val){

document.getElementById("d").value=val;}

function v(val){

document.getElementById("d").value+=val;}

function e() {

try {

c(eval(document.getElementById("d").value)) }

catch(e) {

c('Error') } }

</script>

</center>

</body>

 

<head>

<script>

function updateTime() { // Update the SVG clock

var now = new Date();

var sec = now.getSeconds();

var min = now.getMinutes();

var hour = (now.getHours() % 12) + min/60;

var secangle = sec*6;

var minangle = min*6;

var hourangle = hour*30;

var actualMonth = now.getMonth() + 1;

//Prepare time output!

document.theClock.theTime.value = ""

+ now.getHours() + ":"

+ now.getMinutes() + ":"

+ now.getSeconds();

document.theClock.theDate.value = ""

+ now.getFullYear() + ":"

+ actualMonth + ":"

+ now.getDate();

// Get SVG elements for the hands of the clock

var sechand = document.getElementById("secondhand");

var minhand = document.getElementById("minutehand");

var hourhand = document.getElementById("hourhand");

// Set an SVG attribute on them to move them around the clock face

sechand.setAttribute("transform", "rotate(" + secangle + ",50,32)");

minhand.setAttribute("transform", "rotate(" + minangle + ",50,50)");

hourhand.setAttribute("transform", "rotate(" + hourangle + ",50,50)");

setTimeout(updateTime, 1000);

}

 

</script>

<style>

/* These CSS styles all apply to the SVG elements defined below */

#clock {

/* styles for everything in the clock */

stroke: black;

/* black lines */

stroke-linecap: round;

/* with rounded ends */

fill: #eef;

/* on a light blue gray background */

}

#face { stroke-width: 3px;}

/* clock face outline */

#smallface { stroke:green; stroke-width: 1px;}

/* clock face outline */

#ticks { stroke-width: 2; }

/* lines that mark each hour */

#smallticks { stroke:green; stroke-width: 1; }

#hourhand {stroke-width: 5px;}

/* wide hour hand */

#minutehand {stroke: deepskyblue; stroke-width: 3px;} /* narrow minute hand */

#secondhand {stroke:red; stroke-width: 1px;}

#numbers {

/* how to draw the numbers */

font-family: sans-serif; font-size: 7pt; font-weight: bold;

text-anchor: middle; stroke: none; fill: blue;

}

</style>

</head>

 

</html>