Toaster_functions.ino 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /********************
  2. Toaster Project
  3. Rev 3
  4. Jian Song
  5. Functions:
  6. 1. Drehschalter
  7. 2. Auslösemagnet
  8. 3. Temperatursensor
  9. 4. Heizwendel
  10. 5. Stoptaste
  11. 6. LCD
  12. 7. Kontrollanzeige
  13. *********************/
  14. void Drehschalter() { //function for calibration of the heating time
  15. int Grades; //define the degrees of browning
  16. unsigned long tmax = 60000; //define the max. heating time
  17. Grades = analogRead(0); //read the data from pin A0 of analog input
  18. if (Grades >= 0 && Grades <= 205) { //input range for A0 is from 0 to 1024(because of 10 bit ADC), define potentiometer as 5 steps
  19. tmax = 10000;
  20. }
  21. if (Grades > 205 && Grades <= 410) {
  22. tmax = 20000;
  23. }
  24. if (Grades > 410 && Grades <= 615) {
  25. tmax = 30000;
  26. }
  27. if (Grades > 615 && Grades <= 820) {
  28. tmax = 40000;
  29. }
  30. if (Grades > 820 && Grades <= 1024) {
  31. tmax = 50000;
  32. }
  33. }
  34. void Ausloesemagnet() { //function for magnet trigger
  35. int Magnet = 11; //define the magnet
  36. unsigned long t = 0; //define the runtime
  37. pinMode(Magnet, OUTPUT); //set pin 11 as digital output
  38. while (t < tmax) { //run the loop if the runtime is less then tmax
  39. t = millis(); //returns the number of milliseconds since the Arduino board began running the current program
  40. digitalWrite(Magnet, HIGH); //turn on the magnet
  41. }
  42. digitalWrite(Magnet, LOW); //turn off the magnet, if the loop is broken
  43. }
  44. void Temperatursensor() { //function for reading temperature in celsius
  45. #include <OneWire.h> //use onewire library
  46. #include <DallasTemperature.h> //use dallastemperature library
  47. int tempsensor = 6; //define temperature sensor
  48. double temperature; //define the temperature
  49. double Tmax = 50; //define the max. temperature
  50. OneWire onewirepin(tempsensor); //setup a onewire instance to communicate with any onewire devices
  51. DallasTemperature sensors(&onewirepin); //pass our oneWire reference to dallas temperature
  52. sensors.begin(); //start the library up
  53. sensors.requestTemperatures(); //send the command to get temperatures
  54. temperature=sensors.getTempCByIndex(0); //get the temperature from the first sensor only
  55. if (temperature > Tmax) { //compare the current temperature and max. temperature. if higher than max. , set t as tmax, then that while loop condition in function of ausloesemagnet is not true anymore, so the while loop will be broken until reset.
  56. t = tmax;
  57. }
  58. }
  59. void Heizwendel() { //function for controlling the heater
  60. #include <PWM.h> //use PWM library
  61. #define PIN_OUTPUT 3 //define pin 3 as output of PWM
  62. int D; //set the dutycycle for PWM(8 bit timer can provide a range from 0 to 255 steps for PWM)
  63. int32_t frequency = 10; //set the frequency of PWM in hertz
  64. InitTimersSafe(); //initialize all timers except for 0, to save time keeping functions(for UNO timer0 and timer2 are 8 bit, timer1 is 16 bit; pins 5 and 6: controlled by timer0; pins 9 and 10: controlled by timer1; pins 11 and 3: controlled by timer2)
  65. bool success = SetPinFrequencySafe(PIN_OUTPUT,frequency); //sets the frequency for pin 3
  66. D = map(analogRead(1), 0, 1023, 0, 255); //re-maps the range of analog input A1 from 0 - 1023 to 0 - 255
  67. pwmWrite(PIN_OUTPUT, D); //use this functions instead of analogWrite on pin 3
  68. }
  69. void Stoptaste() { //function for the stop button
  70. int S; //define the push button as stop button
  71. S = digitalRead(2); //read HIGH or LOW digital signal from pin 2
  72. if (S == HIGH) { //if the button is pressed, set t as tmax, then that while loop condition in function of ausloesemagnet is not true anymore, so the while loop will be broken until reset.
  73. t = tmax;
  74. }
  75. }
  76. void LCD() {
  77. #include <LiquidCrystal.h> //use LCD library
  78. LiquidCrystal lcd(13, 12, 10, 9, 8, 7); //initialize the library with the numbers of the interface pins
  79. lcd.begin(16, 2); //set up the LCD's number of columns and rows
  80. lcd.setCursor(0, 0); //set the cursor to column 0, line 0(line 0 is the first row, since counting begins with 0)
  81. lcd.print("T="); //print "Temperature = "
  82. lcd.print(temperature); //print temperature in celsius
  83. lcd.print("`C ");
  84. lcd.print("t="); //print "Time = "
  85. lcd.print(millis()/1000); //print current runtime in seconds
  86. lcd.print("s");
  87. lcd.setCursor(0, 1); //set the cursor to column 0, line 1
  88. lcd.print("D="); //print "Dutycycle = "
  89. lcd.print((D*100)/255); //print current dutycycle in percent
  90. lcd.print("% ");
  91. lcd.print("tmax="); //print "max. runtime = "
  92. lcd.print(tmax/1000); //print preset of max. runtime in seconds
  93. lcd.print("s ");
  94. }
  95. void Kontrollanzeige() { //shows the status, if PWM is working
  96. #define LED 5
  97. analogWrite(LED, D); //put a LED and resistor between pin 5 and GND
  98. }