safwan092

test code for faults

Apr 11th, 2021
584
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal.h>
  2. LiquidCrystal lcd(2, 4, 5, 6, 7, 8);
  3.  
  4.  
  5. #define ADJUST_PIN A0
  6. #define BUCK_PIN 3
  7. #define BOOST_PIN 11
  8. #define VIN 12.0
  9. #define BOOST_EN 13
  10. #define BUCK_EN 12
  11. #define VOUT_PIN A1
  12.  
  13. int hystrsis = 5;
  14.  
  15. void setup() {
  16.   pinMode(BUCK_PIN, OUTPUT);
  17.   pinMode(BOOST_PIN, OUTPUT);
  18.   pinMode(BOOST_EN, OUTPUT);
  19.   pinMode(BUCK_EN, OUTPUT);
  20.   TCCR2B = TCCR2B & B11111000 | B00000010; // for PWM frequency of 3921.16 Hz
  21.   Serial.begin(9600);
  22.   lcd.begin(16, 2);
  23.  
  24. }
  25.  
  26. void loop() {
  27.   float Vout = volt_adjust();
  28.   if (Vout >= 12.0) {
  29.     boost_regulating(Vout);
  30.   }
  31.   else if (Vout < 12.0) {
  32.     buck_regulating(Vout);
  33.   }
  34. }
  35.  
  36. void boost_regulating(float Vout) {
  37.   digitalWrite(BUCK_EN, HIGH);
  38.   digitalWrite(BOOST_EN, LOW);
  39.   float Vout_feedback = (analogRead(VOUT_PIN) * 120.0) / 1023.0; //calculate v feedback
  40.   float duty = 1.0 - (VIN / Vout);
  41.   float analog_output = duty * 255.0;
  42.   analog_output = constrain(analog_output, 0, 255);
  43.   analogWrite(BOOST_PIN, analog_output);
  44.  
  45.   while ((Vout - Vout_feedback) > (Vout * 0.15)) {
  46.     analog_output++;
  47.     analog_output = constrain(analog_output, 0, 255);
  48.     analogWrite(BOOST_PIN, analog_output);
  49.     Vout_feedback = (analogRead(VOUT_PIN) * 120.0) / 1023.0;
  50.     Serial.println(Vout_feedback);
  51.   }
  52.   while ((Vout_feedback - Vout) > (Vout * 0.15)) {
  53.     analog_output--;
  54.     analog_output = constrain(analog_output, 0, 255);
  55.     analogWrite(BOOST_PIN, analog_output);
  56.     Vout_feedback = (analogRead(VOUT_PIN) * 120.0) / 1023.0;
  57.     Serial.println(Vout_feedback);
  58.   }
  59. }
  60.  
  61. void buck_regulating(float Vout) {
  62.   digitalWrite(BOOST_EN, HIGH);
  63.   digitalWrite(BUCK_EN, LOW);
  64.   float Vout_feedback = (analogRead(VOUT_PIN) * 120.0) / 1023.0; //calculate v feedback
  65.   float duty = (Vout / VIN);
  66.   float analog_output = duty * 255.0;
  67.   analog_output = constrain(analog_output, 0, 255);
  68.   analogWrite(BUCK_PIN, analog_output);
  69.  
  70.   while ((Vout - Vout_feedback) > (Vout * 0.15)) {
  71.     analog_output++;
  72.     analog_output = constrain(analog_output, 0, 255);
  73.     analogWrite(BUCK_PIN, analog_output);
  74.     Vout_feedback = (analogRead(VOUT_PIN) * 120.0) / 1023.0;
  75.     Serial.println(analog_output);
  76.   }
  77.   while ((Vout_feedback - Vout) > (Vout * 0.15)) {
  78.     analog_output--;
  79.     analog_output = constrain(analog_output, 0, 255);
  80.     analogWrite(BUCK_PIN, analog_output);
  81.     Vout_feedback = (analogRead(VOUT_PIN) * 120.0) / 1023.0;
  82.     Serial.println(analog_output);
  83.   }
  84. }
  85. float volt_adjust() {
  86.   delay(50);
  87.   int pot_read = analogRead(ADJUST_PIN);
  88.   pot_read = map(pot_read, 0, 1023, 0, 120);
  89.   pot_read = constrain(pot_read, 0, 120);
  90.  
  91.   lcd.clear();
  92.   lcd.setCursor(2, 0);
  93.   lcd.print("OUTPUT VOLT");
  94.   lcd.setCursor(5, 1);
  95.   lcd.print(pot_read);
  96.   lcd.print(" V");
  97.  
  98.   return pot_read;
  99.   //return 6.0;
  100. }
RAW Paste Data