Advertisement
TolentinoCotesta

Saldatore_PWM_PID

May 4th, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. #include <PID_v1.h>
  2. #include <LiquidCrystal.h>
  3.  
  4. #define  PTC_PIN  A0
  5. #define  POT_PIN  A1
  6. #define  LCD_ROW  2
  7. #define  LCD_COL  16
  8. #define  OUT_PIN  6
  9.  
  10. // <+5V> --- <R1 150 Ohm> --- <PTC> --- <GND>
  11. //                         |  
  12. //                     Analog Vin
  13. // 35.0°C  In 275
  14. // 36.7°C  In 278
  15. // 50.2°C  In 297
  16. // 48.7°C  In 278
  17. // 95.0°C  In 360
  18. // 36.7°C  In 278
  19.  
  20.  
  21.  
  22. //Define Variables we'll be connecting to
  23. double Setpoint, Input, Output;
  24. int OutBar = 0;
  25.  
  26. //Specify the links and initial tuning parameters
  27. double Kp=1, Ki=0.1, Kd=0.5;
  28. PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
  29.  
  30. // initialize the library with the numbers of the interface pins
  31. LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
  32. byte barElement[8] = {
  33.   B00000,
  34.   B00000,
  35.   B10101,
  36.   B10101,
  37.   B10101,
  38.   B10101,
  39.   B10101,
  40.   B10101,
  41. };
  42.  
  43. //int sensorValue, setPoint = 0;
  44.  
  45. void setup() {  
  46.   Serial.begin(9600);
  47.   //turn the PID on
  48.   myPID.SetMode(AUTOMATIC);
  49.  
  50.   analogReference(INTERNAL);
  51.   lcd.createChar(0, barElement);
  52.    // set up the LCD's number of columns and rows:
  53.   lcd.begin(LCD_COL, LCD_ROW);
  54.   // Print a message to the LCD.
  55.   lcd.print("Hello World!");
  56.  
  57.   delay(500);
  58.  
  59.   // set up Timer 4 for PWM 38095 Khz  (ATmega32U4)
  60.   /*
  61.   TCCR4A = ((1<<COM4A0) | (1<<WGM40));
  62.   TCCR4B = (1<<CS40);
  63.   OCR4A = 105;
  64.   */
  65. }
  66.  
  67. void loop() {  
  68.  
  69.   // read the value from the sensor:
  70.   Input = analogRead(PTC_PIN);
  71.   Setpoint = map(analogRead(POT_PIN), 0, 1024, 0, 500);
  72.  
  73.   myPID.Compute();
  74.   analogWrite(OUT_PIN, Output);
  75.  
  76.   // Print setpoint value to LCD
  77.   clearRow(0);
  78.   lcd.setCursor(0, 0);
  79.   lcd.print("In; ");
  80.   lcd.print((int)Input);
  81.   lcd.print(" Set");
  82.   lcd.print((int)Setpoint);  
  83.  
  84.   // Print out status value to LCD
  85.   clearRow(1);
  86.   lcd.setCursor(0, 1);
  87.   OutBar = map(Output, 0, 255, 0, 16);
  88.   for(int bar = 0; bar <= OutBar-1; bar++){
  89.      lcd.write(byte(0));
  90.   }
  91.      
  92.   Serial.println(OutBar);
  93.   delay(200);
  94.  
  95. }
  96.  
  97.  
  98. void clearRow(int row){
  99.    lcd.setCursor(0, row);
  100.    for(int Column = 0; Column <LCD_COL; Column++)
  101.      lcd.print(" ");
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement