Wind Sensor, LED's, Servo and Arduino
Posted: Wed May 19, 2010 2:39 pm
I have your Wind sensor and a Servo powered separately on two different breadboards.
The Arduino powers the wind sensor and a battery powers the servo. (power is not a problem)
Each part of the code works fine separately but together the wind sensor becomes very delayed.
The delay(15) seems to be the cause. If I run it with both delays off, the servo has a convulsion but the wind sensor works fine.
Is there another way to write this code to have the best of both worlds?
A fast response for the wind sensor and the delays needed for the servo?
The feedback I get without the delays is instantaneous. As wind blows LEDs light-up.
I understand I need the delays for the servo.
How can I have it both ways? (1) instant feedback and (2) servo operation?
Or is this impossible?
Thanks
The Arduino powers the wind sensor and a battery powers the servo. (power is not a problem)
Each part of the code works fine separately but together the wind sensor becomes very delayed.
The delay(15) seems to be the cause. If I run it with both delays off, the servo has a convulsion but the wind sensor works fine.
Is there another way to write this code to have the best of both worlds?
A fast response for the wind sensor and the delays needed for the servo?
Code: Select all
// Wind sensor and Servo Sweep
byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; //pins used for LED's
#include <Servo.h>
Servo myservo; // servo object to control a servo
int pos = 0; // variable for servo position
void setup()
{
myservo.attach(3); // servo on pin 3 to the servo
//Serial.begin(9600);
// set all pins to output
for (int x = 0; x < 10; x++) {
pinMode (ledPin[x], OUTPUT); }
}
void loop()
{
int sensorValue = analogRead(2);
//Serial.println(sensorValue, DEC);
if (sensorValue > 160){ digitalWrite(ledPin[0], HIGH); }
if (sensorValue > 180){ digitalWrite(ledPin[1], HIGH); }
if (sensorValue > 200){ digitalWrite(ledPin[2], HIGH); }
if (sensorValue > 220){ digitalWrite(ledPin[3], HIGH); }
if (sensorValue > 240){ digitalWrite(ledPin[4], HIGH); }
if (sensorValue > 260){ digitalWrite(ledPin[5], HIGH); }
if (sensorValue > 280){ digitalWrite(ledPin[6], HIGH); }
if (sensorValue > 300){ digitalWrite(ledPin[7], HIGH); }
if (sensorValue > 320){ digitalWrite(ledPin[8], HIGH); }
if (sensorValue > 340){ digitalWrite(ledPin[9], HIGH); }
if (sensorValue < 160){ digitalWrite(ledPin[0], LOW); }
if (sensorValue < 180){ digitalWrite(ledPin[1], LOW); }
if (sensorValue < 200){ digitalWrite(ledPin[2], LOW); }
if (sensorValue < 220){ digitalWrite(ledPin[3], LOW); }
if (sensorValue < 240){ digitalWrite(ledPin[4], LOW); }
if (sensorValue < 260){ digitalWrite(ledPin[5], LOW); }
if (sensorValue < 280){ digitalWrite(ledPin[6], LOW); }
if (sensorValue < 300){ digitalWrite(ledPin[7], LOW); }
if (sensorValue < 320){ digitalWrite(ledPin[8], LOW); }
if (sensorValue < 340){ digitalWrite(ledPin[9], LOW); }
{ servoGo();}
}
void servoGo()
{
for(pos = 0; pos < 180; pos += 1) //0 to 180 degrees, by 1 degree steps
{
myservo.write(pos); // go to variable 'pos'
delay(15); // waits 15ms for servo to reach position
}
for(pos = 180; pos>=1; pos-=1) // 180 to 0 degrees, by 1 degree steps
{
myservo.write(pos); // go to variable 'pos'
delay(15); // waits 15ms for servo to reach position
}
} I understand I need the delays for the servo.
How can I have it both ways? (1) instant feedback and (2) servo operation?
Or is this impossible?
Thanks