2017年5月4日木曜日
驚いて口を開く
const int deg0msec = 600; // msec.
const int deg180msec = 2350; // msec.
int microSec = deg0msec;
int cnt = 0;
void setup() {
pinMode( 4, OUTPUT );
pinMode(2,INPUT) ;
Serial.begin( 9600 );
}
void loop() {
if ( Serial.available() > 0 ) {
delay(10);
int deg = serialReadAsInt();
if ( deg >= 0 && deg <= 180 )
microSec = deg0msec + deg / 180.0 * ( deg180msec - deg0msec );
}
if (digitalRead(2) == LOW) {
microSec = deg0msec + 45 / 180.0 * ( deg180msec - deg0msec );
Serial.print("BIN");
}
if (digitalRead(2) == HIGH) {
microSec = deg0msec + 0 / 180.0 * ( deg180msec - deg0msec );
}
// create PWM
if ( microSec >= deg0msec ) {
digitalWrite( 4, HIGH );
delayMicroseconds( microSec ); // ON
//digitalWrite( 4, HIGH );
digitalWrite( 4, LOW );
delayMicroseconds( 10000 ); // OFF
delayMicroseconds( 10000 - microSec ); // OFF
}
}
int serialReadAsInt() {
char c[ 9 ] = "0";
for ( int i = 0; i < 8; i++ ) {
c[ i ] = Serial.read();
if ( c[ i ] == '\0' )
break;
}
return atoi( c );
}
登録:
コメントの投稿 (Atom)
0 件のコメント:
コメントを投稿