Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //---------LIBRARIES
- #include <Servo.h>
- //---------CONSTANTS(won't change)
- const int servoPin = 9;
- const int buttonPin = 8;
- const int servoMinDegrees = 10;
- const int servoMaxDegrees = 170;
- //---------VARIABLES(will change)
- Servo myservo;
- int val = 5;
- int servoPosition = 90;
- int servoSlowInterval = 80;
- int servoFastInterval = 10;
- int servoInterval = servoSlowInterval;
- int servoDegrees = 25;
- unsigned long currentMillis = 0;
- unsigned long previousServoMillis = 0;
- //==============
- void setup() {
- Serial.begin(9600);
- Serial.println("Hinata is bae");
- pinMode(buttonPin, INPUT_PULLUP);
- myservo.write(servoPosition);
- myservo.attach(servoPin);
- }
- //==============
- void loop() {
- currentMillis = millis();
- servoSweep();
- }
- //==============
- void servoSweep() {
- if (digitalRead(buttonPin) == HIGH){
- val = val + 1;
- }
- if (val > 1){
- val = 0;
- }
- if (val < 1){
- if (currentMillis - previousServoMillis >= servoInterval){
- previousServoMillis += servoInterval;
- servoPosition = servoPosition + servoDegrees;
- if(servoPosition <= servoMinDegrees){
- if(servoInterval == servoSlowInterval){
- servoInterval = servoFastInterval;
- }}
- else {
- servoInterval = servoSlowInterval;
- }
- if((servoPosition >= servoMaxDegrees)||(servoPosition <=servoMinDegrees)) {
- servoDegrees = - servoDegrees;
- servoPosition = servoPosition + servoDegrees;
- }
- myservo.write(servoPosition);
- }
- }
- }
Add Comment
Please, Sign In to add comment