ok, so now i've decided to do a little more with this, the butterfly's wings will move back and forth at a steady pace while a green led blinks in at the same time, then when one walks in front of it, the wings will pick up speed and a red led will start blinking at the same time. based on the code developed earlier this is what i came up with:
#include <Servo.h>
#define NUMBER_OF_READINGS 10
// create servo object to control a servo
// a maximum of eight servo objects can be created
Servo myservo1; Servo myservo2;
int ledPin1 = 13; // LED connected to digital pin 13
int ledPin2 = 8; // LED connected to digital pin 8
int pos = 360; // variable to store the servo position ;
byte pingPin = 7;
byte distanceThreshold = 5; //you need to stand closer than 5 cm
void setup()
{
pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT);
myservo1.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(10); //attaches the servo on pin 10 to the servo object
}
void loop()
{
//if it is true that the current distance is below the threshold: execute code
if (getDistanceAverage() <= distanceThreshold ){
for(pos = 0; pos < 75; pos +=1) // goes from 0 degrees to 75 degrees
{ // in steps of 1 degree
myservo1.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
myservo2.write(pos);
delay(5);
}
for(pos = 75; pos > 0; pos -=1) // goes from 75 degrees to 0 degrees
{
myservo1.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
myservo2.write(pos);
delay(5);
}
{
digitalWrite(ledPin1, HIGH); // sets the LED on
delay(500); // waits for a second
digitalWrite(ledPin1, LOW); // sets the LED off
delay(500); // waits for a second
}
} else {
while (getDistance() > distanceThreshold){
for(pos = 0; pos < 75; pos +=1) // goes from 0 degrees to 75 degrees
{ // in steps of 1 degree
myservo1.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
myservo2.write(pos);
delay(15);
}
for(pos = 75; pos > 0; pos -=1) // goes from 75 degrees to 0 degrees
{
myservo1.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
myservo2.write(pos);
delay(15);
}
{
digitalWrite(ledPin2, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin2, LOW); // sets the LED off
delay(1000); // waits for a second
}
}
}
}
byte getDistanceAverage(){
byte averageDistance = getDistance();
for (byte i=0; i<NUMBER_OF_READINGS; i++){
averageDistance += getDistance();
averageDistance /= 2;
}
return averageDistance;
}
//Tutorial/Ping
byte getDistance(){
long duration=0;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
return duration / 29 / 2;
}
At this point, it doesn't do anything until i move up to it and then it interchanges speeds and the leds only blink between wing turns.
I also tried it with the first code that you developed:
#include <Servo.h>
#define NUMBER_OF_READINGS 10
// create servo object to control a servo
// a maximum of eight servo objects can be created
Servo myservo1; Servo myservo2;
int ledPin1 = 13; // LED connected to digital pin 13
int ledPin2 = 8; // LED connected to digital pin 8
int pos = 360; // variable to store the servo position ;
byte pingPin = 7;
byte distanceThreshold = 5; //you need to stand closer than 5 cm
void setup()
{
pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT);
myservo1.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(10); //attaches the servo on pin 10 to the servo object
}
void loop()
{
//if it is true that the current distance is below the threshold: execute code
if (getDistance() <= distanceThreshold ){
for(pos = 0; pos < 75; pos +=1) // goes from 0 degrees to 75 degrees
{ // in steps of 1 degree
myservo1.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
myservo2.write(pos);
delay(5);
}
for(pos = 75; pos > 0; pos -=1) // goes from 75 degrees to 0 degrees
{
myservo1.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
myservo2.write(pos);
delay(5);
}
{
digitalWrite(ledPin1, HIGH); // sets the LED on
delay(500); // waits for a second
digitalWrite(ledPin1, LOW); // sets the LED off
delay(500); // waits for a second
}
} else (getDistance() > distanceThreshold); {
for(pos = 0; pos < 75; pos +=1) // goes from 0 degrees to 75 degrees
{ // in steps of 1 degree
myservo1.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
myservo2.write(pos);
delay(15);
}
for(pos = 75; pos > 0; pos -=1) // goes from 75 degrees to 0 degrees
{
myservo1.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
myservo2.write(pos);
delay(15);
}
{
digitalWrite(ledPin2, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin2, LOW); // sets the LED off
delay(1000); // waits for a second
}
}
}
//Tutorial/Ping
byte getDistance(){
long duration=0;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
return duration / 29 / 2;
}
the leds do the same thing, im not sure if i need to have a seperate while for each one so that the leds can blink along with the servos. it works fine otherwise, except that when one moves in front of it, it will only do the faster movement one time and then it goes back to being slow again and i'd really like for it to continue to go fast the entire time that someone is in front of it.
I really hope all of that made sense. :o
/me