Upon a button press move to 90-degrees and turn LED on.
When button is released move to 0-degrees and turn LED off.
So what I think you probably want to do is in the setup function set the input and output pins and move the servo to 0-degrees.
In the loop function you could read the state of the button pin then using an if...else move to 90-degrees w/ LED or to 0-degrees w/o LED.
You could make things a little more elaborate by using a state variable so it only moves the servo on a change in state and some debouncing on the button code.
First there's no reason to read the switch input twice in one loop. This code is going to execute in milli-seconds and reading it switch could actually goof up the timing.
Second, the code you have would actually do something more like the pseudo-code I'd posted last where when the button is pressed it goes to 90 (over and over) and if the buttons released it goes to 0 (over and over).
What it sounds like you really want is some kind of state check to see if the button state has changed since last time (HIGH to LOW or LOW to HIGH) and to save what position the servo is in.
I'll work on some code for it. I haven't used the timer control for servos so I'm assuming that part is right.
int Switch1=2;
int val=0;
int Ledpin=13;
#define servo1control OCR1A
#define servo1null 1500
int buttonState = 0;
int servoPosition = 0;
void setup(){
pinMode(Switch1, INPUT);
pinMode(9,OUTPUT);
pinMode(Ledpin,OUTPUT);
TCCR1B = 0b00011010; // Fast PWM, top in ICR1, /8 prescale (.5 uSec)
TCCR1A = 0b10100010; //clear on compare match, fast PWM
// to use pin 9 as normal input or output, use TCCR1A = 0b00100010
ICR1 = 39999; // 40,000 clocks @ .5 uS = 20mS
servo1control = servo1null; // controls chip pin 15 ARDUINO pin 9
}
void loop(){
val = digitalRead(Switch1); //Read switch value
if (val == HIGH) { //If switch is pressed
if (buttonState == 0) { //and switch didn't used to be pressed
if (servoPosition == 0) { //and servo is in original position
servo1control = 3300; //move servo to other position
digitalWrite(Ledpin, HIGH); //turn LED on
servoPosition = 1; //and set servo state to other
}
else { //If servo was already in other position
servo1control = servo1null; //move servo to original position
digitalWrite(Ledpin, LOW); //turn led off
servoPosition = 0; //and set servo state to original
}
buttonState = 1; //either way set button state to pressed
}
}
else {
buttonState = 0; //if button isnt pressed set state as such
It worked good after sorting out the brackets... I thank you very much anyway for giving me this direction.
The only problem is I get some erratic result sometimes. I tried putting a delay after each move of the servo. It did calm down the weird moves of the motor. If I push the button rapidly the pogram "jumps" a push. I guess this is caused by the switch contact that is poor quality and is sending several HIGH and LOW through one cycle down or up. If I do it slowly its perfect
I will also try using the existing library but it just seems easier to me to have a code right in front of me and not some kind of file that I don't know nothin about...I will learn that too sometime but now I add a project to deliver.
Thank you for your help !!
I will post the final code and a film of the result soon.
That's probably because the code I gave you does not do any debouncing of the pushbutton. Pushbuttons are notorious for giving noisy data especially at the beginning and end of a press.
You can search the forum or google for "debouncing code" and probably find many ways to add it.