Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Matthew Park
- * Servo Motor, function map, constrain, Learn to use libraries
- *
- * When you use a library, must INCLUDE the library.
- */
- #include <Servo.h>
- Servo servito;
- const int PinPot=A0;
- const int PinLed=2;
- int Value;
- int Angle;
- int Min = 1000;
- int Max = 0;
- void setup() {
- // put your setup code here, to run once:
- pinMode(PinPot, INPUT);
- servito.attach(9);
- Serial.begin(9600);
- calibrate();
- }
- void calibrate()
- {
- digitalWrite(PinLed, HIGH);
- delay(50);
- digitalWrite(PinLed,LOW);
- unsigned long intTime=millis();
- while (millis()-intTime <=5000)
- analogRead(PinPot);
- if( Value < Min)
- {
- Min = Value;
- }
- if( Value > Max)
- {
- Max=Value;
- }
- }
- void loop() {
- // put your main code here, to run repeatedly:
- Value = analogRead(PinPot);
- Angle = map(Value, Min,Max,0,180);
- Angle = constrain(Angle, 0, 180);
- servito.write(Angle);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement