TringaliLuca

gelatiera con arduino

Jul 3rd, 2019
109
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <AFMotor.h>
  2. #include <Servo.h>
  3.  
  4. #include <OneWire.h>
  5. #include <DallasTemperature.h>
  6.  
  7.  
  8. AF_DCMotor motor1(1);
  9. //AF_DCMotor motor(1, MOTOR12_64KHZ);
  10. Servo servo1;
  11. const int buzzerPin = 9;  //servo2
  12.  
  13. #define ONE_WIRE_BUS 10  //servo1
  14.  
  15. const int currentPin = A0;  //ACS712
  16.  
  17. // Setup a oneWire instance to communicate with any OneWire devices
  18. OneWire oneWire(ONE_WIRE_BUS);
  19.  
  20. // Pass our oneWire reference to Dallas Temperature sensor
  21. DallasTemperature sensors(&oneWire);
  22.  
  23.  
  24. // Adafruit Motor Board https://cdn-learn.adafruit.com/downloads/pdf/adafruit-motor-shield.pdf
  25. // China Clone: http://makeitanywhere.org/nyc/index.php?option=com_content&view=article&id=6&Itemid=132#!DK_Motorshield_V1_Top_B
  26.  
  27. bool stopped = false;
  28.  
  29. float threshold = 300;
  30.  
  31.  
  32. void setup()
  33. {  
  34.   Serial.begin(9600);
  35.     pinMode(buzzerPin, OUTPUT);
  36.     pinMode(ONE_WIRE_BUS, INPUT);
  37.     Serial.println("Loading temperature");
  38.     sensors.begin();
  39.    
  40. }
  41.      
  42. void loop()
  43. {
  44.     sensors.requestTemperatures();
  45.     float mytemp = sensors.getTempCByIndex(0);
  46.     Serial.println(mytemp);
  47.     int myspeed = 200;
  48.     long myduration = 1000;
  49.     long mywait = 0;
  50.     if (mytemp > 10) { //mescolamento continuo lento
  51.       myspeed = 100;
  52.       myduration = 30000;
  53.       mywait = 1000;
  54.      }
  55.     if (mytemp < 10 && mytemp > 5 ) { //velocizziamo la perdita di calore
  56.       myspeed = 230;
  57.       myduration = 10000;
  58.       mywait = 10000;
  59.     }
  60.     if (mytemp < 5 && mytemp > 3 ) { //mantecatura
  61.       myspeed = 170;
  62.       myduration = 20000;
  63.       mywait = 5000;
  64.     }
  65.     if (mytemp < 3) {  //congelamento
  66.       myspeed = 200;
  67.       myduration = 10000;
  68.       mywait = 30000;
  69.     }
  70.    
  71.     motor1.run(RELEASE);
  72.     motor1.setSpeed(myspeed);
  73.     if (stopped == false){
  74.       motor1.run(FORWARD);
  75.       stopped = delayMotor(myduration);
  76.     } else {
  77.       innoallagioia();
  78.     }
  79.     motor1.run(RELEASE);
  80.     delay(mywait);
  81.  
  82. }
  83.  
  84. bool delayMotor(long duration)
  85. {
  86.   int window = 100;
  87.   for(int n = 0; n < duration; n=n+window) {
  88.   float average = 0;
  89.   for(int i = 0; i < window; i++) {
  90.     average = average + (0.0264 * analogRead(currentPin) -13.51);
  91.     delay(1);
  92.   }
  93.   average = (average/window)*1000; //milliampere
  94.   Serial.println(average);
  95.   if (average > threshold) return true;
  96.   }
  97.   return false;
  98. }
  99.  
  100. void innoallagioia()
  101. {
  102.   // create variables for notes
  103. int c6 = 1047;
  104. int d6 = 1157;
  105. int e6 = 1319;
  106. int f6 = 1397;
  107. int g6 = 1568;
  108. int a6 = 1760;
  109. int b6 = 1976;
  110. int c7 = 2093;
  111.  
  112. // create variables for note durations
  113. int whole = 2000; //whole note
  114. int half = whole/2; //half note
  115. int quarter = whole/4; //quarter note
  116. int eighth = whole/8; //eighth
  117. int sixteenth = whole/16; //sixteenth
  118.  
  119. // create a variable for a gap between notes
  120. int gap = 20;
  121. tone(buzzerPin, e6, quarter);
  122.   delay(quarter+gap);
  123.   tone(buzzerPin, e6, quarter);
  124.   delay(quarter+gap);
  125.   tone(buzzerPin, f6, quarter);
  126.   delay(quarter+gap);
  127.   tone(buzzerPin, g6, quarter);
  128.   delay(quarter+gap);
  129.  
  130.   tone(buzzerPin, g6, quarter);
  131.   delay(quarter+gap);
  132.   tone(buzzerPin, f6, quarter);
  133.   delay(quarter+gap);
  134.   tone(buzzerPin, e6, quarter);
  135.   delay(quarter+gap);
  136.   tone(buzzerPin, d6, quarter);
  137.   delay(quarter+gap);
  138.  
  139.   tone(buzzerPin, c6, quarter);
  140.   delay(quarter+gap);
  141.   tone(buzzerPin, c6, quarter);
  142.   delay(quarter+gap);
  143.   tone(buzzerPin, d6, quarter);
  144.   delay(quarter+gap);
  145.   tone(buzzerPin, e6, quarter);
  146.   delay(quarter+gap);
  147.  
  148.   tone(buzzerPin, e6, quarter);
  149.   delay(quarter+gap);
  150.   tone(buzzerPin, d6, quarter);
  151.   delay(quarter+gap);
  152.   tone(buzzerPin, d6, half);
  153.   delay(half+gap);
  154. }
RAW Paste Data