int button = 2;   //pin of the first button
int servoPin = 3; //pin to attach the servo
int LED = LED_BUILTIN;
int lamp = 13;
int coil = 4;//
float annealTime;
float startTime;
float timeCount;
float value = 6;
volatile byte buttonFlag = 0;
volatile byte encoderFlag = 0;


#define outputA 6
#define outputB 7

 int counter = 0; 
 int aState;
 int aLastState;  

 

//******OLED Library******************************
#include <SPI.h>//must be all caps, apparently
#include <Wire.h>//first letter cap, apparently
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

//*****Encoder Library******************************

//*************************************************


//******Arduino Servo Library***********************
#include<Servo.h> //include the servo library, for ESP32 use "ESP32Servo.h" instead
//***************************************************

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);//create display object

Servo servo;  //create a servo object




void  setup() {

  timeCount = annealTime;
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);//start display including address
  display.clearDisplay();
  
  servo.attach(servoPin);  //pin  used by the servo
  servo.write(100);//start the servo in the correct position
  pinMode(LED,OUTPUT);//indicator

  pinMode(button, INPUT_PULLUP);  //define first button as  input pullup
  pinMode(coil,OUTPUT);// to SSR
  pinMode(lamp,OUTPUT);//external LED to indicate coil is active
  

  pinMode (outputA,INPUT);
  pinMode (outputB,INPUT);
   
  Serial.begin (9600);
  // Reads the initial state of the outputA
  aLastState = digitalRead(outputA);  
}





void anneal(){
   buttonFlag = 1;
}


void loop() {
//*********Encoder Operation**********************
aState = digitalRead(outputA); // Reads the "current" state of the outputA
   // If the previous and the current state of the outputA are different, that means a Pulse has occured
   if (aState != aLastState){     
     // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
     if (digitalRead(outputB) != aState) { 
       counter ++;
     } else {
       counter --;
     }
     Serial.print("Position: ");
     Serial.println(counter);
   } 
   aLastState = aState; // Updates the previous state of the outputA with the current state
 
  
  

 
  

annealTime = counter;  //link the setting with that value


  //***************** Display operation*******************
  display.clearDisplay();
  display.setTextSize(1.5);
  display.setTextColor(WHITE);
  display.setCursor(20,0);
  display.print("Time Set: ");
  display.print(annealTime,1);// add ",1" to print number to single decimal
  display.print("s");
  display.display();
if (buttonFlag == 1){
  startTime = millis();
    
  while(timeCount>0){
    timeCount = annealTime - ((millis()-startTime)/1000);
    display.setTextSize(2.5);
    display.setCursor(20,15);
    display.print(timeCount,1);
    display.print("s");
    display.display();
    digitalWrite(LED, HIGH);  
    digitalWrite(coil, HIGH);// send 5v to the solid state relay
    digitalWrite(lamp, HIGH);
      
  }

 display.clearDisplay();
 display.setTextSize(3);
 display.setTextColor(WHITE);
 display.setCursor(20,10);
 display.print("DONE!");  
 display.display();
 digitalWrite(LED, LOW); //Stop annealing
 digitalWrite(coil, LOW); 
 digitalWrite(lamp, LOW); 
 servo.write(0); //drop the case out of the annealer
 delay(1000);//give the case time to drop
 servo.write(100);//return servo (trap door) to start position
 timeCount = annealTime; 
 buttonFlag = 0;}
  

}