/*
   Sweep adapter for the ATtiny85, using the AdaFruit SoftServo library.
   Servo is connected to pin PB0.
   Example created especially for Blake Madison
*/

#include <Adafruit_SoftServo.h>  // SoftwareServo (works on non PWM pins)

//#include <TinyDebug.h> //Only when testing on WOKWI

Adafruit_SoftServo myservo;

const int STEP_DELAY = 100; //Might have to change this
int currPos = -1;    // current servo position

void setup() {
  myservo.attach(PB0);
  pinMode(PB1, INPUT_PULLUP);
  pinMode(A3, INPUT); //PB3
  pinMode(A2, INPUT); //PB4
  pinMode(A1, INPUT); //PB2

  currPos = ((float)analogRead(A3)/1024) * 180;
}

long int lastUpdate = 0;


void loop() {

  int startPos = ((float)analogRead(A3)/1024) * 180;
  int endPos = ((float)analogRead(A2)/1024) * 180;
  int speed = max(((float)analogRead(A1)/1024) * 180, 1);

  if(!digitalRead(PB5)){
    if(currPos > endPos){      
      currPos = max(currPos-speed, endPos);
    } else {
      currPos = min(currPos+speed, endPos);
    }
    myservo.write(currPos);             
    myservo.refresh(); 
    delay(STEP_DELAY); 
  }
  else{
    
     if(currPos > startPos){      
      currPos = max(currPos-speed, startPos);
    } else {
      currPos = min(currPos+speed, startPos);
    }
     myservo.write(currPos);
    myservo.refresh(); 
    delay(STEP_DELAY); 
    
  }
}


// Note: you can also use a timer to automatically refresh the position of the servo for you.
// For example, check this out: https://github.com/adafruit/Adafruit_SoftServo/blob/master/examples/TrinketKnob/TrinketKnob.ino 
ATTINY8520PU