#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>
hd44780_I2Cexp lcd;
#define potPin A3
#define rightButton 8
#define leftButton 9
#define centerButton 13
#define spindleStep 3
#define spindleDir 2
#define xStep 5
#define xDir 4
#define lSwitch 7
#define rSwitch 6
bool rarmed = true;
bool larmed = true;
float potVal;
bool wireSet = false;
bool turnSet = false;
bool limitSet = false;
bool ready = false;
bool go = false;
int turnNum = 0;
int speed = 750;
float wireThickness;
float turns;
void setup() {
lcd.begin(16, 2);
pinMode(rightButton, INPUT);
pinMode(leftButton, INPUT);
pinMode(lSwitch, INPUT);
pinMode(rSwitch, INPUT);
pinMode(spindleStep, OUTPUT);
pinMode(spindleDir, OUTPUT);
pinMode(xStep, OUTPUT);
pinMode(xDir, OUTPUT);
}
void loop() {
while (wireSet == false) {
ready = false;
wireSetting();
}
while (turnSet == false) {
ready = false;
turnSetting();
}
while (limitSet == false) {
limitSetting();
}
while (ready == false) {
readyScreen();
}
while (go == true) {
winding();
}
}
float mapf(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void wireSetting() {
lcd.setCursor(0, 0);
lcd.print("Wire thickness:");
lcd.setCursor(0, 1);
lcd.print("(mm)");
potVal = analogRead(potPin);
float wireDisplay = mapf(potVal, 0.000, 1023.000, 0.01, 2.50);
lcd.setCursor(6, 1);
lcd.print(wireDisplay);
if (digitalRead(centerButton) == HIGH) {
wireThickness = wireDisplay;
wireSet = true;
lcd.clear();
delay(1000);
return;
}
}
void turnSetting() {
lcd.setCursor(0, 0);
lcd.print("# of turns:");
potVal = analogRead(potPin);
int turnDisplay = map(potVal, 0.000, 1023.000, 0, 999);
lcd.setCursor(6, 1);
lcd.print(turnDisplay);
if (digitalRead(centerButton) == HIGH) {
turns = turnDisplay;
turnSet = true;
lcd.clear();
delay(1000);
return;
}
}
void limitSetting() {
lcd.setCursor(0, 0);
lcd.print("left done right");
lcd.setCursor(0, 1);
lcd.print("V V V");
if (digitalRead(rightButton) == HIGH) {
if (rarmed == true) {
rarmed = false;
digitalWrite(xDir, LOW);
larmed = true;
while (digitalRead(rSwitch) == LOW) {
digitalWrite(xStep, HIGH);
delayMicroseconds(speed);
digitalWrite(xStep, LOW);
delayMicroseconds(speed);
}
}
}
if (digitalRead(leftButton) == HIGH) {
if (larmed == true) {
larmed = false;
digitalWrite(xDir, HIGH);
rarmed = true;
while (digitalRead(lSwitch) == LOW) {
digitalWrite(xStep, HIGH);
delayMicroseconds(speed);
digitalWrite(xStep, LOW);
delayMicroseconds(speed);
}
}
}
if (digitalRead(centerButton) == HIGH) {
limitSet = true;
lcd.clear();
digitalWrite(xDir, HIGH);
while (digitalRead(lSwitch) == LOW) {
digitalWrite(xStep, HIGH);
delayMicroseconds(speed);
digitalWrite(xStep, LOW);
delayMicroseconds(speed);
}
delay(1000);
return;
}
}
void readyScreen() {
lcd.setCursor(0, 0);
lcd.print("wire: turns: go");
lcd.setCursor(0, 1);
lcd.print(wireThickness);
lcd.setCursor(6, 1);
lcd.print(turns);
lcd.setCursor(15, 1);
lcd.print("V");
if (digitalRead(leftButton) == HIGH) {
wireSet = false;
ready = true;
lcd.clear();
delay(1000);
return;
}
if (digitalRead(centerButton) == HIGH) {
turnSet = false;
ready = true;
lcd.clear();
delay(1000);
return;
}
if (digitalRead(rightButton) == HIGH) {
ready = true;
go = true;
lcd.clear();
delay(1000);
return;
}
}
void winding() {
lcd.setCursor(0, 0);
lcd.print("Winding...");
float xMove = wireThickness / .04;
digitalWrite(spindleDir, HIGH);
for (int i = 0; i <= 200; i++) {
digitalWrite(spindleStep, HIGH);
delayMicroseconds(speed);
digitalWrite(spindleStep, LOW);
delayMicroseconds(speed);
}
if (digitalRead(lSwitch) == HIGH) {
digitalWrite(xDir, LOW);
}
if (digitalRead(rSwitch) == HIGH) {
digitalWrite(xDir, HIGH);
}
for (int j = 0; j <= xMove; j++) {
digitalWrite(xStep, HIGH);
delayMicroseconds(speed);
digitalWrite(xStep, LOW);
delayMicroseconds(speed);
}
turnNum = turnNum + 1;
int i = 0;
int j = 0;
if (turnNum >= turns) {
while (digitalRead(lSwitch) == LOW) {
digitalWrite(xStep, HIGH);
delayMicroseconds(speed);
digitalWrite(xStep, LOW);
delayMicroseconds(speed);
}
go = false;
ready = false;
turnNum = 0;
return;
}
}