Pittsburgh
Offline
Newbie
Karma: 0
Posts: 20
It's full of stars.
|
 |
« on: November 25, 2009, 09:02:38 am » |
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; }
|
|
|
|
|
Logged
|
|
|
|
|
North Yorkshire, UK
Offline
Faraday Member
Karma: 104
Posts: 5531
|
 |
« Reply #1 on: November 25, 2009, 10:00:42 am » |
what you hitting a wall about then?
Mowcius
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh
Offline
Newbie
Karma: 0
Posts: 20
It's full of stars.
|
 |
« Reply #2 on: November 25, 2009, 10:10:28 am » |
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.  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
Faraday Member
Karma: 104
Posts: 5531
|
 |
« Reply #3 on: November 25, 2009, 10:53:57 am » |
Can you post the code including the LED fading code, then I may be able to help.
Mowcius
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: November 25, 2009, 10:59:56 am » |
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.
|
|
|
|
Pittsburgh
Offline
Newbie
Karma: 0
Posts: 20
It's full of stars.
|
 |
« Reply #5 on: November 25, 2009, 11:09:41 am » |
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
Faraday Member
Karma: 104
Posts: 5531
|
 |
« Reply #6 on: November 25, 2009, 11:11:55 am » |
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  Mowcius
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh
Offline
Newbie
Karma: 0
Posts: 20
It's full of stars.
|
 |
« Reply #7 on: November 25, 2009, 08:04:03 pm » |
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; }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35533
Seattle, WA USA
|
 |
« Reply #8 on: November 25, 2009, 08:10:34 pm » |
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
Newbie
Karma: 0
Posts: 20
It's full of stars.
|
 |
« Reply #9 on: November 25, 2009, 08:16:49 pm » |
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
Online
Brattain Member
Karma: 316
Posts: 35533
Seattle, WA USA
|
 |
« Reply #10 on: November 25, 2009, 08:36:42 pm » |
It's the chip, not the board, that is limited. So, yes, you're out of luck.
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh
Offline
Newbie
Karma: 0
Posts: 20
It's full of stars.
|
 |
« Reply #11 on: November 25, 2009, 10:10:00 pm » |
Well thanks for the info anyways. I guess it's back to the multiple Arduino board option.  :'( :-[
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #12 on: November 25, 2009, 11:39:50 pm » |
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
Newbie
Karma: 0
Posts: 20
It's full of stars.
|
 |
« Reply #13 on: November 29, 2009, 10:58:25 pm » |
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
Full Member
Karma: 0
Posts: 178
Arduino rocks
|
 |
« Reply #14 on: November 29, 2009, 11:21:21 pm » |
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 }
|
|
|
|
|
Logged
|
|
|
|
|
|