servo rotation and activation

How do I program my arduino to turn my servo 45 degrees stay there and only turn back to 0 degrees when I complete my circuit again using recessed magnetic contact?

I was able to programm the servo to turn the 45 degrees, but once it reaches the 45 degrees it immediatly starts going back to 0 degrees. I want it to go to 45 degrees when I bring my magnets together and stay there once my circuit is complete. when I bring the magnets together again Then I want it to go back to 0 degrees. I have inserted a picture of my current progamming below.

prent van programming.pdf (107 KB)

How do I program my arduino to turn my servo 45 degrees stay there and only turn back to 0 degrees when I complete my circuit again using recessed magnetic contact?

You need to detect when the switch becomes pressed rather than when it is pressed

Look at the StateChangeDetection example in the IDE

The PDF file you posted is unreadable, post your code between code tags like this:
Type [code] before the first line of your code,
Type [/code] after the last line.

Why have you written delay(5000); in your codes in place of delay(15);.

GolamMostafa:
Why have you written delay(5000); in your codes in place of delay(15);.

Don't worry. Just look at how many steps there are in the for loops

UKHeliBob:
Don't worry. Just look at how many steps there are in the for loops

I am worry because the delay(5000); will prevent the servo from maintaining the shaft position at the desired point set by the servo.write(pos); command.

To maintain the shaft position at the new point, the control pin of the servo requires to receive control signal usually at 20 ms apart. The 5000 ms delay after the servo.write(pos); command violate this requirement.

I am worry because the delay(5000); will prevent the servo from maintaining the shaft position at the desired point set by the servo.write(pos); command.

If you are right then after running this

#include <Servo.h>
Servo theServo;

void setup()
{
  Serial.begin(115200);
  theServo.attach(8);
  theServo.write(90);
}

void loop()
{
}

then the servo would not retain its position but that is not the case.

GolamMostafa:
To maintain the shaft position at the new point, the control pin of the servo requires to receive control signal usually at 20 ms apart. The 5000 ms delay after the servo.write(pos); command violate this requirement.

I have derived my above understanding from the flowing IDE example where the control signal in being asserted at every 15 ms interval. I also checked the presence of the signal with scope.

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup() 
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() 
{
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
}

After going home from my work place, I will test your codes of Post#6 and accordingly I will be ready to update/adjust my understanding.

The example reads the pot, map()s it to a smaller range (although simply dividing by 4 would have been easier) and outputs it to the servo. The 15ms delay() is just to make the whole process smoother, not to maintain the output to the servo.

The Servo library uses interrupts to maintain the output to the pin so it does not have to be refreshed explicitly

UKHeliBob:
The Servo library uses interrupts to maintain the output to the pin so it does not have to be refreshed explicitly.

This appears to be the intelligence which I didn't have before.

GolamMostafa:
This appears to be the intelligence which I didn't have before.

Before trying to comment on how libraries work it is a good idea to either do some testing or, perhaps even better, read the library code. It's all readily available.

Steve

It is a human culture that some level of confidence is always there on the propagated knowledge until it is proved no more valid. This tendency is also seen in the field of science and engineering though not much in technology. The Arduino Forum is a gathering house of the experienced veterans many of whom don't posses the hardware setup to verify the proposition being presented/commented; but, they speak meaningful things for most of the times.

@UKHeliBob

The following observations are accounted based on actual measurements using Tower Pro SG90 Servo Motor, UNO, and the program codes of your Post#6.

#include <Servo.h>
Servo theServo;

void setup()
{
  Serial.begin(115200);
  theServo.attach(8);
  theServo.write(30);
}

void loop()
{

}

1. The servo needs continuous signals at its control pin to retain the shaft position as desired by the servo.write(arg); command.

2. In the above program, the servo.write(arg); instruction has been executed only once; the shaft position has gone to 9030oposition. Scope shows that there are continuous control pulses at the control pin of the servo at 20 ms interval. Who is maintaining these pulses? Of course, as @UKHeliBob has indicated, this job must be done by an ISR under the supervision of the MCU.

3. When either control pin of the servo is disconnected or UNO is kept at RESET state or control signal is prevented from arriving at the control pin, the servo goes back to the original position.

4 As ISR is maintaining the control signal, there is no need to refresh it by the user codes – very good comment of @UKHeliBob.

the shaft position has gone to 90o position

In view of the code I would hope that it would not go to that angle…

UKHeliBob:
In view of the code I would hope that it would not go to that angle…

It is 30o; my bad, typo mistake!