15/05/2019
unsigned long interval1 =40;
unsigned long interval2 = 100;
unsigned long previousMillis1;
unsigned long previousMillis2;
int pwm1=8;
int pwm2=9;
int pwm3=10;
int pwm4=11;
int sensor=38;
int n=1;
int auxiliar=0;
int paso1;
int start=12;
int pwmlcd=13;
const int stepsPerRevolution1 = 400;
const int stepsPerRevolution2 = 400;
byte a;
byte sube;
byte largo=0;
byte busca;
byte calculo=0;
float area;
float ladoa;
float ladob;
float voltprimario;
float voltsecundario;
float corrienteprim;
float corrientesec;
float corriente;
float anchocarre;
float dia;
float cal;
float diap;
float dias;
float vueltaspcap;
float vueltaspcas;
float relvueltas;
long cuentapanta=0;
long cuentapanta1=0;
char key;
char value[5];
char value1[5];
char value2[5];
char value3[5];
char value4[5];
char largotraf[5];
char anchotraf[5];
char voltpri[5];
char voltsec[5];
char anchobob[5];
double Setpoint, Input, Output;
double Kp=2, Ki=5, Kd=1;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0',' #','D'}
};
byte rowPins[ROWS] = {18, 19, 20, 21};
byte colPins[COLS] = {14, 15, 16, 17};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal lcd(7, 6, 2, 3, 4, 5);
Stepper myStepper1(stepsPerRevolution1, 30, 31, 32, 33);
Stepper myStepper2(stepsPerRevolution2, 34, 35, 36, 37);
void setup() {
Serial.begin(9600);
pinMode(start,INPUT);
pinMode(pwm1,OUTPUT);
pinMode(pwm2,OUTPUT);
pinMode(pwm3,OUTPUT);
pinMode(pwm4,OUTPUT);
pinMode(pwmlcd,OUTPUT);
pinMode(sensor,INPUT);
analogWrite(pwmlcd,130);
lcd.begin(16,2);
lcd.setCursor(0, 0);
lcd.print("Abt Machine");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("1=autotransformador");
lcd.setCursor(0,1);
lcd.print("2=transformador");
a=0;
sube=0;
Input = analogRead(A1);
Setpoint = 100;
myPID.SetMode(AUTOMATIC);
}
void loop() {
{analogWrite(pwmlcd,130);
key = keypad.getKey();
if (key){
Serial.println(key);
}
if (key=='A')
{delay(150);
++sube;
}
if (sube==255)
{sube=0;}
if (key=='B')
{ delay(150);
--sube;
}
if(sube==3)
{sube=2;}
menu();
}
}
void menu(){
if (sube==0)
{
lcd.setCursor(0,0);
lcd.print("1=autotransform");
if(key=='1')
{busca=1;
autotransform();}
lcd.setCursor(0,1);
lcd.print("2=transformador ");
if(key=='2')
{busca=2;
transformador();}
}
if (sube==1)
{
lcd.setCursor(0,0);
lcd.print("2=transformador ");
if(key=='2')
{busca=2;
transformador();}
lcd.setCursor(0,1);
lcd.print("3=bobina RF ");
if(key=='3')
{busca=3;
bobinaRF();}
}
if (sube==2)
{
lcd.setCursor(0,0);
lcd.print("3=bobina Rf ");
if(key=='3')
{busca=3;
bobinaRF();}
lcd.setCursor(0,1);
lcd.print("4=bobina motor ");
if(key=='4')
{busca=4;
bobinamotor();}
}
}
void autotransform(){
}
void transformador()
{ lcd.clear();
largo=0;
while(busca==2){
key = keypad.getKey();
if(key=='D')
{busca=0;
}
if(key==' #')
{busca=5;
}
lcd.setCursor(0,0);
lcd.print("Ingresa largo= ");
lcd.setCursor(0,1);
if ((key!='A') && (key!='B') && (key!='C')&& (key!='D') && (key!=' #'))
{ if (key)
{delay(150);
++largo;
numero();
if (key=='*')
{key='.';
largotraf[largo]= key;
} else
{largotraf[largo]= key; }
char value[5]={largotraf[1],largotraf[2],largotraf[3],largotraf[4],largotraf[5]};
Serial.println(atof(value));
ladoa=atof(value);
}
}
}
lcd.clear();
largo=0;
while(busca==5){
key = keypad.getKey();
if(key=='D')
{busca=0;
}
if(key==' #')
{busca=6;
}
lcd.setCursor(0,0);
lcd.print("Ingresa ancho= ");
lcd.setCursor(0,1);
if ((key!='A') && (key!='B') && (key!='C')&& (key!='D') && (key!=' #'))
{ if (key)
{delay(150);
++largo;
numero();
if (key=='*')
{key='.';
anchotraf[largo]= key;
} else
{anchotraf[largo]= key; }
char value1[5]={anchotraf[1],anchotraf[2],anchotraf[3],anchotraf[4],anchotraf[5]};
ladob=atof(value1);
area=ladoa*ladob;
}
}
}
lcd.clear();
largo=0;
while(busca==6){
key = keypad.getKey();
if(key=='D')
{busca=0;
}
if(key==' #')
{busca=7;
}
lcd.setCursor(0,0);
lcd.print("Ingresa volt Pri= ");
lcd.setCursor(0,1);
if ((key!='A') && (key!='B') && (key!='C')&& (key!='D') && (key!=' #'))
{ if (key)
{delay(150);
++largo;
numero();
if (key=='*')
{key='.';
voltpri[largo]= key;
} else
{voltpri[largo]= key; }
char value2[5]={voltpri[1],voltpri[2],voltpri[3],voltpri[4],voltpri[5]};
voltprimario=atof(value2);
}
}
}
lcd.clear();
largo=0;
while(busca==7){
key = keypad.getKey();
if(key=='D')
{busca=0;
}
if(key==' #')
{busca=8;
}
lcd.setCursor(0,0);
lcd.print("Ingresa volt Sec= ");
lcd.setCursor(0,1);
if ((key!='A') && (key!='B') && (key!='C')&& (key!='D') && (key!=' #'))
{ if (key)
{delay(150);
++largo;
numero();
if (key=='*')
{key='.';
voltsec[largo]= key;
} else
{voltsec[largo]= key; }
char value3[5]={voltsec[1],voltsec[2],voltsec[3],voltsec[4],voltsec[5]};
voltsecundario=atof(value3);
}
}
}
lcd.clear();
largo=0;
while(busca==8){
key = keypad.getKey();
if(key=='D')
{busca=0;
}
if(key==' #')
{busca=9;
}
lcd.setCursor(0,0);
lcd.print("Ingresa ancho bob= ");
lcd.setCursor(0,1);
if ((key!='A') && (key!='B') && (key!='C')&& (key!='D') && (key!=' #'))
{ if (key)
{delay(150);
++largo;
numero();
if (key=='*')
{key='.';
anchobob[largo]= key;
} else
{anchobob[largo]= key; }
char value4[5]={anchobob[1],anchobob[2],anchobob[3],anchobob[4],anchobob[5]};
anchocarre=atof(value4);
}
}
}
lcd.clear();
calculo=0;
while(busca==9)
{ if (digitalRead(start)==HIGH)
{delay(150);
busca=10; }
key = keypad.getKey();
if (key){
Serial.println(key);
}
if (key=='A')
{delay(150);
++calculo;
lcd.clear();
}
if(key=='D')
{busca=0;
}
if (calculo==255)
{calculo=0;}
if (key=='B')
{ delay(150);
--calculo;
lcd.clear();
}
if(calculo==5)
{calculo=4;}
if (calculo==0)
{
lcd.setCursor(0,0);
lcd.print("area=");
lcd.setCursor(5,0);
lcd.print(area);
lcd.setCursor(0,1);
lcd.print("Potencia=");
lcd.setCursor(9,1);
lcd.print(area*area);
relvueltas=area*0.02112;
}
if (calculo==1)
{
lcd.setCursor(0,0);
lcd.print("VPri=");
lcd.setCursor(5,0);
lcd.print(voltprimario/relvueltas);
lcd.setCursor(0,1);
lcd.print("VSec=");
lcd.setCursor(5,1);
lcd.print(voltsecundario/relvueltas);
}
if (calculo==2)
{
lcd.setCursor(0,0);
lcd.print("CoPri=");
lcd.setCursor(6,0);
corrienteprim=(area*area)/voltprimario;
lcd.print(corrienteprim);
lcd.setCursor(0,1);
lcd.print("CoSec=");
lcd.setCursor(6,1);
corrientesec=(area*area)/voltsecundario;
lcd.print(corrientesec);
}
if (calculo==3)
{
corriente=corrienteprim;
tablaawg();
lcd.setCursor(0,0);
lcd.print("DiaPri=");
lcd.setCursor(7,0);
diap=dia;
vueltaspcap=anchocarre/diap;
lcd.print(diap);
lcd.setCursor(0,1);
lcd.print("CalPri=");
lcd.setCursor(7,1);
lcd.print(cal);
}
if (calculo==4)
{corriente=corrientesec;
tablaawg();
lcd.setCursor(0,0);
lcd.print("DiaSec=");
lcd.setCursor(7,0);
dias=dia;
vueltaspcas=anchocarre/dias;
lcd.print(dias);
lcd.setCursor(0,1);
lcd.print("CalSec=");
lcd.setCursor(7,1);
lcd.print(cal);
}
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("VueltasPri=");
analogWrite(pwmlcd,130);
analogWrite(pwm1,90);
analogWrite(pwm2,90);
analogWrite(pwm3,90);
analogWrite(pwm4,90);
while (busca==10)
{
activamotoresprim();
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("VueltasSec=");
analogWrite(pwm1,90);
analogWrite(pwm2,90);
analogWrite(pwm3,90);
analogWrite(pwm4,90);
analogWrite(pwmlcd,130);
n=1;
while (busca==11)
{
activamotoressec();
}
}
void bobinaRF(){
}
void bobinamotor(){
}
void activapid()
{
Input = analogRead(A1);
myPID.Compute();
analogWrite(13, Output);
}
void numero(){
lcd.setCursor((largo-1),1);
if(key=='0')
{ delay(150);
lcd.print("0 ");}
if(key=='1')
{ delay(150);
lcd.print("1 ");}
if(key=='2')
{ delay(150);
lcd.print("2 ");}
if(key=='3')
{ delay(150);
lcd.print("3 ");}
if(key=='4')
{ delay(150);
lcd.print("4 ");}
if(key=='5')
{ delay(150);
lcd.print("5 ");}
if(key=='6')
{ delay(150);
lcd.print("6 ");}
if(key=='7')
{ delay(150);
lcd.print("7 ");}
if(key=='8')
{ delay(150);
lcd.print("8 ");}
if(key=='9')
{ delay(150);
lcd.print("9 ");}
if(key=='*')
{ delay(150);
lcd.print(". ");}
if(largo==6)
{largo=5;}
}
void activamotoresprim()
{
if (digitalRead(sensor)==HIGH)
a=1;
{if( (digitalRead(sensor)==LOW)&&(a==1))
{
++cuentapanta;
a=0;
}}
lcd.setCursor(11,0);
lcd.print(cuentapanta);
paso1=1;
if(cuentapanta( (0.1*(voltprimario/relvueltas)))&&(cuentapanta((0.9*(voltprimario/relvueltas))))&&(cuentapanta=(stepsPerRevolution2*diap)/8)
{paso1=0;}
if(cuentapanta>= (voltprimario/relvueltas))
{
myStepper1.setSpeed(0);
myStepper2.setSpeed(0);
analogWrite(pwm1,0);
analogWrite(pwm2,0);
analogWrite(pwm3,0);
analogWrite(pwm4,0);
if (digitalRead(start)==HIGH)
{delay(150);
lcd.clear();
busca=11; }
}
}
void activamotoressec()
{ if (digitalRead(sensor)==HIGH)
a=1;
{if( (digitalRead(sensor)==LOW)&&(a==1))
{
++cuentapanta1;
a=0;
}}
lcd.setCursor(11,0);
lcd.print(cuentapanta1);
paso1=1;
if(cuentapanta1( (0.1*(voltsecundario/relvueltas)))&&(cuentapanta1((0.9*(voltsecundario/relvueltas))))&&(cuentapanta1=(stepsPerRevolution2*diap)/8)
{paso1=0;}
if(cuentapanta1>= (voltsecundario/relvueltas))
{
myStepper1.setSpeed(0);
myStepper2.setSpeed(0);
analogWrite(pwm1,0);
analogWrite(pwm2,0);
analogWrite(pwm3,0);
analogWrite(pwm4,0); }
}
void tablaawg()
{if((corriente>=0.005)&&(corriente=0.007)&&(corriente=0.009)&&(corriente=0.011)&&(corriente=0.014)&&(corriente=0.017)&&(corriente=0.022)&&(corriente=0.028)&&(corriente=0.036)&&(corriente=0.045)&&(corriente=0.057)&&(corriente=0.072)&&(corriente=0.09)&&(corriente=0.11)&&(corriente=0.15)&&(corriente=0.18)&&(corriente=0.23)&&(corriente=0.29)&&(corriente=0.37)&&(corriente=0.46)&&(corriente=0.58)&&(corriente=0.73)&&(corriente=0.92)&&(corriente=1.2)&&(corriente=1.6)&&(corriente=2.0)&&(corriente=2.5)&&(corriente=3.2)&&(corriente=3.7)&&(corriente=4.8)&&(corriente=6.0)&&(corriente=7.5)&&(corriente=9.5)&&(corriente=12)&&(corriente=15)&&(corriente=19)&&(corriente=24)&&(corriente=30)&&(corriente=38)&&(corriente=48)&&(corriente=60)&&(corriente=78)&&(corriente=96)&&(corriente=120)&&(corriente=150)&&(corriente=190)&&(corriente=240)&&(corriente=319))
{dia=11.86;
cal=0000;}
}