Strange outcome. Please take a look.

Greetings,

I am trying to implement a doser using a Mega board and I am having problems. I drive the doser by military time with a define start time. I devided the 24 hours in four intervals but with the code I wrote it works only the first time. I am confused why. Please take a look. Thank you very much.

cheers,
MaLi

int dosing_time_Carbon = 5;      // minutes for dosing
int time_dosing_Carbon = 700;   // military time for first time dosing
int n_C = 4;                            // how many time in 24h to dose
int i_C = 0;                             // incrementing dosing index
boolean dosing_C_flag = false;

int mil_time;                       // military time running out of RTC
int Carbon_doser = 35;         // Arduino Mega pin controlling the doser 

void setup()
{
  pinMode(Carbon_doser, OUTPUT);       // digital pin for Carbon doser
  digitalWrite(Carbon_doser, LOW);
}


void loop()
{
  doser_Carbon();
}

void doser_Carbon()
{
  if (i_C = n_C-1)
        {
          i_C = 0;
        }
  if (mil_time >= (time_dosing_Carbon + i_C * (2400/n_C)) % 2400 && mil_time < ((time_dosing_Carbon + i_C * (2400/n_C)) + dosing_time_Carbon) % 2400)
      {
        digitalWrite(Carbon_doser, HIGH);
        dosing_C_flag = true; 
      }
      else {
        digitalWrite(Carbon_doser, LOW);
        if (dosing_C_flag == true)
          {
            i_C = i_C + 1;
            dosing_C_flag = false;
          }
      }
}

You must use the == in an if, not just one =

Check this reference page about half way down under "Warning".

Thank you very much for your prompt answer. So, you are saying that inven inside the
{
...
}

for a boolean variable the == should be used ?

I'm not sure what you are asking there.....

But in an if, you must say if x == y

If you say x = y, in an if or anywhere else, it will give x the value of y.

When you say if x = y, C assigns the value y to x before checking the if, and it's always going to be true.

I think your question means "must I use == even when I want to assign y to x", and of course the answer to that is No, because one = by itself is an assignment.

Did you read this?

Thank you again.
I believe I understand the assignment vs. comparison.
Please ignore the first if cycle. That was meant to reset the index at 24 hours mark.

My confusion comes from why after the first doser run the doser_C_flag sets to true and the first time it goes into the else cycle (at the end of dosing period) the i_c should increment with one. It does not. This way the doser will run the next period which is 700 + 1*2400/4 = 1300.

What am I missing ?

cheers,
MaLi

What am I missing ?

Serial.begin() and Serial.print() statements to tell you what is happening.

Serial.begin() and Serial.print() statements to tell you what is happening.

I'd certainly agree with that.... you could for example print the flag to the monitor to see if it is in fact changing to true when it should.