/*This sketch works as a servo tester using a pot and also a frequency generator
depending on the selector swich. */
#include <Servo.h>
Servo servo1; // create servo1 object to control a servo
Servo servo2;
int potPin = A0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int selectPin = 7;
int outputPin = 5;
int frequency;
void setup() {
servo1.attach(9); // attaches the servo on pin 9 to the servo object
servo2.attach(10); // attaches the servo on pin 9 to the servo object
pinMode(selectPin, INPUT_PULLUP);
pinMode(outputPin, OUTPUT); // set the output pin as an output
}
/*-------------This is Servo Sweep via Pot--------------*/
void loop() {
Serial.begin(9600);
while(digitalRead(selectPin) == HIGH)
{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)
servo1.write(val); // sets the servo1 position according to the scaled value
servo2.write(val);//sets the servo2 position according to the scaled value
delay(15); // waits for the servo to reach the position
}//end of servo while
/*-------------This is Frequency Generator------------*/
while(digitalRead(selectPin)== LOW) {
val = analogRead(potPin); // read the value of the potentiometer
frequency = map(val, 0, 1023, 1, 4000); // map the potentiometer value
//to a frequency (40 Hz to 4000 Hz)
digitalWrite(outputPin, HIGH); // set the output pin to HIGH
delayMicroseconds(500000 / frequency); // wait for half the period(mSec). T=1/f
digitalWrite(outputPin, LOW); // set the output pin to LOW
delayMicroseconds(500000 / frequency); // wait for the other half of the period
Serial.println(val);
Serial.print("fequency = ");
Serial.println(frequency);
Serial.print("On Time = ");
Serial.print(float(500000 / frequency));
Serial.print(" Microseconds");
delay(3000);
}//end of frequency while
}