Wind Sensor, LED's, Servo and Arduino

Interfacing hardware, sensors, output drivers, chips.
Post Reply
emersonb
Posts: 9
Joined: Wed May 12, 2010 2:03 pm

Wind Sensor, LED's, Servo and Arduino

Post by emersonb » 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?

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
  }
} 
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

paul
Site Admin
Posts: 735
Joined: Mon May 12, 2008 4:19 pm

Re: Wind Sensor, LED's, Servo and Arduino

Post by paul » Sat Jun 05, 2010 8:45 am

Sorry for the Really slow reply emersonb ,
The notification system, despite many updates, still seems to be a little buggy.

My suggestion would be to rewrite the servoGo function with millis() - see the blink without delay sketch in Arduino IDE.
This should work but isn't tested.

Paul

Code: Select all

   void servoGo()
    {
      // lastMillis declared as unsigned long global variable above 
      // upDown, pos declared as int global variable above 
      if (millis() - lastMillis > 15){
      
        if (upDown ==  1) pos++;
        else pos --;
        
        if (pos > 180) upDown = 0;  // sweep down
        if (pos < 0)   upDown = 1;  // sweep up
        
        myservo.write(pos);                        
        lastMillis = millis();      // update lastMillis
     } 

Post Reply