For my stupid pet trick, I made a woodpecker that pecks the phones. The concept is that the woodpecker does not like the phones because they make people anti-social. Here is the video of my product:
And here is the coding for the woodpecker to peck.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
int mySwitch = 2;
void setup()
{
pinMode(mySwitch, INPUT);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
if (digitalRead(mySwitch == HIGH)) {
for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(1); // waits 1ms for the servo to reach the position
}
for(pos = 180; pos>=0; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(1); // waits 1ms for the servo to reach the position
}
} else {
myservo.write(0);
}
}
I used an example that is provided by arduino, and changed the delay time so that the woodpecker pecks frequently enough.
Here, I tried creating a switch in a way that the woodpecker only clicks when the switch is closed. However, it did not work. I think that using an if else statement is correct, but misery.write(0) does not work as I think it does. I should be able to find out how I can do about it, but for now, I need to do other things so I cannot allocate the time to improving this product.