Tuesday, October 26, 2010

Arduino Servo Sweep

ArduinoServoSweep1.pde



// Sweep
// by BARRAGAN a href="http://barraganstudio.com">http://barraganstudio.com>

#include

Servo myservo; // create servo object to control a servo

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

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

void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 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 = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}



http://arduino.cc/en/Tutorial/Sweep








here is another piece of code i found that makes an attached led blink, but at irregular rates.


/* Heartbeat
* Paul Badger 2009
* Demonstates how to use an array with millis to achieve irregular rhythms
* This function still uses one delay but it never leaves the LEDpin / LED in
* the ON state.
*/


long heartBeatArray[] = {
50, 100, 15, 1200};
int hbeatIndex = 1; // this initialization is important or it starts on the "wrong foot"
long prevMillis;

int LEDpin = 13;

void setup()
{
Serial.begin(9600);
pinMode(LEDpin, OUTPUT);
}

void loop()
{
heartBeat(1.0); // try changing the parameter
}

void heartBeat(float tempo){
if ((millis() - prevMillis) > (long)(heartBeatArray[hbeatIndex] * tempo)){
hbeatIndex++;
if (hbeatIndex > 3) hbeatIndex = 0;

if ((hbeatIndex % 2) == 0){
digitalWrite(LEDpin, HIGH);
delay((int)heartBeatArray[hbeatIndex]) ;
digitalWrite(LEDpin, LOW);
}
hbeatIndex++;
// Serial.println(hbeatIndex);
prevMillis = millis();

}
}

No comments:

Post a Comment