antisocial cat_prototype 006 code iteration
So, yeah, I figured out what the problems were and finally made the servo work as the way I want! Hooray!
When the toy detects me (using sensor), it would turn its head and stay at that position until it detects me again and turns its head.
http://vimeo.com/moogaloop.swf?clip_id=2414067&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=&fullscreen=1
prototype 006 code iteration from maze on Vimeo.
And here is the code:
———————————————————–
#include <Servo.h>
Servo myservo; //create servo object to control a servo
int sensor = 0; // analog pin used to connect the sensor
int motorPin=11;
int val; // variable to read the value from the analog pin
boolean status=0; //status of detecting
int counter=0;
void setup()
{
myservo.attach(11);
myservo.setMaximumPulse(2200);
Serial.begin(9600); // set up Serial library at 9600 bps
pinMode(sensor, INPUT);
//pinMode(relay, OUTPUT);
pinMode(motorPin, OUTPUT);
myservo.setMaximumPulse(2000);
myservo.setMinimumPulse(700);
Serial.print("Ready\n");
}//end of setup
int getSensor() {
val = analogRead(sensor); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
val=max(val,5);
val=min(val,180);
return val;
} //end of getSensor
void myRefresh(int delayTime){
for(int i=0; i < delayTime/20; i++){ //delay is the total ms delay we want, 20 is the delay per iteration of the loop
Servo::refresh();
delay(20);
}
}
int move0(){
Serial.print("servo position 0\n");
myservo.write(0);
//Servo::refresh();
myRefresh(100);
}//end of move0
int move90(){
Serial.print("servo position 90\n");
myservo.write(90);
//Servo::refresh();
myRefresh(100);
}//end of move90
int move180(){
Serial.print("servo position 180\n");
myservo.write(180);
//Servo::refresh();
myRefresh(100);
}//end of move180
void loop()
{
val=getSensor();
Serial.println(val);
if (val>20){
if(status==0){
move180();
myRefresh(1000); // waits for the servo to get there
counter++;
}
else if(status==1){
move0();
myRefresh(1000);
counter++;
}//end of else
}//end of if
if(counter%2!=0){
status=1;
}else{
status=0;
}
//myservo.write(getSensor());
//Serial.println(getSensor());
//delay(15);
Servo::refresh();
}
—————————————————————————–
I defined a function myRefresh() to make sure the servo would be refreshed every 20ms.
Then I added 3 moving functions with different angles.
The tricky part was that how to make servo stop and turn to reverse direction when the sensor detects someone again. By using 2 variables counter and status, I made servo turn to position 180 when the counter is even and turn to position 0 when it is odd.
In this week, I’ll try to put the prototype that I have so far into the body of my plush prototype.
The focus of the testing this week will be:
– redesign the character for prototyping (make it big enough to put all my electronics)
– building/ sewing the plush toy
– make the toy’s head move! Think about materials, the skeleton, connections… etc.
Reply