// Copy allof this ctrl A then ctrl C // then open your arduino IDE and paste ctrl V #include //imports the servo library #include //imports the EEPRom library //setup the servo Servo myservo; //give the servo a name int pos = 120; //tell the servo its starting position int endPos; //will be used to store the trimmed servo end position //set up the trigger switch byte TrigPin = A0; //which pin it will connect to on the nano int TrigRead = 1; //what state the trig switch is in 1 = vernier in holder 0 = vernier in use //set up the mode switch byte modePin = A1; //which pin it will connect to on the nano int mode = 0; //what state the mode switch is in 1 = in setup mode 0 = normel operation //set up the potentiometer byte trimPin = A2; //which pin it will connect to on the nano int trimReading; //will be used to store the reading from the potentiometer //this section only runs on initial start up void setup() { endPos = EEPROM.read(0); //read the saved value of the potentiometer from the nano memory myservo.attach(9); //tell the nano what pin theservo is connected to Serial.begin(9600); //start the serial monitor pinMode(TrigPin, INPUT); //tell the nano the trigger pin is an input pinMode(modePin, INPUT); //tell the nano the mode pin is an input pinMode(trimPin, INPUT); //tell the nano the trim pin is an input } // this is the loop, it is what runs round and round void loop() { mode = digitalRead(modePin); //read the mode pin and store the reading 1 or 0 if (mode == 1) { //if the reading from above is a 1 do from { trimming(); //to here } // this will go to the trimming function } else { //if the reading was a 0 do from { running(); // this will go to the running function } //to here } } //this is the function that controlls the servo for the setup process void trimming() { while (mode == 1) { //while themode switc read is 1 it will stay in thei loop Serial.println("In setup mode."); //lets you know you are in setup mode in the serial monitor trimReading = analogRead(trimPin); //reads thevaluefrom the potentiometer endPos = map(trimReading, 0, 1024, 0, 45); //takes the value of the potentiometer of 0-1024 and maps it to values from 0 - 45 Serial.println(endPos); //tells you how much you have adjusted it in the serial monitor myservo.write(endPos); //tells the servo to go to the mapped end positiom mode = digitalRead(modePin); //reads the mode pin to check if you have changed to normal running position } Serial.println("Write to eeprom."); //tells you it is saving the end position to the nano memory EEPROM.update(0, endPos); //this is where it actualy saves the value,in position 0 } //this is the function that controlls the servo in normal running mode void running() { TrigRead = digitalRead(TrigPin); //reads the trigger switch Serial.print("In Use : "); //lets you know the vernier is not in its mount/being used Serial.println(TrigRead); //prints the value of the triggerto the serial monitor if (TrigRead == 1) { //if the trigger value is 1 it will do from here { for (pos = 120; pos >= endPos; pos -= 1) { //the servo starts at of 120 degrees and will go through the for statment minusing 1 deg each time round till it reaches the value of the end position (set by the potentiometer) myservo.write(pos); //writes the new vallue for each round of the for statment tothe servo delay(15); //whates 15milliseconds then starts the for statment again till it is no longer true (pos and endPos are the same) } delay(2500); //how long it presses the on/off button on the vernier in milliseconds for (pos = endPos; pos <= 120; pos += 1) { //same as the above for statement but this time it starts at the endPos and adds 1 deg iach time it goes through the statment tillit is at 120 deg myservo.write(pos); // delay(15); // } while (TrigRead == 1) { //once it has moved the servo it will run this while statemnt till the trig switch = 1 TrigRead = digitalRead(TrigPin); //reads the trig switch to check it hasnt changed mode = digitalRead(modePin); //reads the mode pin to check it hasnt changed Serial.print("Mounted : "); //tells you the vernier is in its holder in the serial monitor Serial.println(TrigRead); //tells you the value of the trigger switch if (TrigRead == 0 || mode == 1) { //if the value of the trig switch = 0 or the mode switch = 1 break; //it breaks from this if statment and starts from the top of the running loop again } } } //to here } }