#include <SimpleRotary.h>
#include <Mouse.h>
int led = 10;           // the pin the LED is attached to
byte brightness = 0;
// soldered my scrollwheel to pins 6 and 7. so fill in the pins that you connected your scrollwheel to. if you have a button asw fill // in the pin you connected that to in the last field, but since I have no middle mouse click connected I left it at 0.
SimpleRotary rotary(6, 7, 0);

void setup() {
  // startup the mouse code
  pinMode(led, OUTPUT);
  Mouse.begin();
}
void loop() {
  // put your main code here, to run repeatedly:
  byte i;
  i = rotary.rotate();
  analogWrite(led, brightness);


  if ( i == 0) {
    //Serial.print("not scrolling");
    //if the scrollwheel is not moving, we want it to dim the light. So we call the function that dims the light.
    aftellen();
  }

  //the following 2 functions is what happens if the scrollwheel is moving.
  // Clock wise rotation
  if ( i == 1 ) {
    //it's moving, so make the led brighter
    brightness++;
    // if you want to check if your scrollwheel works you can fill in the following code: (otherwise you can just delete it to clean things up)
    //Serial.println(brightness);
    //Serial.print("CCW");
    //Serial.print("\t");
    //Serial.println(i);
    //Mouse.move() let's us either move the x/y-axis or the mouse wheel (which is the third one). in this code if the i is the same as 1
    // it'll tell the virtual mouse to scroll up 1 whatever they use to measure this.
    Mouse.move(0, 0, 1);
    // turns clockwise
  }

  // counter Clock Wise rotation
  if ( i == 2 ) {
    brightness++;
    // if you want to check if your scrollwheel works you can fill in the following code: (otherwise you can just delete it to clean things up)
    //Serial.println(brightness);
    //Serial.print("CCW");
    //Serial.print("\t");
    //Serial.println(i);
    // exact same as before but the other way around
    Mouse.move(0, 0, -1);
    // turns counter clock wise
  }

}

//because the arduino does not work in seconds but in refresh rate, we need a function that adds a delay to every time we dim the light. Else it'd be impossible to make the light glow brighter
//this is the function that handles that
void aftellen()
{
  //this is the amount of miliseconds you want the interval to be
  static const unsigned long REFRESH_INTERVAL = 100; // ms
  static unsigned long lastRefreshTime = 0;

  //here it checks if the amount the of milliseconds the board has been running - the var "lastrefreshtime" is bigger or even as the refresh interval we assigned
  if (millis() - lastRefreshTime >= REFRESH_INTERVAL)
  {
    //if it is true, we make the last refresh time our refresh interval.
    lastRefreshTime += REFRESH_INTERVAL;
    //then we check if the var brightness is bigger than 0. because our light cant emit -100 energy.
    if (brightness > 0) {
      //as long as it is, it'll try to dim the light
      brightness--;
      //this line is for testing if everything works, just remove the two "//"'s and the variable will start to print in the serial monitor
      //the serial monitor can be accessed by pressing ctrl + shift + M or going to tools -> serial monitor
      //Serial.println(brightness);
    }
  }
}
