Hello folks !
I'm actually building an airsoft replica based on an electrovanne and a paintball air tank.
The electrovanne would be controlled by a arduino uno.
So i need to have a loop that opens the valve 100 ms or so at a frequency of 6Hz, I tried using this :
char led=13;
int gachette=7;
int semi=6;
int burst=5;
int auto=4;
int count=0;
unsigned long intervalForCadence=160000;
unsigned long lastForCadence=0;
int stateOfCadence=LOW;
unsigned long intervalForOuverture=100000;
unsigned long lastForOuverture=0;
int stateOfOuverture=LOW;
void setup()
{
pinMode(led,OUTPUT);
pinMode(gachette,INPUT);
pinMode(semi,INPUT);
pinMode(burst,INPUT);
pinMode(auto,INPUT);
digitalWrite(led,LOW);
}
void loop ()
{
unsigned long time = micros();
if (digitalRead(auto)==HIGH)
{
while (digitalRead(gachette)==HIGH)
{
if (time-lastForCadence>=intervalForCadence)
{
stateOfCadence=stateOfCadence==HIGH?LOW:HIGH;
digitalWrite(led,stateOfCadence) ;
lastForCadence+=intervalForCadence ;
}
}
}
Last part of the code is missing and about single fire.
This code doesn't allow me to specify the open time, only the rate of fire, so I tried including a second time loop, but it failed :
char led=13;
int gachette=7;
int semi=6;
int burst=5;
int auto=4;
int count=0;
unsigned long intervalForCadence=160000;
unsigned long lastForCadence=0;
int stateOfCadence=LOW;
unsigned long intervalForOuverture=100000;
unsigned long lastForOuverture=0;
int stateOfOuverture=LOW;
void setup()
{
pinMode(led,OUTPUT);
pinMode(gachette,INPUT);
pinMode(semi,INPUT);
pinMode(burst,INPUT);
pinMode(auto,INPUT);
digitalWrite(led,LOW);
}
void loop ()
{
unsigned long time = micros();
if (digitalRead(auto)==HIGH)
{
while (digitalRead(gachette)==HIGH)
{
if (time-lastForCadence>=intervalForCadence)
{
stateOfCadence=stateOfCadence==HIGH?LOW:HIGH;
lastForCadence+=intervalForCadence;
if (time-lastForOuverture>=intervalForOuverture)
{
stateOfOuverture=stateOfOuverture==HIGH?LOW:HIGH;
digitalWrite(led,stateOfOuverture);
lastForOuverture+=intervalForOuverture;
}
}
}
}
Is it the good idea and just a syntax problem ?
Many thanks in advance for your help