Toaster_interrupt.ino 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <LiquidCrystal.h>
  2. #include <PWM.h>
  3. #include <DallasTemperature.h>
  4. #include <OneWire.h>
  5. LiquidCrystal lcd(13, 12, 10, 9, 8, 7);
  6. #define PIN_OUTPUT 3
  7. #define LED 5
  8. volatile int Stop = 2;
  9. int tempsensor = 6;
  10. int Magnet = 11;
  11. int32_t frequency = 10;
  12. int Grades;
  13. double temperature;
  14. double Tmax = 30;
  15. int S;
  16. unsigned long t = 0;
  17. unsigned long tmax = 60000;
  18. int D;
  19. void Drehschalter();
  20. void LCD();
  21. void Abbrechen();
  22. OneWire onewirepin(tempsensor);
  23. DallasTemperature sensors(&onewirepin);
  24. void setup() {
  25. lcd.begin(16, 2);
  26. InitTimersSafe();
  27. bool success = SetPinFrequencySafe(PIN_OUTPUT,frequency);
  28. sensors.begin();
  29. pinMode(Magnet, OUTPUT);
  30. pinMode(Stop, INPUT);
  31. attachInterrupt(digitalPinToInterrupt(Stop), Abbrechen, CHANGE);
  32. }
  33. void loop() {
  34. while (t < tmax) {
  35. t = millis();
  36. digitalWrite(Magnet, HIGH);
  37. Drehschalter();
  38. sensors.requestTemperatures();
  39. temperature=sensors.getTempCByIndex(0);
  40. D = map(analogRead(1), 0, 1023, 0, 255);
  41. pwmWrite(PIN_OUTPUT, D);
  42. analogWrite(LED, D);
  43. LCD();
  44. // S = digitalRead(2);
  45. // if (S == HIGH) {
  46. // t=tmax;
  47. // }
  48. if (temperature > Tmax) {
  49. digitalWrite(Magnet, LOW);
  50. t = tmax;
  51. }
  52. }
  53. digitalWrite(Magnet, LOW);
  54. }
  55. void Drehschalter() {
  56. Grades = analogRead(0);
  57. if (Grades >= 0 && Grades <= 205) {
  58. tmax = 10000;
  59. }
  60. if (Grades > 205 && Grades <= 410) {
  61. tmax = 20000;
  62. }
  63. if (Grades > 410 && Grades <= 615) {
  64. tmax = 30000;
  65. }
  66. if (Grades > 615 && Grades <= 820) {
  67. tmax = 40000;
  68. }
  69. if (Grades > 820 && Grades <= 1024) {
  70. tmax = 50000;
  71. }
  72. }
  73. void LCD() {
  74. lcd.setCursor(0, 0);
  75. lcd.print("T=");
  76. lcd.print(temperature);
  77. lcd.print("`C ");
  78. lcd.print("t=");
  79. lcd.print(t/1000);
  80. lcd.print("s");
  81. lcd.setCursor(0, 1);
  82. lcd.print("D=");
  83. lcd.print((D*100)/255);
  84. lcd.print("% ");
  85. lcd.print("tmax=");
  86. lcd.print(tmax/1000);
  87. lcd.print("s ");
  88. }
  89. void Abbrechen() {
  90. digitalWrite(Magnet, LOW);
  91. t = tmax;
  92. }