Pages: [1] 2   Go Down
Author Topic: moving a servo and fading leds, a newbies prob  (Read 1291 times)
0 Members and 1 Guest are viewing this topic.
Pittsburgh
Offline Offline
Newbie
*
Karma: 0
Posts: 28
It's full of stars.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?  :-/


Quote
/**
 * 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;
}

Logged

North Yorkshire, UK
Offline Offline
Faraday Member
**
Karma: 104
Posts: 5531
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what you hitting a wall about then?

Mowcius
Logged

Pittsburgh
Offline Offline
Newbie
*
Karma: 0
Posts: 28
It's full of stars.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.   smiley-sad

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

North Yorkshire, UK
Offline Offline
Faraday Member
**
Karma: 104
Posts: 5531
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Mowcius
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24452
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
       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)
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pittsburgh
Offline Offline
Newbie
*
Karma: 0
Posts: 28
It's full of stars.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

North Yorkshire, UK
Offline Offline
Faraday Member
**
Karma: 104
Posts: 5531
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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-grin

Mowcius
Logged

Pittsburgh
Offline Offline
Newbie
*
Karma: 0
Posts: 28
It's full of stars.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Quote

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

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pittsburgh
Offline Offline
Newbie
*
Karma: 0
Posts: 28
It's full of stars.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pittsburgh
Offline Offline
Newbie
*
Karma: 0
Posts: 28
It's full of stars.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well thanks for the info anyways. I guess it's back to the multiple Arduino board option.  smiley-sad :'( :-[
Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
« Last Edit: November 25, 2009, 11:41:40 pm by mem » Logged

Pittsburgh
Offline Offline
Newbie
*
Karma: 0
Posts: 28
It's full of stars.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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;
}
}
« Last Edit: November 29, 2009, 11:03:02 pm by tatticus » Logged

South Louisiana
Offline Offline
Full Member
***
Karma: 0
Posts: 178
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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
   }
Logged

Pages: [1] 2   Go Up
Jump to: