Skip to content

Instantly share code, notes, and snippets.

@RyoKosaka
Created October 24, 2024 05:39
Show Gist options
  • Save RyoKosaka/a7e500fce7040d92248d649712944cc3 to your computer and use it in GitHub Desktop.
Save RyoKosaka/a7e500fce7040d92248d649712944cc3 to your computer and use it in GitHub Desktop.
プロダクトデザイン応用実習サンプルコード - ボタンを押した時にサーボを指定した角度まで動かすコード
//プロダクトデザイン応用実習サンプルコード - ボタンを押した時にサーボを指定した角度まで動かすコード
#include <Servo.h> //ライブラリを使いますという宣言
Servo myservo; // サーボにmyservoと名前をつける
const int buttonPin = 2;
const int servoPin = 9;
void setup() { //電源をつけた時に一回だけ行われる
myservo.attach(servoPin); //サーボを(servoPin)9番ピンに接続しますという宣言
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin); //2番ピンを読み取る
if (buttonState == HIGH) {
myservo.write(150); //myservoを150度まで動かすという指定
delay(300);
} else {
myservo.write(0); //myservoを0度まで動かすという指定
delay(300);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment