HAOTIAN_HOMEWORK

I make a fortune cat by using a servo and a pressure sensor. The fortune cat waves his hand to gather good luck and fortune. If you press his right hand, he will moves his hand faster.IMG_3141 IMG_3144

 

Code_________________________________________

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position

int sensorPin = 0;
int pre = 0;
int val = 0;

void setup()
{
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop()
{
pre = analogRead(sensorPin);
val = map(pre,0,500,1,10);

for(pos = 30; pos < 160; pos += val) // goes from 30 degrees to 160 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 160; pos>=30; pos-=val) // goes from 160 degrees to 30 degrees
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
Serial.println(val);
}

Leave a Reply

Your email address will not be published.