TringaliLuca

food vacuum box with motor control

Dec 10th, 2019
102
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. #include <Adafruit_BMP085.h>
  3. #include <AFMotor.h>
  4.  
  5. AF_DCMotor pump(1);
  6. AF_DCMotor valve(4);
  7. AF_DCMotor openvalve(3);
  8.  
  9.  
  10. Adafruit_BMP085 bmp;
  11. // VCC -> 3.3V
  12. // GND -> GND
  13. // SCL -> Analog 5
  14. // SDA -> Analog 4
  15.  
  16. int pushButton = 9;
  17. int pumpRelay = 10;
  18.  
  19. double vacuum = 960.0; //at this pressure we got vacuum
  20. double safety = 20.0;  //safety range for pressure
  21.  
  22. bool openbox = false;
  23. bool pressurereached = false;
  24.  
  25. void setup() {
  26.  
  27.   pinMode(pushButton, INPUT);
  28.   pinMode(pumpRelay, OUTPUT);
  29.   valve.run(RELEASE);
  30.   valve.setSpeed(255);
  31.   openvalve.run(RELEASE);
  32.   openvalve.setSpeed(255);
  33.   pump.run(RELEASE);
  34.   pump.setSpeed(255);
  35.  
  36.   Serial.begin(9600);
  37.   Serial.println("Reading pressure");
  38.   if (!bmp.begin()) {
  39.     Serial.println("Could not find a valid BMP085 sensor, check wiring!");
  40.   delay(1000);
  41.     while (1) {}
  42.   }
  43. }
  44.  
  45. void loop() {
  46.     Serial.print("Temperature = ");
  47.     Serial.print(bmp.readTemperature());
  48.     Serial.println(" *C");
  49.  
  50.     double pressure = (bmp.readPressure()/100); //hPa
  51.     Serial.print("Pressure = ");
  52.     Serial.print(pressure);
  53.     Serial.println(" Pa");
  54.  
  55.     delay(500);
  56.  
  57.     if (digitalRead(pushButton) == HIGH) {
  58.       openbox = false;
  59.       if (pressure > vacuum) pressurereached = false;
  60.       if (!pressurereached){
  61.       openvalve.run(RELEASE);
  62.       valve.run(FORWARD);
  63.       pump.run(FORWARD);
  64.       delay(500);
  65.       digitalWrite(pumpRelay, HIGH);
  66.      
  67.       while (pressure > vacuum && digitalRead(pushButton) == HIGH) {
  68.         pressure = (bmp.readPressure()/100)+safety; //we will actually get to (960-20)=940 hPa before stopping
  69.         Serial.print("Pressure = ");
  70.         Serial.print(pressure);
  71.         Serial.println(" Pa");
  72.         delay(1000);
  73.       }
  74.       Serial.println("Stop");
  75.       pump.run(RELEASE);
  76.       digitalWrite(pumpRelay, LOW);
  77.       valve.run(RELEASE);
  78.       pressurereached = true;
  79.       }
  80.       delay(5000);
  81.     } else {
  82.       digitalWrite(pumpRelay, LOW);
  83.       pump.run(RELEASE);
  84.       valve.run(RELEASE);
  85.       if (!openbox) openvalve.run(FORWARD);
  86.       int i = 0;
  87.       while (i < 20 && digitalRead(pushButton) == LOW) {
  88.       delay(1000);
  89.       i++;
  90.       }
  91.       openvalve.run(RELEASE);
  92.       openbox = true;
  93.     }
  94.    
  95. }
RAW Paste Data