Looking for Ideas for multiple conditions / Best way to proceed.


hi all!

ill start saying, have been playing around arduino little while (when chance; father of 2 girls...).
i have background in electrical engineering. never got in depth c code. have done small amounts of plc programming ie rslogix500 & 5000 few others in past. more concentrated in schematics , wiring of controls.

to main issue:

background:
i purchased house on edge of city. below sewer level had installed "holding tank" in front yard has grinder pump pump sewers.
the previous owners had issues system because not meant harsh canadian winters. needless system shot.. of it...

now:
i have sorted out wiring , correct control equipment both safe , within code.
i have high level switch in tank, motor contactor overload, , arduino controlling all.

description of plan

i want to:
1) watch input of level switch ( if low green led on)
2)when level switch goes high, a)  turn on output motor (for specified amount of time 10 minutes)
                                                              b) turn off green led
3)after time has passed, turn off motor
4) check level switch again, if still high a) sound alarm
                                                                              b) not allow pump turn on until alarm acknowledged(see other ideas)
                                                                              c) turn on red led 

thats it

this have done far. virtually nothing started if statement realized not going work properly.

code: [select]

const int ackbuttonpin =5;    // stops buzzer when alarm sounded
const int alarmpin = 4;     //will sound alarm if switch high after motor pin has been on
const int overloadpin = 6    //motor overload has tripped
const int levelokpin = 3;    //green led show tank level ok
const int levelswitchpin = 2;     // level switch in tank
const int motorpin =  13;      // pin starting pump

// variables change:
int levelswitchstate = 0;         // variable reading pushbutton status

void setup() {
  // initialize led pin output:
  pinmode(motorpin, output);
  pinmode(alarmpin, output);
  pinmode(levelokpin, output);
  pinmode(levelswitchpin, input);
  pinmode(overloadpin, input);
  pinmode(ackbuttonpin, input); 
}

void loop(){
  // read state of pushbutton value:
  levelswitchstate = digitalread(levelswitchpin);

  // check if pushbutton pressed.
  // if is, buttonstate high:
  if (levelswitchstate == high) {     
    // turn led on:   
    digitalwrite(motorpin, high);
  delay(1000);
  }
  else if{
    // turn led off:
    digitalwrite(motorpin, low);
  }


i looking best route, doing alot of reading trying figure out ...


thanks !!!

looks want implement finite state machine.  there lots of websites instructions , examples.

here example own writing:

note use of 'enum' (enumeration) variables track state.  there 2 fsm's in code.  why there separate states.

note use of select/case statements steer code handles current state.  in each state, check inputs that  cause switch different state. if no such events have occurred, sit in current state until happens move state new state.

code: [select]

/*
description of problem:

set heater1 match heatin1.

if heatin1 low , reverse high:
delay 5sec
write heater1r high
delay 10sec
write heater1r low
delay 10sec
wait heatin1 come on

set heater2 match heatin2.

if heatin2 low , reverse high:
delay 5sec
write heater2r high
delay 10sec
write heater2r low
delay 10sec
wait heatin1 come on

if heatin1 or heatin2 high:
write valve high (open valve)
delay 15sec
write pump high (start pump)

if heatin1 , heatin2 have been low full 30 minutes:
write valve low (close valve)
write pump low (stop pump)
*/

//          *** output's  ***
const int valve     =  2;    // valve in pooler room
const int pump      =  3;    //  pump in pooler room
const int heater1   =  4;    // heater 1 output forward
const int heater1r  =  5;    // heater 1 output reverse
const int heater2   =  6;    // heater 2 output forward
const int heater2r  =  7;    // heater 2 output reverse
const int ledinput  =  8;    // input led pc

//         ***  input's  ***
const int heatin1   =  9;    // input pc 1, active low
const int heatin2   =  10;    // input pc 2, active low
const int reverse   =  11;   // reverse swich heat's


void setup()
{
  //        ***  output's  ***
  pinmode(valve   ,    output);
  pinmode(pump    ,    output);
  pinmode(heater1 ,    output);
  pinmode(heater1r,    output);
  pinmode(heater2 ,    output);
  pinmode(heater2r,    output);
  pinmode(ledinput,    output);

  //        ***  input's  ***
  pinmode(heatin1,     input);
  pinmode(heatin2,     input);
  pinmode(reverse,     input);

}
enum looponestates {
  heating, notheating, reversemode, reversemodeon,  reversemodeoff, waitingtoheat};

enum looptwostates {
  eitherhot, openvalve, startpump, neitherhot,  closevalvestoppump};

void loop() {
  unsigned long currenttime = millis();

  //  loopone heater 1
  static enum looponestates state1 = heating;
  static unsigned long state1entrytime = 0;
  switch (state1) {
  case heating:
    digitalwrite(heater1, high);
    if (digitalread(heatin1))
      state1 = notheating;
    break;

  case notheating:
    digitalwrite(heater1, low);
    if (digitalread(reverse)) {
      state1 = reversemode;
      state1entrytime = currenttime;
    }
    if (!digitalread(heatin1))
      state1 = heating;
    break;

  case reversemode:
    if (currenttime - state1entrytime > 5000) {
      digitalwrite(heater1r, high);
      state1 = reversemodeon;
      state1entrytime = currenttime;
    }
    break;

  case reversemodeon:
    if (currenttime - state1entrytime > 10000) {
      digitalwrite(heater1r, low);
      state1 = reversemodeoff;
      state1entrytime = currenttime;
    }
    break;

  case reversemodeoff:
    if (currenttime - state1entrytime > 10000) {
      state1 = waitingtoheat;
    }
    break;

  case waitingtoheat:
    if (!digitalread(heatin1)) {
      state1 = heating;
    }
    break;
  }




  //  loopone heater 2
  static enum looponestates state2 = heating;
  static unsigned long state2entrytime = 0;
  switch (state2) {
  case heating:
    digitalwrite(heater2, high);
    if (digitalread(heatin2))
      state2 = notheating;
    break;

  case notheating:
    digitalwrite(heater2, low);
    if (digitalread(reverse)) {
      state2 = reversemode;
      state2entrytime = currenttime;
    }
    if (!digitalread(heatin2))
      state2 = heating;
    break;

  case reversemode:
    if (currenttime - state2entrytime > 5000) {
      digitalwrite(heater2r, high);
      state2 = reversemodeon;
      state2entrytime = currenttime;
    }
    break;

  case reversemodeon:
    if (currenttime - state2entrytime > 10000) {
      digitalwrite(heater2r, low);
      state2 = reversemodeoff;
      state2entrytime = currenttime;
    }
    break;

  case reversemodeoff:
    if (currenttime - state2entrytime > 10000) {
      state2 = waitingtoheat;
    }
    break;

  case waitingtoheat:
    if (!digitalread(heatin2)) {
      state2 = heating;
    }
    break;

  }

  //                                                        **loop two**


  //  looptwo heater 1
  static enum looptwostates state3 = eitherhot;
  static unsigned long state3entrytime = 0;
  switch (state3) {
  case eitherhot:
    if (!digitalread(heatin1) || !digitalread(heatin2)) {
      state3 = openvalve;
      state3entrytime = currenttime;
    }
    break;

  case openvalve:
    digitalwrite(valve, high);
    // fifteen seconds later, starty pump
    if (currenttime - state3entrytime > 15000) {
      state3 = startpump;
      state3entrytime = currenttime;
    }
    break;

  case startpump:
    digitalwrite(pump, high);
    state3 = neitherhot;
    state3entrytime = currenttime;
    break;

  case neitherhot:
    // wait full 30 minutes neither heat indicators on
    if (!digitalread(heatin1) || !digitalread(heatin2)) {
      // reset timer if either heater on
      state3entrytime = currenttime;
    }
    if (currenttime - state3entrytime > 1000ul*60ul*30ul) {
      state3 = closevalvestoppump;
    }
    break;

  case closevalvestoppump:
    digitalwrite(valve,       low);
    digitalwrite(pump,        low);
    state3 = eitherhot;
    break;
  }
}


Arduino Forum > Using Arduino > Programming Questions > Looking for Ideas for multiple conditions / Best way to proceed.


arduino

Comments

Popular posts from this blog

Connecting Raspberry Pi 2 to P10(1R)-V706 LED Dot Matrix - Raspberry Pi Forums

TypeError: <unknown> is not a numpy array - Raspberry Pi Forums

datso and removing imagetitle - Joomla! Forum - community, help and support