Help required in void loop function


hey im fimiliar programming newbie regards arduino projects , development. i'm trying automate electric generator on/off using arduino uno , bunch of relays im kinda stuck in little problem while programming. im trying code process in such way can readily detect if generator gets started in between self. im using 2 220v relays sense presence of mains , generator , opto-isolated relay module power on , off generator accordingly. im giving on signal generator 3 seconds , waiting 5 seconds , again total of 3 times.after third time should not try power on again.i have attached code have written pretty job acting weird i'll explain in bit.

var = variable for loop
wapda = mains 240v supply (utility company_
gen = generator 240v supply
genon = output pin power generator on
genoff = output pin power generator off


code: [select]

int var = 3;
int wapda = 7;
int gen = 8;
int genon = 9;
int genoff = 10;

void setup() {
 
  pinmode(wapda, input);
  digitalwrite(wapda, low);
  pinmode(gen, input);
  digitalwrite(gen, low);
  pinmode(genon, output);
  digitalwrite(genon, low);
  pinmode(genoff, output);
  digitalwrite(genoff, low);
}
 
void loop()
{
         delay(10000);

        if (digitalread(wapda) == high && digitalread(gen) == low)
        {}


        if (digitalread(wapda) == low && digitalread(gen) == low)
        {
          delay(10000);
        }
        for(var;var>=1;var--)               // first self start
        {
        if (digitalread(wapda) == low && digitalread(gen) == low)
         {
          digitalwrite(genon, high);
          delay(1000);
          }
          else
           {
            digitalwrite(genon, low);
           }
           if (digitalread(wapda) == low && digitalread(gen) == high)
           {
            digitalwrite(genon, low);
           }
         }

         digitalwrite(genon, low); 
         var = 3;                       // first self stop



if (digitalread(wapda) == low && digitalread(gen) == low)
        {
          delay(5000);
        }
        for(var;var>=1;var--)               // second self start
        {
        if (digitalread(wapda) == low && digitalread(gen) == low)
         {
          digitalwrite(genon, high);
          delay(1000);
          }
          else
           {
            digitalwrite(genon, low);
           }
           if (digitalread(wapda) == low && digitalread(gen) == high)
           {
            digitalwrite(genon, low);
           }
         }

         digitalwrite(genon, low); 
         var = 3;                       // second self stop





         if (digitalread(wapda) == low && digitalread(gen) == low)
        {
          delay(5000);
        }
        for(var;var>=1;var--)               // third self start
        {
        if (digitalread(wapda) == low && digitalread(gen) == low)
         {
          digitalwrite(genon, high);
          delay(1000);
          }
          else
           {
            digitalwrite(genon, low);
           }
           if (digitalread(wapda) == low && digitalread(gen) == high)
           {
            digitalwrite(genon, low);
           }
         }

         digitalwrite(genon, low);        // third self stop
         var=0;                   

         
      if (digitalread(wapda) == high && digitalread(gen) == high)
      {
   
      var = 3;
      delay(10000);
      digitalwrite(genoff, high);
      delay(10000);
      digitalwrite(genoff, low);
      digitalwrite(genon, low);
      }
}
           




problem:
as i'm using var = 3 in each loop delay of 1 sec , condition check in between, have set value of var 3 after first loop used in second , third. after third loop have set value 0 when loop starts again, should not try power generator on again. after 30 seconds, again!! . don't want try again :/ . i'm not sure how fix it.

ps. same problem off statement in end not matter since generator gets off first time

any appreciated. thanks

you see you  have exact same code copy-pasted 3 times? that's candidate making function.
code: [select]
void genstart() {
  (var; var >= 1; var--)         
  {
    if (digitalread(wapda) == low && digitalread(gen) == low)
    {
      digitalwrite(genon, high);
      delay(1000);
    }
    else
    {
      digitalwrite(genon, low);
    }
    if (digitalread(wapda) == low && digitalread(gen) == high)
    {
      digitalwrite(genon, low);
    }
  }

  digitalwrite(genon, low);
}


now can re-write rest of code use function. logic sequence easier read. recommend making stop function too.
code: [select]
void loop()
{
  delay(10000);

  if (digitalread(wapda) == high && digitalread(gen) == low)
  {}

  if (digitalread(wapda) == low && digitalread(gen) == low)
  {
    delay(10000);
  }
  genstart();
  var = 3;                       // first self stop

  if (digitalread(wapda) == low && digitalread(gen) == low)
  {
    delay(5000);
  }
  genstart();
  var = 3;                       // second self stop

  if (digitalread(wapda) == low && digitalread(gen) == low)
  {
    delay(5000);
  }
  genstart();
  var = 0;

  if (digitalread(wapda) == high && digitalread(gen) == high)
  {

    var = 3;
    delay(10000);
    digitalwrite(genoff, high);
    delay(10000);
    digitalwrite(genoff, low);
    digitalwrite(genon, low);
  }
}


Arduino Forum > Using Arduino > Programming Questions > Help required in void loop function


arduino

Comments

Popular posts from this blog

Valutazione Template - Joomla! Forum - community, help and support

SD Datastring Convention

First use of Arduino Uno : avrdude error on Blink uploading