moving a servo and fading leds, a newbies prob

I have this sketch that moves a servo in random increments to random positions. I'd like to add a couple pins of PWM LED's that would fade in and out at predetermined lengths, not randomly.

I know this has got to be super simple but I'm new at this and I've been hitting a wall. A deadline is looming ( I want to use this in a sculpture I'm making) and I would hate to have to put two Arduinos in the piece when I'm sure there is a way to code it all into one chip.

Any hints or known sketches that anybody can pass along? :-/

/**

  • randomly moves servo back and forth
    */

#include <Servo.h>

Servo servoHead;

int posA;
int posB;
int wait;
int rate = 2;

void setup()
{
servoHead.attach(5);

posB = posA = 90;

}

void loop()
{
if( wait == 0 ) {
posB = random(10,120); // move between 10-120 degrees
wait = random(200,400); // between 2 secs & 4 secs
}
else {
posA = slew_servo( posA, posB, rate);
wait--;
}

servoHead.write( posA );

delay(20);
}

// return new pos from current pos, dest and rate
int slew_servo( int pos, int dest, int rate)
{
int diff = pos - dest;
if( diff == 0 ) return pos;
else if( diff > -rate ) pos++;
else if( diff < rate ) pos--;
if( diff > 0 ) pos -= rate;
else if( diff < 0 ) pos += rate;
return pos;
}

what you hitting a wall about then?

Mowcius

I've tried putting in a fade function various ways. Every time I tried it I either A. got no light at all or B. got a nice fading LED but it interfered with the servo and caused it to move very erratically. :frowning:

I was hoping someone out there had a similar project at some point since it's such a simple task.

Can you post the code including the LED fading code, then I may be able to help.

Mowcius

        wait = random(200,400);     // between 2 secs & 4 secs
    }
    else {
        posA = slew_servo( posA, posB, rate);
        wait--;
    }
 
    servoHead.write( posA );

    delay(20);

A delay of 200 * 20 milliseconds is 4 seconds, and 400 * 20 is 8 seconds, so either the comment is off, or the arithmetic is off.

You need to look at the blink without delay example in the tutorials, and see how you could adapt it to move and fade at the same time.
(Big Hint: It can be done without two Arduinos)

AWOL, the comment is probably off unsurprisingly. I have a bad habit of playing around with the code without updating the comments.

MOWCLUS, I'll try putting one of the failed attempts back together and posting it. I never saved any of them. Something about being up at 3AM fighting with code just made me want to delete everything and start fresh the next day.

Something about being up at 3AM fighting with code just made me want to delete everything and start fresh the next day.

Yep, I think we all get that :smiley:

Mowcius

So here is where I am. I've got an LED blinking fine while the servo does it's random movements. I can't figure out how to fade that LED instead though. I opened up the analog fade example and tried inserting it into the code but I'm just not catching on. Would that PWM example code even work in this sketch in the first place?

This is the code up to now

#include <Servo.h>

Servo servoHead;
const int ledPin = 13; // the number of the LED pin
// stop crying, people are looking at me. It's only code it can't hurt me.

int posA;
int posB;
int wait;
int rate = 2;

int ledState = LOW;
long previousMillis = 0;
long interval = 6000;

void setup()
{
servoHead.attach(5);
posB = posA = 90;
pinMode(ledPin, OUTPUT);

}

void loop()
{

if (millis() - previousMillis > interval) {

previousMillis = millis();

if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(ledPin, ledState);
}

if( wait == 0 ) {
posB = random(10,120);
wait = random(200,400);
}
else {
posA = slew_servo( posA, posB, rate);
wait--;
}

servoHead.write( posA );

delay(20);
}

// return new pos from current pos, dest and rate
int slew_servo( int pos, int dest, int rate)
{
int diff = pos - dest;
if( diff == 0 ) return pos;
else if( diff > -rate ) pos++;
else if( diff < rate ) pos--;
if( diff > 0 ) pos -= rate;
else if( diff < 0 ) pos += rate;
return pos;
}

You won't be able to fade the LED while the servos are moving. Fading an LED requires use PWM, which you can not do while using servos. On the Duemilanove, at least.

Says so in the servo library cpp file.

I'm using a Bare Bones Board which I guess is a Diecimila which is older than the Duemilanove right? Apparently I'm outa luck on my fade quest?

It's the chip, not the board, that is limited. So, yes, you're out of luck.

Well thanks for the info anyways. I guess it's back to the multiple Arduino board option. :frowning: :cry: :-[

You won't be able to fade the LED while the servos are moving. Fading an LED requires use PWM, which you can not do while using servos. On the Duemilanove, at least.

Says so in the servo library cpp file

Not quite, it says: " analogWrite of PWM on pins associated with the timer are disabled when the first servo is attached."

the Duemilanove uses timer1 for the servos so only two PWM pins are lost.

As it says in the Arduino Servo library reference page, "On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10",

So there are four PWM pins that are still available.

Well this is one of the sketches i was trying to cobble together. I keep getting a "error slew_servo not declared in this scope" when I try to compile.

Anybody see any obvious newbie errors I'm making?

Thanks :-?

#include <Servo.h>

Servo servoHead;

int pulsewidth;
int time = 5;
int led = 11;
int posHead;
int posHdest, posEdest;
int wait;
int rate = 2;

void setup()
{
servoHead.attach(5);
posHdest = posHead = 90;

}

void loop()
{
// slowly fade the LEDs to full brightness
for (pulsewidth=0; pulsewidth <= 255; pulsewidth++){
analogWrite(led, pulsewidth);
delay(time);
}
// slowly dim the LEDs
for (pulsewidth=255; pulsewidth >= 0; pulsewidth--){
analogWrite(led, pulsewidth);
delay(time);
}

{ if( wait == 0 ) {
posHdest = random(70,110); // move had between 70-110 degrees
wait = random(150,450); // between 1.5 secs & 4.5 secs
}
else {
posHead = slew_servo( posHead, posHdest, rate);
wait--;
}

servoHead.write( posHead );

delay(10);
}

int slew_servo ( int pos, int dest, int rate)

{ int diff = pos - dest;
if (diff == 0 ) return pos;
else if( diff > -rate ) pos++;
else if( diff < rate ) pos--;
if( diff > 0 ) pos -= rate;
else if( diff < 0 ) pos += rate;
return pos;
}
}

I am fairly new to this whole game too but it looks to me like the highlighted bracket shouldn't be there. Just my 2 pennies....

for (pulsewidth=255; pulsewidth >= 0; pulsewidth--){  
   analogWrite(led, pulsewidth);  
   delay(time);  
 }  

[glow]{[/glow]  if( wait == 0 ) {
       posHdest = random(70,110);  // move had between 70-110 degrees
       wait = random(150,450);     // between 1.5 secs & 4.5 secs
   }

Good call. That did kind of get things rolling. It lets the sketch compile and LED pulse but the servo does nothing now. :cry:

UPDATE**** :exclamation

Well I got this code running! It's probably ugly judging by the forums standards but I'm trying my best. Thanks to all who chimed in and helped me through a lot of late night coding.

#include <Servo.h>
#define FADE_RATE3 2000
Servo servoHead;

int pwmPin3 = 3;
int posA;
int posB;
int wait;
int rate = 2;

void setup()
{
servoHead.attach(5);
pinMode(pwmPin3, OUTPUT);

posB = posA = 90;

}

void loop()
{ unsigned long time = millis();

long val = labs((FADE_RATE3 / 2) - (time % FADE_RATE3));
val = 255 * val / (FADE_RATE3 / 2); // convert to range 0..255..0
analogWrite(pwmPin3, val);

if( wait == 0 ) {
posB = random(10,120);
wait = random(200,400);
}
else {
posA = slew_servo( posA, posB, rate);
wait--;
}

servoHead.write( posA );

delay(20);
}

// return new pos from current pos, dest and rate
int slew_servo( int pos, int dest, int rate)

{
int diff = pos - dest;
if( diff == 0 ) return pos;
else if( diff > -rate ) pos++;
else if( diff < rate ) pos--;
if( diff > 0 ) pos -= rate;
else if( diff < 0 ) pos += rate;
return pos;
}

Yes, you see in that code, you have defined slew_servo:

int slew_servo( int pos, int dest, int rate)

Before, the IDE did not know what it was so was basically asking you that.

Mowcius