Advertisement
Alx09

asd

Nov 19th, 2021
1,116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.29 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2.  
  3. #define BUTTON_OK 6
  4. #define BUTTON_CANCEL 7
  5. #define BUTTON_PREV 8
  6. #define BUTTON_NEXT 9
  7.  
  8. enum Buttons {
  9.   EV_OK,
  10.   EV_CANCEL,
  11.   EV_NEXT,
  12.   EV_PREV,
  13.   EV_NONE,
  14.   EV_MAX_NUM
  15. };
  16.  
  17. enum Menus {
  18.   MENU_MAIN = 0,
  19.   MENU_START,
  20.   MENU_KP,
  21.   MENU_KI,
  22.   MENU_KD,
  23.   MENU_TEMP,
  24.   MENU_TINCAL,
  25.   MENU_TMEN,
  26.   MENU_TRAC,
  27.   MENU_MAX_NUM
  28. };
  29. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  30. unsigned int numar_CAN;
  31. int voltageValue, temperatureRead;
  32. unsigned int temp;
  33. int tincal=5;
  34. double tset=50;
  35. int tmen=5;
  36. int trac=3;
  37. double moving_sp;
  38. double f;
  39. float temp_q = 0;
  40. Menus scroll_menu = MENU_MAIN;
  41. Menus current_menu =  MENU_MAIN;
  42.  
  43. double kp = 20, kpNemod;
  44. double ki = 0.01;
  45. double kd = 0.01; // exemplu valori
  46.  
  47. double afisare;
  48. double eroare= -30;
  49. int suma_erori= 0;
  50. double eroare_anterioara = 0;
  51. double derivativa = 0;
  52. double dt; // timp esantionare
  53. double output=0;
  54. int nr;
  55.  
  56. void state_machine(enum Menus menu, enum Buttons button);
  57. Buttons GetButtons(void);
  58. void print_menu(enum Menus menu);
  59.  
  60. typedef void (state_machine_handler_t)(void);
  61.  
  62. void print_menu(enum Menus menu)
  63. {
  64.   lcd.clear();
  65.   switch(menu)
  66.   {  case MENU_MAIN:
  67.         lcd.print("Meniu");
  68.         lcd.setCursor(0,1);
  69.       lcd.print("ALEGE program:");
  70.         break;
  71.     case MENU_START:
  72.          lcd.print("Meniu:");
  73.          lcd.setCursor(0,1);
  74.          lcd.print("START program:");
  75.          /*delay(3000);
  76.          lcd.clear();
  77.          lcd.print("Confirmare...");
  78.           delay(2000);
  79.           lcd.clear();
  80.        lcd.setCursor(0,0);
  81.        lcd.print("TI");
  82.        lcd.print(tincal);
  83.        lcd.print(" ");
  84.        lcd.print("TM");
  85.        lcd.print(tmen);
  86.        lcd.print(" ");
  87.        lcd.print("TR");
  88.        lcd.print(trac);
  89.        lcd.setCursor(0,1);
  90.        lcd.print("T:");
  91.        lcd.print(Afis(temperatureRead));
  92.        lcd.print(" ");
  93.        lcd.print("TS:");
  94.        //lcd.print();
  95.        //lcd.print(total_seconds);*/
  96.        break;
  97.     case MENU_KP:
  98.         lcd.print(kp);
  99.         lcd.setCursor(0,1);
  100.         lcd.print("Const KP");
  101.         break;
  102.     case MENU_KI:
  103.         lcd.print(ki);
  104.         lcd.setCursor(0,1);
  105.         lcd.print("Const KI");
  106.         break;
  107.     case MENU_KD:
  108.         lcd.print(kd);
  109.         lcd.setCursor(0,1);
  110.         lcd.print("Const KD");
  111.         break;
  112.     case MENU_TINCAL:
  113.         lcd.print(tincal);
  114.         lcd.setCursor(0,1);
  115.         lcd.print("Timp incalzire");
  116.         break;
  117.     case MENU_TMEN:
  118.         lcd.print(tmen);
  119.         lcd.setCursor(0,1);
  120.         lcd.print("Timp ment");
  121.         break;
  122.     case MENU_TRAC:
  123.         lcd.print(trac);
  124.         lcd.setCursor(0,1);
  125.         lcd.print("Timp racire");
  126.         break;
  127.     case MENU_TEMP:
  128.         lcd.print(Afis(temperatureRead));
  129.         lcd.setCursor(0,1);
  130.         lcd.print("TEMPERATURA");
  131.         break;
  132.     default:
  133.         lcd.print("Stare sistem:");
  134.         lcd.setCursor(0,1);
  135.         lcd.print(afisare);
  136.         break;
  137.   }
  138.   if(current_menu != MENU_MAIN)
  139.   {
  140.     lcd.setCursor(0,1);
  141.     lcd.print("ModificareParam");
  142.   }
  143. }
  144.  
  145. void enter_menu(void)
  146. {
  147.   current_menu = scroll_menu;
  148. }
  149.  
  150. void go_home(void)
  151. {
  152.   scroll_menu = MENU_MAIN;
  153.   current_menu = scroll_menu;
  154. }
  155.  
  156. void go_next(void)
  157. {
  158.   scroll_menu = (Menus) ((int)scroll_menu + 1);
  159.   scroll_menu = (Menus) ((int)scroll_menu % MENU_MAX_NUM);
  160. }
  161.  
  162. void go_prev(void)
  163. {
  164.   scroll_menu = (Menus) ((int)scroll_menu - 1);
  165.  /* if( scroll_menu == MENU_START)
  166.      scroll_menu =(Menus)((int)MENU_MAX_NUM - 1);*/
  167.   scroll_menu = (Menus) ((int)scroll_menu % MENU_MAX_NUM);
  168. }
  169.  
  170. void inc_kp(void)
  171. {
  172.  ++kp;
  173. }
  174.  
  175. void dec_kp(void)
  176. {
  177.   --kp;
  178. }
  179. void inc_ki(void)
  180. {
  181.   ki++;
  182. }
  183.  
  184. void dec_ki(void)
  185. {
  186.   ki--;
  187. }
  188. void inc_kd(void)
  189. {
  190.   kd++;
  191. }
  192.  
  193. void dec_kd(void)
  194. {
  195.   kd--;
  196. }
  197. void cancel_KP(void)
  198. {
  199.   kp = kpNemod;
  200.   scroll_menu = MENU_KP;
  201.   current_menu =  MENU_MAIN;
  202. }
  203. void cancel_KI(void)
  204. {
  205.  
  206. }
  207. void cancel_KD(void)
  208. {
  209.  
  210. }
  211.  
  212. void cancel_Temp(void)
  213. {
  214.  
  215. }
  216. void save_temp(void)
  217. {
  218. }
  219.  
  220. void inc_temp(void)
  221. {
  222.   temperatureRead++;
  223. }
  224.  
  225. void dec_temp(void)
  226. {
  227.   temperatureRead--;
  228. }
  229. void cancel_tIncal(void)
  230. {
  231.  
  232. }
  233. void cancel_tMen(void)
  234. {
  235.  
  236. }
  237. void cancel_tRac(void)
  238. {
  239.  
  240. }
  241. void inc_tIncal(void)
  242. {  tincal++;
  243. }
  244. void inc_tMen(void)
  245. { tmen++;
  246. }
  247. void inc_tRac(void)
  248. { trac++;
  249. }
  250. void dec_tIncal(void)
  251. { tincal--;
  252. }
  253. void dec_tMen(void)
  254. { tmen--;
  255. }
  256. void dec_tRac(void)
  257. { trac--;
  258. }
  259. void save(void)
  260. {
  261.  
  262. }
  263. void confirmare(void)
  264. {
  265.  
  266. }
  267. void save_kp(){
  268.    scroll_menu = MENU_KP;
  269.    current_menu =  MENU_MAIN;
  270. }
  271. state_machine_handler_t* sm[MENU_MAX_NUM][EV_MAX_NUM] =
  272. { //events: OK , CANCEL , NEXT, PREV
  273.   {enter_menu, go_home, go_next, go_prev},        // MENU_MAIN
  274.   {confirmare, go_home, go_next ,go_prev},        // MENU_START
  275.   {save_kp, cancel_KP, inc_kp, dec_kp},              // MENU_KP
  276.   {save, cancel_KI, inc_ki, dec_ki},              // MENU_Ki
  277.   {save, cancel_KD, inc_kd, dec_kd},              // MENU_Kd
  278.   {save, cancel_Temp, inc_temp, dec_temp},        // MENU_TEMP
  279.   {save, cancel_tIncal, inc_tIncal, dec_tIncal},  // MENU_TINCAL
  280.   {save, cancel_tMen, inc_tMen, dec_tMen},        // MENU_TMEN
  281.   {save, cancel_tRac, inc_tRac, dec_tRac}         // MENU_TRAC
  282. };
  283.  
  284. void state_machine(enum Menus menu, enum Buttons button)
  285. {
  286.   sm[menu][button]();
  287. }
  288.  
  289. Buttons GetButtons(void)
  290. {
  291.   enum Buttons ret_val = EV_NONE;
  292.   if (digitalRead(BUTTON_OK))
  293.   {
  294.     switch(scroll_menu){
  295.      case MENU_KP: kpNemod = kp; break;
  296.      case MENU_KI:   break;
  297.     }
  298.     ret_val = EV_OK;
  299.   }
  300.   else if (digitalRead(BUTTON_CANCEL))
  301.   {
  302.     ret_val = EV_CANCEL;
  303.   }
  304.   else if (digitalRead(BUTTON_NEXT))
  305.   {
  306.     ret_val = EV_NEXT;
  307.   }
  308.   else if (digitalRead(BUTTON_PREV))
  309.   {
  310.     ret_val = EV_PREV;
  311.   }
  312.  // Serial.print(ret_val);
  313.   return ret_val;
  314. }
  315.  
  316. void setup()
  317. {
  318.   Serial.begin(9600);
  319.   lcd.begin(16,2);
  320.   pinMode(6, INPUT);
  321.   digitalWrite(6, LOW); // pull-down
  322.     pinMode(7, INPUT);
  323.   digitalWrite(7, LOW); // pull-down
  324.     pinMode(8, INPUT);
  325.   digitalWrite(8, LOW); // pull-down
  326.     pinMode(9, INPUT);
  327.   digitalWrite(9, LOW); // pull-down
  328.    adc_init();
  329. }
  330. void Print0(int nr){
  331.  if(nr < 10 && nr > 0) lcd.print('0');
  332. }
  333. int Afis(float temperatureRead){
  334.    
  335.  
  336.      numar_CAN=read_adc(4);
  337.      temp=read_adc(5);
  338.      voltageValue=(temp*5000.0)/1024.0;
  339.      temperatureRead=(voltageValue-500)/10;
  340.     return temperatureRead;
  341.    
  342. }
  343.   void Timp(void)
  344. { double now=millis();
  345.    int total_seconds=(now/1000);
  346.  
  347.  if(total_seconds<=tincal)
  348.  {
  349.    Serial.print("INC:");
  350.     Serial.println(total_seconds);
  351.    moving_sp=tset;
  352.  }
  353.  else if(total_seconds<=(tincal+tmen))
  354.  {
  355.     Serial.print("Men:");
  356.     Serial.println(total_seconds);
  357.     moving_sp=temperatureRead+(tset-temperatureRead)*total_seconds/tincal;
  358.     Serial.print("SP:");
  359.     Serial.println(moving_sp);
  360.  }
  361.  else if(total_seconds<=(tincal+tmen+trac))
  362.  {
  363.     Serial.print("Rac:");
  364.     Serial.println(total_seconds);
  365.     moving_sp=temperatureRead+(tset-temperatureRead)-(tset-temperatureRead)*total_seconds/(tincal+tmen+trac);
  366.  }
  367.  
  368.  else
  369.  {
  370.     Serial.print("Oprit");
  371.     Serial.println(total_seconds);
  372.  }
  373.    lcd.print(total_seconds);
  374.  }  
  375. void adc_init() //adc initialization
  376. {
  377.   //set division factor between system clock frequency and the input clock to the ADC- 128
  378.   ADCSRA |= ((1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0));
  379.   ADMUX|=(1<<REFS0); //AVcc with external capacitor at Aref pin
  380.   ADCSRA|=(1<<ADEN); //enable ADC
  381.   ADCSRA|=(1<<ADSC); //ADC start conversion
  382. }
  383.  
  384. uint16_t read_adc(uint8_t channel)
  385. {
  386.   ADMUX&=0xF0; //set input AO to A5
  387.   ADMUX|=channel; //select chanel AO to A5
  388.   ADCSRA|=(1<<ADSC); //start conversion
  389.   while(ADCSRA & (1<<ADSC)); //wait while adc conversion are not updated
  390.   return ADCW; //read and return voltage
  391. }
  392. int PID(void)
  393. { eroare = tset- temperatureRead;//tR=movingsp
  394.   suma_erori= suma_erori + eroare * dt;
  395.   derivativa = (eroare - eroare_anterioara)/ dt;
  396.   output = (kp * eroare) + (ki * suma_erori )+ (kd * derivativa);
  397.   eroare_anterioara = eroare;
  398.  /*if(output>255)
  399.  {
  400.    output=255;
  401.  }
  402.  else if (output<0)
  403.  {
  404.    output=0;
  405.  }*/
  406. }
  407. void loop()
  408. {
  409.   Timp();
  410.   PID();
  411.   moving_sp=temperatureRead;
  412.   volatile Buttons event = GetButtons();
  413.   if (event != EV_NONE)
  414.   {
  415.     state_machine(current_menu, event);
  416.   }
  417.     print_menu(scroll_menu);
  418.     delay(1000);
  419.   analogWrite(10,255-output);
  420. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement