const int mot_karetka_G=9; //Blue LED on Pin 9
const int rele_kluv_R=10; //Green LED on Pin 10
const int mot_klin_Y=11; //Red LED on Pin 11
const int but=2; //The but is connected to pin 2
const int but1=4; //The but is connected to pin 4
const int but2=3; //The but is connected to pin 3
const int but3=5; //The but is connected to pin 5
const int but4=6; //The but is connected to pin 6
boolean lastbut = LOW; //Last but State
boolean currentbut = LOW; //Current but State
int Activ_G_knopka=1;
boolean lastbut1 = LOW; //Last but State
boolean currentbut1 = LOW; //Current but State
int Activ_Y_knopka=0;
boolean lastbut2 = LOW; //Last but State
boolean currentbut2 = LOW; //Current but State
int Activ_Blue_koncevik=0;
boolean lastbut3 = LOW; //Last but State
boolean currentbut3 = LOW; //Current but State
int Activ_W_koncevik=0;
boolean lastbut4 = LOW; //Last but State
boolean currentbut4 = LOW; //Current but State
int Activ_Gray_koncevik=0;
///deleys init
int del_open=200;
int del_close=200;
int del_mov_mot=1000;
//step 1
int delay_karetka_G_step1=1000; //каретка с клювом едет вперед перед остановкой после нажатия кнопки Green
//step 2
int delay_mot_klin_Y_Move_Down=400; //опускание клина после нажатия кнопки yellow (Y мотор)
int deley_rele_kluv_R_open=del_open; //время открытия клюва (R)
int delay_karetka_G_step2=del_mov_mot; // время работы мотора коретки при уезде назад (G мотор)
int deley_karetka_G_step2_stop=200; //ждем полной остановки мотора (G мотор)
int deley_rele_kluv_R_close=del_close; //время закрытия клюва клюва (R)
int delay_mot_klin_Y_up_step2=del_mov_mot; //время работы мотора при отходе клина вверх (Y мотор)
void setup()
{
pinMode (mot_karetka_G, OUTPUT); //Set Blue LED as Output
pinMode (rele_kluv_R, OUTPUT); //Set Green LED as Output
pinMode (mot_klin_Y, OUTPUT); //Set Red LED as Output
pinMode (but, INPUT); //Set but as input (not required)
pinMode (but1, INPUT); //Set but as input (not required)
pinMode (but2, INPUT); //Set but as input (not required)
pinMode (but3, INPUT); //Set but as input (not required)
}
boolean debounce(boolean last)
{
boolean current = digitalRead(but); //Read the but state
if (last != current) //if it's different...
{
delay(5); //wait 5ms
current = digitalRead(but); //read it again
}
return current; //return the current value
}
boolean debounce1(boolean last1)
{
boolean current1 = digitalRead(but1); //Read the but state
if (last1 != current1) //if it's different...
{
delay(5); //wait 5ms
current1 = digitalRead(but1); //read it again
}
return current1; //return the current value
}
boolean debounce2(boolean last2)
{
boolean current2 = digitalRead(but2); //Read the but state
if (last2 != current2) //if it's different...
{
delay(5); //wait 5ms
current2 = digitalRead(but2); //read it again
}
return current2; //return the current value
}
boolean debounce3(boolean last3)
{
boolean current3 = digitalRead(but3); //Read the but state
if (last3 != current3) //if it's different...
{
delay(5); //wait 5ms
current3 = digitalRead(but3); //read it again
}
return current3; //return the current value
}
boolean debounce4(boolean last4)
{
boolean current4 = digitalRead(but4); //Read the but state
if (last4 != current4) //if it's different...
{
delay(5); //wait 5ms
current4 = digitalRead(but4); //read it again
}
return current4; //return the current value
}
void loop()
{
// currentbut = debounce(lastbut); //read debounced state
//Activ_G_knopka=1;
if (lastbut == LOW && currentbut == HIGH && Activ_G_knopka == 1 ) //if it was pressed...
//////////step 1
///зеленая кнопка
{
Activ_Blue_koncevik=1;
Activ_G_knopka=0;
digitalWrite(mot_karetka_G, HIGH);
//delay(delay_karetka_G_step1);
//digitalWrite(mot_karetka_G, LOW);
}
currentbut = debounce(lastbut);
///концевик 1 (синий)
if (lastbut2 == LOW && currentbut2 == HIGH && Activ_Blue_koncevik == 1 ) //если нажата и текущий мод
{
Activ_Blue_koncevik=0;
Activ_Y_knopka=1;
digitalWrite(mot_karetka_G, LOW);
}
currentbut2 = debounce2(lastbut2);
////нажимаем желтую кнопку (клин вниз)
if (lastbut1 == LOW && currentbut1 == HIGH && Activ_Y_knopka == 1) //if it was pressed...
{
////////step 2
Activ_Y_knopka=0;
Activ_Gray_koncevik=1;
//Activ_W_koncevik=1;
Activ_G_knopka=0;
///Клин едет вниз
digitalWrite(mot_klin_Y, HIGH);
//delay(delay_mot_klin_Y_Move_Down);
//delay(500);
//digitalWrite(mot_klin_Y, LOW);
///клин остановлен
/*
///Открываем зажим проволоки (клюв)
digitalWrite(rele_kluv_R, HIGH);
//delay(deley_rele_kluv_R_open); //время открытия клюва
///клюв открыт запускаем с открытым клювом каретку назад (G мотор)
digitalWrite(mot_karetka_G, HIGH);
//delay(delay_karetka_G_step2); // время работы мотора коретки при уезде назад (G мотор)
delay(650);
digitalWrite(mot_karetka_G, LOW);
//delay(deley_karetka_G_step2_stop); //ждем полной остановки мотора (G мотор)
///каретка остановленна (G мотор)
///закрываем клюв
digitalWrite(rele_kluv_R, LOW);
//delay(deley_rele_kluv_R_close);
///клюв закрыт
///запускаем мотор клина вверх
digitalWrite(mot_klin_Y, HIGH);
//delay(delay_mot_klin_Y_up_step2);
delay(600);
*/
}
currentbut1 = debounce1(lastbut1);
///концевик клюва нижнее положение (серый)
if (lastbut4 == LOW && currentbut4 == HIGH && Activ_Gray_koncevik == 1 )
{
Activ_W_koncevik=1;
Activ_G_knopka=1;
Activ_Gray_koncevik=0;
digitalWrite(mot_klin_Y, LOW);
digitalWrite(rele_kluv_R, HIGH);
digitalWrite(mot_karetka_G, HIGH);
delay(650);
digitalWrite(mot_karetka_G, LOW);
digitalWrite(rele_kluv_R, LOW);
digitalWrite(mot_klin_Y, HIGH);
}
currentbut4 = debounce4(lastbut4);
///концевик клюва верхнее положение (белый)
if (lastbut3 == LOW && currentbut3 == HIGH && Activ_W_koncevik== 1 )
{
Activ_W_koncevik=0;
Activ_G_knopka=1;
digitalWrite(mot_klin_Y, LOW);
}
currentbut3 = debounce3(lastbut3);
}