Estability in a system for controling pressure

Hi! Can you please help me with my code and electronic instalation? I attach a copy of the code below.

Im doing a single instalation that control the pressure with a BMP280 or a MPL3115A2 using a servo for take out the airand a pump for take inside air.
It is not too complicated and the code you can see below it is not complicated, but often it stop and i have to reset.
I´ve put a Diode Flyback, solded three capacitors in the motor, changed the chinese arduino mini for one original,.. but it continue to stop and have to reset.
Thank you very much.

//SISTEMA: CON SENSOR DE PRESIÓN BMP280 CONTROLA MOTOR DC Y SERVO (PARA CONTROLAR LA PRESIÓN CON VALVULA Y TURBINA).
//Hay que colocar el servo de forma que a 80º este cerrada la válvula y a 110º abierta (RANGO 80-110 APROX).
//El SISTEMA está dispuesto con la válvula y el motor-turbina conectados a la misma tubería que va a la válvula para meter y sacar aire por el mismo sitio.
//Cuando la presión sea alta, esté por encima de un valor concreto, SPSup, hay que extraer aire del Sistema, para lo que
//se accionará el servo. Y cuando la presión este baja, por debajo de un valor concreto, SPInf, se accionará el servo para abrir y el motor-turbina para insuflar aire.

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>

#define BMP_SCK (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS (10)

Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);

#include <Servo.h>
//Define Variables we'll be connecting to
/haciendo medidas vemos que el sensor de presión ofrece unos valores con un rango
de entre 950 hPa (menor presión posible) y 1200hPa (balón a tope)
/
float Setpoint = 96000; //valor de presión deseado, en Pascales

Servo ServoValvula;
const int motorPin = 3; //Debe ser PWM
int velocidadMotor = 0; //Inicializo la velocidad del motor a 0

const int Kp = 1; //Constante de proporcionalidad

int diferencia;//Diferencia entre el setPoint y la presión actual, pressure

void setup() {

//Wire.begin(); // Join i2c bus
Serial.begin(9600); // Start serial for output

Serial.println(F("BMP280 test"));

if (!bmp.begin()) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
while (1);
}

/* Default settings from datasheet. /
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /
Operating Mode. /
Adafruit_BMP280::SAMPLING_X2, /
Temp. oversampling /
Adafruit_BMP280::SAMPLING_X16, /
Pressure oversampling /
Adafruit_BMP280::FILTER_X16, /
Filtering. /
Adafruit_BMP280::STANDBY_MS_500); /
Standby time. */

ServoValvula.attach(2);
pinMode(motorPin, OUTPUT);
//Posiciones iniciales servo cerrado y motor parado:
ServoValvula.write(115);//Servo en cerrado
analogWrite(motorPin, 0);//Motor parado
//Coge como SetPoint la presion en ese momento
Setpoint = bmp.readPressure();//Hace una medida inicial de presión y la asigna al Set Point.
}
void loop() {

Serial.print("Pressure Set Point=");
Serial.print(Setpoint);
Serial.print("Pa");

//Serial.print(F("Temperature = "));
//Serial.print(bmp.readTemperature());
//Serial.println(" *C");

Serial.print(F(" Pressure = "));
Serial.print(bmp.readPressure());
Serial.println(" Pa");
float pressure=bmp.readPressure();

//Serial.print(F("Approx altitude = "));
//Serial.print(bmp.readAltitude(1013.25)); /* Adjusted to local forecast! */
//Serial.println(" m");

Serial.println();
delay(100);

//CONTROL PROPORCIONAL:

velocidadMotor = Kp * (Setpoint - pressure);
velocidadMotor = constrain(velocidadMotor, 0, 255);

//Hay una ZONA DE CONFORT alrededor del SetPoint entre 2mmb arriba (200Pa) y 0.5 mbares (50Pa) por debajo
//EN LA QUE NO SE ACCIONA NI SERVO NI MOTOR Y SE MANTIENE CERRADO EL SISTEMA.
if (pressure > Setpoint + 200) { //Presión por encima del valor Umbral máximo: Servo lleva el control
ServoValvula.write(65);//Servo accionado para abrir válvula
delay(15);
analogWrite(motorPin, 0);//Motor parado
delay(15);

} else if (pressure < Setpoint - 50) { //Presión por debajo del valor umbral mínimo: Servo abre y motor accionado. Valor Tipico de 0 a 50.

ServoValvula.write(65);//Servo coloca válvula en abierto al máximo para que entre el aire insuflado por el motor-turbina
delay(15);
analogWrite(motorPin, velocidadMotor);//Motor lleva el control e insufla aire, Valor de 0 a 255.
delay(15);
} else {//estamos dentro de los valores umbrales mínimo (SpInf) y máximo(SpSup): Todo parado, sevo cierra válvula.
ServoValvula.write(115);//Servo con válvula cerrada
delay(15);
analogWrite(motorPin, 0);//Motor parado
delay(15);
}

}

What is powering the servo and motor?
Post links to servo and motor and a wiring diagram.

Hi! Can you please help me with my code and electronic instalation? I attach a copy of the code below.

Im doing a single instalation that control the pressure with a BMP280 or a MPL3115A2 using a servo for take out the airand a pump for take inside air.
It is not too complicated and the code you can see below it is not complicated, but often it stop and i have to reset.
I´ve put a Diode Flyback, solded three capacitors in the motor, changed the chinese arduino mini for one original,.. but it continue to stop and have to reset.
Thank you very much.

//SISTEMA: CON SENSOR DE PRESIÓN MPL3115A2 CONTROLA MOTOR DC Y SERVO (PARA CONTROLAR LA PRESIÓN CON VALVULA Y TURBINA).
//Hay que colocar el servo de forma que a 80º este cerrada la válvula y a 110º abierta (RANGO 80-110 APROX).
//El SISTEMA está dispuesto con la válvula y el motor-turbina conectados a la misma tubería que va a la válvula para meter y sacar aire por el mismo sitio.
//Cuando la presión sea alta, esté por encima de un valor concreto, SPSup, hay que extraer aire del Sistema, para lo que
//se accionará el servo. Y cuando la presión este baja, por debajo de un valor concreto, SPInf, se accionará el servo para abrir y el motor-turbina para insuflar aire.

#include <Wire.h>
#include "SparkFunMPL3115A2.h"

//Create an instance of the object
MPL3115A2 myPressure;

#include <Servo.h>
//Define Variables we'll be connecting to
/haciendo medidas vemos que el sensor de presión ofrece unos valores con un rango
de entre 950 hPa (menor presión posible) y 1200hPa (balón a tope)
/
float Setpoint = 96000; //valor de presión deseado, en Pascales

Servo ServoValvula;
const int motorPin = 3; //Debe ser PWM
int velocidadMotor = 0; //Inicializo la velocidad del motor a 0

const int Kp = 1; //Constante de proporcionalidad

int diferencia;//Diferencia entre el setPoint y la presión actual, pressure

void setup() {

Wire.begin(); // Join i2c bus
Serial.begin(9600); // Start serial for output

myPressure.begin(); // Get sensor online

//Configure the sensor
//myPressure.setModeAltimeter(); // Measure altitude above sea level in meters
myPressure.setModeBarometer(); // Measure pressure in Pascals from 20 to 110 kPa

myPressure.setOversampleRate(7); // Set Oversample to the recommended 128
myPressure.enableEventFlags(); // Enable all three pressure and temp event flags

ServoValvula.attach(2);
pinMode(motorPin, OUTPUT);
//Posiciones iniciales servo cerrado y motor parado:
ServoValvula.write(115);//Servo en cerrado
analogWrite(motorPin, 0);//Motor parado
//Coge como SetPoint la presion en ese momento
Setpoint = myPressure.readPressure();//Hace una medida inicial de presión y la asigna al Set Point.
}
void loop() {

Serial.print("Pressure Set Point=");
Serial.print(Setpoint);
Serial.print("Pa");

//float altitude = myPressure.readAltitude();
//Serial.print("Altitude(m):");
//Serial.print(altitude, 2);//El segundo parámetro debe ser el tiempo de delay

//float altitude = myPressure.readAltitudeFt();
//Serial.print(" Altitude(ft):");
//Serial.print(altitude, 2);

float pressure = myPressure.readPressure();
Serial.print(" Pressure(Pa):");
Serial.println(pressure, 2);

float temperature = myPressure.readTemp();
//Serial.print(" Temp(c):");
//Serial.print(temperature, 2);

//float temperature = myPressure.readTempF();
//Serial.print(" Temp(f):");
//Serial.print(temperature, 2);

//CONTROL PROPORCIONAL:

velocidadMotor = Kp * (Setpoint - pressure);
velocidadMotor = constrain(velocidadMotor, 0, 255);

//Hay una ZONA DE CONFORT alrededor del SetPoint entre 2mmb arriba (200Pa) y 0.5 mbares (50Pa) por debajo
//EN LA QUE NO SE ACCIONA NI SERVO NI MOTOR Y SE MANTIENE CERRADO EL SISTEMA.
if (pressure > Setpoint + 200) { //Presión por encima del valor Umbral máximo: Servo lleva el control
ServoValvula.write(65);//Servo accionado para abrir válvula
delay(15);
analogWrite(motorPin, 0);//Motor parado
delay(15);

} else if (pressure < Setpoint - 0) { //Presión por debajo del valor umbral mínimo: Servo abre y motor accionado. Valor Tipico de 0 a 50.

ServoValvula.write(65);//Servo coloca válvula en abierto al máximo para que entre el aire insuflado por el motor-turbina
delay(15);
analogWrite(motorPin, velocidadMotor);//Motor lleva el control e insufla aire, Valor de 0 a 255.
delay(15);
} else {//estamos dentro de los valores umbrales mínimo (SpInf) y máximo(SpSup): Todo parado, sevo cierra válvula.
ServoValvula.write(115);//Servo con válvula cerrada
delay(15);
analogWrite(motorPin, 0);//Motor parado
delay(15);
}

}

@LMSimarroG. I have deleted your other account (LMDronie). You are only allowed to have one account on the Arduino Forum. If you do that again, you will have all your accounts permanently banned.

Thanks in advance for your cooperation.

Please read the forum rules, one of which is
USE CODE TAGS !
(<|>)
((highlight your code and click the Code Tags
toolbutton)

Hi,
Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you put the "Blink without Delay" example in your code so that the LED on the controller acts as a heartbeat.
This will tell you if the controller has locked up, that is the LED will stop blinking.
OR
You have a hardware or coding problem, that is the LED keeps blinking, even though your project has frozen.

Tom..... :slight_smile:

I suspect this is a dead thread - the OP(s) is having serious identity problems! :cold_sweat:

Hi, Pert, Raschemmel, TomGeorge, Paul_B, sorry, I have new email account so I did one new here. And I´ve used the old one to put the same question but with different code (using the BMP280 pressure sensor). The code with the MPL3115 is in the new account LMDronie.
Ok I´ll use the tags when putting the code, sorry and thanks Raschemmel.

I dont have now the circuit but it is very simple, with a Transistor darlington TIP120 for controlling the motor.
Also has the flyback diode and 3 capacitors soldeds in the motor, as I said. The miniservo directly an a digital pin.
Thank you very much for your help.

I continuing having problems with it because it often gets blocked and stops and I have to reste.

Thank you everybody again for your help

Hi, Pert, Raschemmel, TomGeorge, Paul_B, sorry, I have new email account so I did one new here. And I´ve used the old one to put the same question but with different code (using the BMP280 pressure sensor). The code with the MPL3115 is in the new account LMDronie.
Ok I´ll use the tags when putting the code, sorry and thanks Raschemmel.

I dont have now the circuit but it is very simple, with a Transistor darlington TIP120 for controlling the motor.
Also has the flyback diode and 3 capacitors soldeds in the motor, as I said. The miniservo directly an a digital pin.
Thank you very much for your help.
I´m using a 9-12 v battery Lipo or NiMH feeding the motor and the arduino nano with the Vin.

I continuing having problems with it because it often gets blocked and stops and I have to reste.

Thank you everybody again for your help
[/quote]