Hi!
Need help to get my delay in milliseconds to microseconds, have tried google this problem but I am not much of an programmer and haven´t found anything that could help me.
My problem is that I want to use an delay outside the void loop in microseconds instead of milli. This is my code:
#include <util/delay.h>
//-----------------------------------------------
int const valve_in = 8;
int const valve_ut = 9;
int const matgaffel = 3;
volatile byte matskiva_plats=1;
volatile int pressure=55;
volatile int unsigned time = 30 ;
byte desiredmax = 70;
byte desiredmin = 55;
int start=0;
unsigned long timeold = 0;
//------------------------------- ----------------
void setup()
{
Serial.begin(9600);
pinMode(valve_in,OUTPUT);
pinMode(valve_ut,OUTPUT);
pinMode(matgaffel,INPUT);
attachInterrupt(1, valve_ut_fun, RISING);
pinMode(A0,INPUT);
}
//-----------------------------------------------
void loop()
{
if(matskiva_plats==1)
attachInterrupt(1, valve_in_fun, RISING);
else
attachInterrupt(1, valve_ut_fun, FALLING);
}
//-----------------------------------------------
void valve_in_fun()
{
Serial.println(pressure, DEC);
if(start>20)
{
if(pressure<desiredmin)
time++;
else if(pressure>desiredmax)
time–;
}
else
start++;
digitalWrite(valve_ut, LOW);
digitalWrite(valve_in, HIGH);
delay_ms(time);
digitalWrite(valve_in, LOW);
attachInterrupt(1, fix, CHANGE);
matskiva_plats=0;
}
//-----------------------------------------------
void valve_ut_fun()
{
pressure=analogRead(A0);
digitalWrite(valve_ut, HIGH);
attachInterrupt(1, fix, CHANGE);
matskiva_plats=1;
}
//-----------------------------------------------
void delay_ms(unsigned int time)
{
while (time–)
_delay_ms(1);
}
//-----------------------------------------------
void fix()
{
delay_ms(1);
}
//-----------------------------------------------
What my program does is controlling two inlets/ outlets for an air engine depending on the pressure when the piston reaches its lowest point. But when putting this to practical use the engine/ car hacks forward because changing 1ms makes to much differense in air inlet.
Pls help, thanks