Skip to content

Instantly share code, notes, and snippets.

@nicoledraw
Last active May 4, 2020 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicoledraw/1cb94ef43b88d3c9ed29f947330162aa to your computer and use it in GitHub Desktop.
Save nicoledraw/1cb94ef43b88d3c9ed29f947330162aa to your computer and use it in GitHub Desktop.
/*
Final Project: Mr. Boris The Blue Bear Bunny
Push button to move servo motor from 0 to 180 degrees
Simulate the ears going up and down when nose is pressed
Borrowed code from:
Robojax https://robojax.com/learn/arduino/?vid=robojax_Servo_PB1_move_return
Push button to activate vibrator motor
Simulate pressing one hand to shake the tail
*/
// VIBRATOR
#define vibPin 11
#define handButtonPin 8
// SERVO
#include <Servo.h>
Servo myservo;
#define servoPin 3
#define noseButtonPin 2
int angle = 0;
int angleStep = 10;
const int minAngle = 0;
const int maxAngle = 180;
const int type = 2;
int buttonPushed = 0;
void setup() {
Serial.begin(9600); // setup serial
// VIBRATOR
pinMode(vibPin, OUTPUT);
pinMode(handButtonPin, INPUT);
// SERVO
myservo.attach(servoPin); // attaches the servo on pin 3 to the servo object
pinMode(noseButtonPin, INPUT_PULLUP);
myservo.write(angle);//initial position
}
void loop() {
// VIBRATOR
int pressed = digitalRead(handButtonPin);
if (pressed == HIGH) {
digitalWrite(vibPin, HIGH);
} else {
digitalWrite(vibPin, LOW);
}
// Serial.println(pressed);
// SERVO
if (digitalRead(noseButtonPin) == LOW) {
buttonPushed = 1;
}
if (buttonPushed) {
// change the angle for next time through the loop:
angle = angle + angleStep;
// reverse the direction of the moving at the ends of the angle:
if (angle >= maxAngle) {
angleStep = -angleStep;
delay(1000);
if (type == 1) {
buttonPushed = 0;
}
}
if (angle <= minAngle) {
angleStep = -angleStep;
if (type == 2) {
buttonPushed = 0;
}
}
myservo.write(angle); // move the servo to desired angle
// Serial.print("Moved to: ");
// Serial.print(angle); // print the angle
// Serial.println(" degree");
delay(10); // waits for the servo to get there
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment