Problem with Solenoid Push-Pull

Hello everyone, i am new to arduino and this forum... i need some help for my project and sorry for my bad english.

i used solenoid push - pull (12V/2A) , and pressure sensor.

main goal
1.read the pressure and display on LCD.
2.controlling the push power of the solenoid according to the value from the sensor

So my idea to control the push power is using time... for example
solenoid will on/HIGH = 10ms, then off/LOW=990ms ( less power )
or on/HIGH=100ms, then off/LOW=900 ms (Full Power)

and i know i can't use delay for multitasking on arduino, so i already used milis(); function, and i made void function for the pressure code.

the problem is
solenoid worked fine when i did not call the pressure function, it moves according to the time i set.
but when i called the pressure function, the solenoid just do the normal HIGH and LOW, how do i solve this?

here is the example of my code

//I set the time here for control the push power
.
.
unsigned long tOn = 10;
unsigned long tOff = 750;
.
.
.

void loop() {

//PressureSensor(); this is the function that i made for the sensor i used

//this the milis function for the solenoid
current milis = millis();

if((statusSolenoid == HIGH) && (currentmilis - prevmilis >= tOn)) {

statusSolenoid = LOW;
prevmilis = currentmilis;
digitalWrite(solenoid, statusSolenoid);

} else if ((statusSolenoid == LOW) && (currentmilis - prevmilis >= tOff)) {

statusSolenoid = HIGH;
prevmilis = currentmilis;
digitalWrite(solenoid, statusSolenoid);

}
}

what that causing my problem?

Please! send me link for
i used solenoid push - pull (12V/2A), and pressure sensor.

aruskrisna:
what that causing my problem?

You don't have a complete program or if you do you haven't posted it here. You say things go wrong when you call the PressureSensor() function but you haven't shown us that.

Steve

slipstick:
You don't have a complete program or if you do you haven't posted it here. You say things go wrong when you call the PressureSensor() function but you haven't shown us that.

Steve

it is just simple analog read

unsigned long currentTime ;
unsigned long currentTime2=0;
unsigned long interval = 2500;

void Pressure_Sensor()
{

// i used for "for" increase the acuration of measurement so it count 1000 times and i got the mean value

for(int i=0;i<1000;i++)
{ Output = analogRead(A0);
volt = Output*5/1024;
Total+=volt;}

Avg=Total/1000;
Total=0;
float a=Avg*100; //

if( millis() > currentTime2 + interval){
currentTime = millis();
Serial.print("Pressure: ");
Serial.println(a); }

If you won't post the complete program then I give up.

BTW 1000 reads in a for loop is not "just a simple analog read"

Steve

slipstick:
If you won't post the complete program then I give up.

BTW 1000 reads in a for loop is not "just a simple analog read"

Steve

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

//Pressure Sensor
unsigned long Output; //Output voltage from sensor
float Volt;
float Total;
float Avg;

//Solenoid

int statusSolenoid = LOW;
unsigned long tOn = 10; // i set the time for solenoid to turn on and
unsigned long tOff = 750; // off
unsigned long prevmilis = 0;
unsigned long currentmilis ;
unsigned long currentmilis2=0;
unsigned long interval = 2500;
int solenoid=13;

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(solenoid,OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:

currentmilis = millis();
Pressure_Sensor();

if((statusSolenoid == HIGH) && (currentmilis - prevmilis>= tOn)) {

statusSolenoid = LOW;
prevmilis = currentmilis;
digitalWrite(solenoid, statusSolenoid);

} else if((statusSolenoid == LOW) && (currentmilis - prevmilis>= tOff)) {

statusSolenoid = HIGH;
prevmilis = currentmilis;
digitalWrite(solenoid, statusSolenoid);

}

}

void Pressure_Sensor()
{
for(int i=0;i<1000;i++)
{ Output = analogRead(A0);
Volt = Output*5/1024;
Total+=Volt;}

Avg=Total/1000;
Total=0;
float a=Avg*100;

if( millis() > currentmilis2 + interval){
currentmilis = millis();
Serial.print("Pressure: ");
Serial.println(a);

}}

The Pressure_Sensor function takes a long time to execute, 1000 calls to analogRead and several floating point operations for each one. That going to be perhaps 0.2s or so.

I'd suggest averaging only 100 analogReads, and using long int for the averaging, only doing the *5.0/1024
on the total so that no floating point operations happen in the loop.

That will speed up the function to a few tens of ms, so that the millis() doesn't get completely behind testing against tOn.