#include<Adafruit_SSD1306.h>
#define BUTTON_PIN  4
#define SCREEN_WIDTH 128 
#define SCREEN_HEIGHT 64 
#define OLED_RESET     -1 
#define SCREEN_ADDRESS 0x3D 
float  rpm = 200;
float rowNum = 2;
float wrapNum = 2;
float delayVal = 300;
float var;
float pulse;
float pulse2;
float coilLength;
float wireThicknes = 0.203;
float x = 1.25 ;//pitch(distance between threads on bolt) of the wire guider bolt
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);

void setup(){

 

    pinMode(BUTTON_PIN, INPUT);
  Serial.begin(9600);
  Serial.println("OLED FeatherWing test");
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32
  Serial.println("OLED begun");
  display.display();
  delay(1000);
  display.clearDisplay();
  display.display();

  Serial.println("IO test");
  // text display tests
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0,0);
  display.print(" to SSID\n'adafruit':");
  
  display.setCursor(0,0);
  display.display(); // actually display all of the above
}

void wireWrap(){
    int y;
    pulse = map(var, 0, 255, 0, rpm);
    pulse2 = map((var/x), 0, 255, 0, rpm);
    bool nrow = true;


    for(float i = 0; i <= rowNum; i++){
        if(nrow == true){
            y = 11;
        }
        if(nrow == false){
            y = 13;
        }
        for(float j = 1; j<= wrapNum; j++){

            analogWrite(3,pulse);
            analogWrite(y,pulse2);
            delay(delayVal);
            analogWrite(3,0);
            analogWrite(y,0);
            display.setCursor(10,0);
            display.clearDisplay();

						float percentDone = map(((((i*rowNum)*wrapNum)+j)/(wrapNum*rowNum)),0,(wrapNum*rowNum),0,100);
            display.print(percentDone);
            display.print(" % Complete");

            display.display();
						delay(500);
        }
    } 
    display.setCursor(10,0);
	  display.clearDisplay();
		display.print("Complete");
		while(1);
}

void input(){
    float tempStat;
		Serial.println(digitalRead(BUTTON_PIN));
while (digitalRead(BUTTON_PIN) == LOW) {
        tempStat = map((analogRead(A0)), 0, 1023, 0, 152.4);
              display.setCursor(0,0);
            display.clearDisplay();
            display.print("Length in mm = ");
            display.print(tempStat);
            display.display();

         
    }
    coilLength = tempStat;
Serial.print("Length of Coil in mm = ");
Serial.println(tempStat);
delay(2000);
Serial.println(digitalRead(BUTTON_PIN));
while(digitalRead(BUTTON_PIN) == LOW){


        tempStat = map((analogRead(A0)), 0, 1023, 0, 50);
              display.setCursor(0,0);
            display.clearDisplay();
            display.print("Number of Rows = ");
            display.print(tempStat);
            display.display();

					
    }
    rowNum = tempStat;
		Serial.print("Number of Rows = ");
Serial.println(tempStat);
delay(2000);
Serial.println(digitalRead(BUTTON_PIN));
while(digitalRead(BUTTON_PIN) == LOW){
        tempStat = map((analogRead(A0)), 0, 1023, 0, 152.4);
              display.setCursor(0,0);
            display.clearDisplay();
            display.println("Number of Coils per");
						display.print("Row = ");
            display.print(tempStat);
            display.display();

    }
    wrapNum = tempStat;
		Serial.print("Number of Coils per Row = ");
Serial.println(tempStat);
delay(2000);
    display.setCursor(0, 0);
    display.clearDisplay();
    display.println("Length in mm = ");
    display.print(coilLength);
    display.println("Number of Rows = ");
    display.print(rowNum);
    display.println("Nuber of Coils per Row = ");
    display.print(wrapNum);
	
		Serial.println();
		Serial.println();
		Serial.println("Click to Confirm");
    Serial.print("Length in mm = ");
    Serial.println(coilLength);
    Serial.print("Number of Rows = ");
    Serial.println(rowNum);
    Serial.print("Nuber of Coils per Row = ");
    Serial.println(wrapNum);
		Serial.println("Click to Confirm");

display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); 
display.println("Click to Confirm");
display.setTextColor(SSD1306_WHITE, SSD1306_BLACK); 

    long T = 0;
   while(digitalRead(BUTTON_PIN) == LOW){
        delay(1);
        if(T>=1000){
		Serial.println("Click to Confirm");
		T = 0;
        }
				T++;

    }
		Serial.println("Confirmed");
		display.println("Confirmed");

		delay(500);
}

void loop(){
//input();
wireWrap();
display.setCursor(0, 0);
display.clearDisplay();
display.println("Solenoid Coil Completed");
Serial.println("Solenoid Coil Completed");
while(digitalRead(BUTTON_PIN) == LOW){
	delay(10);
}
display.println("Confirmed");
Serial.println("Confirmed");
delay(1000);
display.clearDisplay();
display.display();
exit(0);

}