Can anyone elaborate me this code

/*

*/

#include <EEPROM.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

#define bt_set A0
#define bt_up A1
#define bt_down A2
#define bt_back A3

#define direction_pin 8
#define step_pin 9
#define enable_pin 10

long Step;
int Set = 0, Mode = 0;
int Run = 1, timer = 0;
int delayTime = 70, spd = 0, ang = 0, breath = 0;

int flag1 = 0, flag2 = 1;

void setup() {

pinMode(bt_up, INPUT_PULLUP);
pinMode(bt_set, INPUT_PULLUP);
pinMode(bt_down, INPUT_PULLUP);
pinMode(bt_back, INPUT_PULLUP);

pinMode(direction_pin, OUTPUT);
pinMode(step_pin, OUTPUT);
pinMode(enable_pin, OUTPUT);

lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print(" Welcome To ");
lcd.setCursor(0, 1);
lcd.print(" Ventilator ");
delay(2000); // Waiting for a while
lcd.clear();
if (EEPROM.read(50) == 0) {
} else {
write_eeprom();
}
EEPROM.write(50, 0);
read_eeprom();
Display();
Step = ang * 56;
}

void loop() {

if (digitalRead(bt_set) == 0) {
timer = timer + 1;
if (timer > 600) {
Run = 1; setting(); Run = 0; timer = 0; flag2 = 1;
}
} else {
timer = 0;
}

if (Run == 0) {
digitalWrite(direction_pin, flag2);

digitalWrite(step_pin, HIGH);
delayMicroseconds(delayTime);
digitalWrite(step_pin, LOW);
delayMicroseconds(delayTime * spd);

Step = Step - 1;
if (Step <= 0) {
  flag2 = !flag2;
  if (flag2 == 1) {
    delay(10 * breath);
  }
  Step = ang * 56;
}

}
if (digitalRead(bt_up) == 0) {
Run = 1;
flag2 = 1;
Step = ang * 56;
digitalWrite(enable_pin, HIGH);
} else {
digitalWrite(enable_pin, LOW);
}

if (digitalRead(bt_down) == 0) {
Run = 0;
}

}

void setting() {
Set = 0;
read_eeprom();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Welcome To ");
lcd.setCursor(0, 1);
lcd.print(" Setting Mode ");
delay(3000); // Waiting for a while
lcd.clear();
while (1) {

if (digitalRead(bt_set) == 0) {
  if (flag1 == 0) {
    flag1 = 1;
    lcd.clear();
    Set = Set + 1;
    if (Set > 2) {
      Set = 0;
    }
  }
} else {
  flag1 = 0;
}

if (digitalRead(bt_up) == 0) {
  if (Set == 0) {
    spd = 2;
  }
  if (Set == 1) {
    ang = ang + 1;
  }
  if (Set == 2) {
    breath = breath + 1;
  }
  if (ang > 140) {
    ang = 30;
  }
  if (breath > 99) {
    breath = 1;
  }
}

if (digitalRead(bt_down) == 0) {
  if (Set == 0) {
    spd = 1;
  }
  if (Set == 1) {
    ang = ang - 1;
  }
  if (Set == 2) {
    breath = breath - 1;
  }
  if (ang < 30) {
    ang = 140;
  }
  if (breath < 1) {
    breath = 99;
  }
}

if (digitalRead(bt_back) == 0) {
  write_eeprom();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(" Back to Save ");
  lcd.setCursor(0, 1);
  lcd.print("    Setting   ");
  delay(1000); // Waiting for a while
  lcd.clear();
  read_eeprom();
  Display();
  Step = ang * 56;
  delay(100);
  return;
}

if (Set == 0) {
  lcd.setCursor(4, 0);
  lcd.print("    ");
}
if (Set == 1) {
  lcd.setCursor(14, 0);
  lcd.print("   ");
}
if (Set == 2) {
  lcd.setCursor(8, 1);
  lcd.print("   ");
}

delay(100);
Display();
delay(100);

}
}

void Display() {
lcd.setCursor(0, 0);
lcd.print("Spd:");
if (spd == 2) {
lcd.print("Slow");
}
if (spd == 1) {
lcd.print("Fast");
}

lcd.setCursor(9, 0);
lcd.print("Ang:");
lcd.print((ang / 100) % 10);
lcd.print((ang / 10) % 10);
lcd.print(ang % 10);

lcd.setCursor(0, 1); //sets the cursor at row 1 column 2
lcd.print("Breath: ");
lcd.print((breath / 10) % 10);
lcd.print(".");
lcd.print(breath % 10);
lcd.print("sec ");
}

void write_eeprom() {
EEPROM.write(11, spd);
EEPROM.write(12, ang);
EEPROM.write(13, breath);
}

void read_eeprom() {
spd = EEPROM.read(11);
ang = EEPROM.read(12);
breath = EEPROM.read(13);
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

1 Like

actually, I have this code in running format but I cant understand because I dont write.
If you can understand what is happening in this code you can tell.
It will be helpfull for me brother

Start with the first line of code you don't understand.

Where did you get the code from?
What does it do?
What board are you using?

Please edit your post to add code tags ("</>" editor button).