Show toolbar

2014年4月29日 星期二

Touch events example

標題:觸控事件範例
JavaScript (index.html):
Start: <div id="t1"></div>
Move: <div id="t2"></div>
End: <div id="t3"></div>
<script type="text/javascript">
document.ontouchstart = function(e) {
    var px = e.changedTouches[0].clientX;
    var py = e.changedTouches[0].clientY;
    e.preventDefault();
    document.getElementById('t1').innerHTML = '(' + px + ', ' + py + ')';
};
document.ontouchmove = function(e) {
    var px = e.changedTouches[0].clientX;
    var py = e.changedTouches[0].clientY;
    e.preventDefault();
    document.getElementById('t2').innerHTML = '(' + px + ', ' + py + ')';
};
document.ontouchend = function(e) {
    var px = e.changedTouches[0].clientX;
    var py = e.changedTouches[0].clientY;
    e.preventDefault();
    document.getElementById('t3').innerHTML = '(' + px + ', ' + py + ')';
};
</script>

說明:
觸控事件取得座標範例。

2014年4月13日 星期日

Servo Motor Control with an Arduino

標題:使用Arduino控制伺服馬達


Arduino (servo_motor):
#include <Servo.h> 
Servo myservo;
const int umin = 500, umax = 2400, umid = (umin + umax)/2;
const int buttonPin = 2;
int buttonState = 0, buttonDownState = 0, motorState = 0;

void setup() {
    pinMode(buttonPin, INPUT);
    myservo.attach(9, umin, umax);
    myservo.writeMicroseconds(umid);
    delay(3000);
}

void loop() {
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
        buttonDownState = 1;
    } else {
        if(buttonDownState == 1) {
            if(motorState == 0) {
                myservo.writeMicroseconds(umin);
                motorState = 1;
            } else if(motorState == 1) {
                myservo.writeMicroseconds(umid);
                motorState = 2;
            } else if(motorState == 2) {
                myservo.writeMicroseconds(umax);
                motorState = 3;
            } else {
                myservo.writeMicroseconds(umid);
                motorState = 0;
            }
            buttonDownState = 0;
        }
    }
}

Arduino Diecimila
電阻 - 10kΩ
伺服機 - GWS S03T

說明:
按鈕依序輸出脈衝寬度最小、中間、最大,以控制依序移動伺服機位置。