servo; leds, muti switching, and other fun stuff---HELP!!!

this is what i want to to...its sounds much more simple than it is proving to be.

one servo waits for a switch to be activated and it moves into one position and stops
the arm attached to the servo trips another switch which turns one led on (that stays on) and two leds that flash in sequence with each other
then either the same switch that was used to activate the servo or a different switch is activated moves the servo back into it original position where it stops and waits for the switch to be activated again telling it to move

now i have tried putting a switch in the ground line for the leds that that works ok for the most part but the sequenced leds just pick back up at what ever part of the cycle the programming happens to be at when the switch is activated, this is not what i want. what i want is for the sequence to start when the switch is activated.

and as for the servo it just has to go to the position i want it to go when i hit the switch, i don't want to be adjusting it with a pot or anything like that

the wiring i have pretty well figured out its the coding that's giving me fits. i have found coding for one part of thing or another but have not been able to figure out how to bring it all together.

i thank any and all that can help me with this in advance

You start by looking for a switch being active. When it sees that, it should move a servo. Next you want another swtich to be seen as active. When it sees that it stops the first servo. etc...

You just gradually build your sketch. This is how it's done.

What have you done so far? Basic servo button/switch code. note that servos usually need an external power supply.

//zoomkat servo button test 7-30-2011
//Powering a servo from the arduino usually *DOES NOT WORK*.

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
  }
  else {
    servo1.write(20);
  }
}

The Thread planning and implementing a program might have some useful stuff.

...R

that does help zoomkat i will start from there and see where it goes thanks

I read threw most of that thread robin and it see ways will help in the other projects...thank you

this is where i am at with this...i am not going into top to bottom codes here just the functionality in the order they are to occur (sorry if it isn't the terminology you folks are used to but it should give you an idea of what is to be going on)

momentary switch 1 (ON) --> moves servo 90 degrees (when momentary switch 1 is (OFF) servo stays in place

when servo moves to the 90 degree position the arm attached to the servo presses momentary switch 2

momentary switch 2 (ON) --> starts the following sequence
led = HIGH
led2 = HIGH
delay = (500)
led2 = LOW
delay = (500) >---- this code does exactly what i want it do for the leds
led3 = HIGH /
delay = (500) /
led3 = LOW /
delay = (500) /

momentary switch 2 (OFF)---> stops the sequence and all leds are LOW [momentary switch 2 is switched (OFF) when the arm attached to the servo moves]

momentary switch 3 (ON)---> returns servo to position (0)
momentary switch 3 (OFF)--->servo doesn't move

eventually i will be adding xbees to send the signals for momentary switches 1 and 3 to the servos, by design momentary switch 2 functionality will take care of itself. but for now if i can just get the thing to work like i want it to hard wired i will be happy.

it would be nice if something could be put in there that tells the servo to ignore switch 1 if it is already at 90 degrees and to ignore switch 3 if it is already at 0 not sure if that is something it will do on its own of is it is something that it needs to be told

Using the description in Reply #5 as a base, can you describe exactly what does happen and what you would like to happen.

My suspicion is that all those delay()s will get in the way of detecting the switches.

...R

i'll take another look at that robin maybe i missed something...thanks

Two button servo code.

//zoomkat servo button test 12-29-2011
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }    
  
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
}

WOW...now i can't wait for my servo to finally get here so i can get this sucker up and running thanks zoomkat and robin

Something you need to be aware of with servos is they're controlled by a pwm signal. Now when you send a PWM signal from your arduino, it maintains that signal without any further action by you.

So imagine a scenario where your servo is sitting at the 90 degree position. If you send the instruction to go to 10 degrees, it will immediately start its progress in that direction. If at say.. 20 degrees it then makes contact with your microswitch, You can't just casually turn off that PWM signal and expect the servo to stop dead in it's tracks. Turning off the signal is the same as telling it to go to 0 degrees (which, in this case, is tha last thing you want to do). So unless that switch is on something that can move out of the way, there's a very real danger that you're going to damage something.

One way around this problem is to move your servo in very tiny increments (like 1 degree at a time). When the switch gets triggered, you can then stop sending those increments. (and maybe even back it up 1 degree to be sure)

well as of now i have finally come up with a code that includes the servo and the leds that doesn't give me any angry red letters at the bottom of the screen so now all this left is to do is test it.

and i have thought about the potential breaking of things so for its first full test nothing will be mounted on anything and i will be triggering the switches manually until i get everything tweaked in to submission.

tested the LED part out and it works fine, even though i did forget to tell one of them that it needed to turn off...lol...but fixed that problem. i'll test the servo part when it finally gets here.

servo finally got here hooked everything up and it all works great.

now i would like to see if i can tweak the code a bit to slow down how fast the servo moves and so the lights come on when the servo moves to the 90 degree position WITH OUT a switch at all and then turns them off when it returns to the 0 position.

this is my current code:

#include <Servo.h>
int button1 = 10;
int press1 = 0;
int button2 = 9;
int press2 = 0;
Servo servo1;
int button3 = 8;
int press3 = 0;
int led1 = 13;
int led2 = 12;
int led3 = 11;

void setup()
{
pinMode (button1, INPUT);
pinMode (button2, INPUT);
pinMode (button3, INPUT);
pinMode (led1, OUTPUT);
pinMode (led2, OUTPUT);
pinMode (led3, OUTPUT);
servo1.attach(6);
digitalWrite(9, HIGH);
digitalWrite(8, HIGH);
digitalWrite(10, HIGH);
}

void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
servo1.write(90);
}
press2 = digitalRead(button2);
if (press2 == LOW)
{
servo1.write(0);
}
press3 = digitalRead(button3);
if (press3 == LOW)
{
digitalWrite (led1, HIGH);
}
else
{
digitalWrite (led1, LOW);
}
if (press3 == LOW)
{
digitalWrite (led2, HIGH);
delay (250);
digitalWrite (led2, LOW);
delay (250);
digitalWrite (led3, HIGH);
delay (250);
digitalWrite (led3, LOW);
delay (250);
}
}

now i would like to see if i can tweak the code a bit to slow down how fast the servo moves

Have you looked at the servo "sweep" example?

The demo several things at a time illustrates how to move a servo at a controlled speed.

...R

i ave but i will look at it again see if i might have missed something