A2.3.3

 This is a kitten fishing toy, which consists of a button and a servo motor. The idea came from the last blog, and was adapted. When the button is pressed the kitten's fishing rod will be retracted, and when pressed again the rod will be lowered.

<textarea>

#include <Servo.h>


const int BUTTON_PIN = 7; 

const int SERVO_PIN  = 9; 


Servo servo; 



int angle = 0;         

int lastButtonState;   

int currentButtonState; 


void setup() {

  Serial.begin(9600);                

  pinMode(BUTTON_PIN, INPUT_PULLUP); 

  servo.attach(SERVO_PIN);          


  servo.write(angle);

  currentButtonState = digitalRead(BUTTON_PIN);

}


void loop() {

  lastButtonState    = currentButtonState;     

  currentButtonState = digitalRead(BUTTON_PIN); 


  if(lastButtonState == HIGH && currentButtonState == LOW) {

    Serial.println("The button is pressed");


   

    if(angle == 0)

      angle = 90;

    else

    if(angle == 90)

      angle = 0;


    servo.write(angle);

  }

}</textarea>

https://youtu.be/JSrbC_jRw-0

https://youtu.be/xOsZSe-Xrzw




评论

此博客中的热门博文

A5OBDF310

OBDF310A3

OBDF310A4