Stupid Pet Trick & Assignment 4

For my stupid pet trick, I started with the idea that I like to bully my brother by repeating an action that creates an annoying sound. So I decided to make something that you can put on the door to create continuous knocking sound until the person has to come to the door to find out what is happening.

When the door is closed, the person with the controller can push the button to start the knocking and use the potentiometer to control the speed of the knock. When the person inside the room finds the sound annoying and opens the door to see who is outside, the knocking stops.

Here is the code I used for the stupid pet trick:

#include ;

int angle = 0;
Servo myServo;
int button = 3;
bool onOff = false;
bool prevButtonState = LOW;
int knob = A0;

void setup() {
pinMode(button, INPUT);
myServo.attach(9);
Serial.begin(9600);
}

void loop() {
bool currButtonState = digitalRead(button);
int knobState = analogRead(knob);
int knockSpeed = map(knobState, 0, 1023, 0, 350);

if (currButtonState == HIGH && prevButtonState == LOW) {
onOff = !onOff;
}

if (onOff) {
for(angle = 0; angle < 90; angle ++);
myServo.write(angle);
delay(analogRead(knockSpeed));
for(angle = 90; angle >0; angle –);
myServo.write(angle);
delay(analogRead(knockSpeed));
} else {
myServo.write(LOW);
}

prevButtonState = currButtonState;
}

Developing on my stupid pet trick, I created a small scale demonstration of the device in action. In doing so, I made the device work more automatically by getting rid of the button and the potentiometer, so now, the knocking continues when the door is closed until the door opens.

Here is the code I used for Assignment 4:

#include ;

Servo fistServo;
int button = 3;
bool onOff = false;
bool prevButtonState = LOW;
int knockRate = 500;
bool knockOn = false;
unsigned long currentMillis = 0;
int knockDirection = 0;
int nextKnockTime;

void setup() {
pinMode (button, INPUT);
fistServo.attach(5);
Serial.begin(9600);
fistServo.write(30);
}

void loop() {
bool currButtonState = digitalRead(button);
Serial.println(currButtonState);

bool prevOnOff = onOff;
onOff = currButtonState;
if (onOff)
currentMillis = millis();

if (currentMillis > nextKnockTime) {
knockOn = true;
nextKnockTime = currentMillis + knockRate;
}

if (onOff) {
if (knockOn) {
if (knockDirection == 0)
fistServo.write(30);
else if (knockDirection == 1)
fistServo.write(0);
knockOn = false;
knockDirection = 1 – knockDirection;
}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *