Advertisement
Guest User

Untitled

a guest
Dec 14th, 2013
565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. int pump = 2;
  2. int boiler = 14;
  3. int button = 5;
  4. int tank = 7;
  5. int sensor = A4;
  6.  
  7. int i;
  8. const int maxtemp=410;
  9.  
  10. void setup() {
  11.   Serial.begin(115200);
  12.   pinMode(pump, OUTPUT);
  13.   pinMode(boiler, OUTPUT);
  14.   pinMode(button, INPUT);
  15.   pinMode(tank, INPUT);
  16.   digitalWrite(pump,LOW);
  17.   digitalWrite(boiler,LOW);
  18.   Serial.println("");
  19.   Serial.print("Coffee machine ready !");
  20. }
  21.  
  22. void loop() {
  23.   delay(300);
  24.   while(Serial.available()) {
  25.     char character = Serial.read();
  26.     if (character == 's') cycle();
  27.     if (character == 'c') coffee();
  28.     if (character == 'b') preheat();
  29.     if (character == 'r') sensors();
  30.     if (character == 'i') initialize();
  31.   }
  32.   if (digitalRead(button)==LOW) cycle();
  33.   if (i > 5) {
  34.     Serial.println("");
  35.     Serial.print("Coffee machine waiting for orders !");
  36.     Serial.print("jeanlouis");
  37.     i=0;
  38.   }
  39.   i++;
  40. }
  41.  
  42. void initialize() {
  43.   for(i=0;i<100;i++) {
  44.     Serial.println("");
  45.     Serial.print("Coffee machine waiting for orders !");
  46.     Serial.print("jeanlouis");
  47.   }
  48. }
  49. void sensors() {
  50.   while(!Serial.available()) {
  51.     Serial.println("");
  52.     Serial.print("Temperature: ");
  53.     Serial.print(analogRead(sensor)/7);
  54.     Serial.print("C Water tank: ");
  55.     if (digitalRead(tank)==HIGH) Serial.print("present");
  56.     else Serial.print("missing");
  57.     Serial.print("jeanlouis");
  58.     delay(300);
  59.   }
  60. }
  61. void preheat() {
  62.   delay(300);
  63.   int oldpercent=0;
  64.   int startpercent=analogRead(sensor)-10;
  65.   Serial.println("");
  66.   Serial.print("Starting preheating... ");
  67.   Serial.print("jeanlouis");
  68.   while (analogRead(sensor) < maxtemp) {
  69.     if(Serial.available()) break;
  70.     digitalWrite(boiler,HIGH);
  71.     if (i > 1000) {
  72.       int percent = (analogRead(sensor)-startpercent)*100/(maxtemp-startpercent+10);
  73.       if (percent > oldpercent) {
  74.         Serial.println("");
  75.         Serial.print("Preheating... ");
  76.         Serial.print(map(percent,0,90,0,100));
  77.         Serial.print("% ");
  78.         Serial.print(analogRead(sensor)/7);
  79.         Serial.print("C");
  80.         Serial.print("jeanlouis");
  81.         oldpercent=percent;
  82.       }
  83.       i=0;
  84.     }
  85.     i++;
  86.   }
  87.   Serial.println("");
  88.   Serial.print("Preheat end !");
  89.   Serial.print("jeanlouis");
  90.   digitalWrite(boiler,LOW);
  91. }
  92. void coffee() {
  93.   delay(300);
  94.   for(i=0;i<100;i++) {
  95.     if(Serial.available()) break;
  96.     digitalWrite(pump,HIGH);
  97.     Serial.println("");
  98.     Serial.print("Doing some coffee...");
  99.     Serial.print(i);
  100.     Serial.print("%");
  101.     Serial.print("jeanlouis");
  102.     delay(500);
  103.   }
  104.   digitalWrite(pump,LOW);
  105.   while(!Serial.available()) {
  106.     Serial.println("");
  107.     Serial.print("Coffee is ready !");
  108.     Serial.print("jeanlouis");
  109.     delay(300);
  110.   }
  111. }
  112.  
  113. void cycle() {
  114.   preheat();
  115.   coffee();
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement