CEB Control Source Code v1.02

From Open Source Ecology
Jump to: navigation, search
  • Benjamin Gatti - 10-5-2009

// State Machine for CEB Press
// Open Source Ecology / Factor-E Farm
// Benjamin Gatti - 10-5-2009
// This code is provided free to use and modify under the GPL

// Constants Defined
int State = 0;
float TFactor = .6;
boolean Run = 1;
int BaseTime = 4000 ; // Defi	nition of base time scalar for all cylinder motion
// End of Constants Defined

// Definition of Positions and Sequence Timing
// Get rid of all times, just scale BaseTime
#define STATEMIN  1
#define STATEDOWN  1 //Drop the press all the way down
// int TD = 3000;
#define STATELOAD  2 //Wait/Jiggle for chamber to fill
// int TL = 250;
#define STATECLOSE  3 //Move Drawer to closed position - (Move for TC millis)
// int TC = 660;
#define STATEPRESS  4  //Raise the press
// int TP = 5000;
#define STATERELEASE  5  //Drop the Press slightly (TR millis)
// int TR = 120;
#define STATEOUT  6  //Move Drawer to open position - fully Out
// int TO = 2000;
#define STATEUP  7  //Raise the press all the way Up - lifting the Brick above the eject trough
// int TU = 600;
#define STATEEJECT  8  //Move Drawer - full In
int TE = 500;
#define STATEMAX  STATEEJECT
// End of Definition of Positions and Sequence Timing

// Definition of Output Pin Assignment on Arduino
int solPressUp = 5;
int solPressDown = 6;
int solDrawerIn = 10;
int solDrawerOut = 11;
int Timer = 0; //clock for current state
int ledPin = 13;                // LED connected to digital pin 13
// End of Definition of Output Pin Assignment on Arduino

// Setup 
void setup()                    // run once, when the sketch starts
{
  shutdown();
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
 
// Definition of pins (5, 6 and 10, 11) in easy to read format:
  pinMode(solPressUp, OUTPUT);      // sets the digital pin as output
  pinMode(solPressDown, OUTPUT);      // sets the digital pin as output
  pinMode(solDrawerIn, OUTPUT);      // sets the digital pin as output
  pinMode(solDrawerOut, OUTPUT);      // sets the digital pin as output

  Run = 1; 
  Serial.begin (19200);
  Serial.println("Started");
  State = STATEMIN;
  shutdown();
  Timer = 100;
}
// End Setup 

// Shutdown
void shutdown(){
  digitalWrite(solPressUp,LOW);
  digitalWrite(solPressDown,LOW);
  digitalWrite(solDrawerIn,LOW); // In defined as contraction of 14” cylinder
  digitalWrite(solDrawerOut,LOW); // Out defined as expansion of 14” cylinder
}
//End Shutdown

// Actual Pressing Steps
void Transition()
{
  shutdown();
  //  Serial.writeln (State,DEC)
 
//Starting position is main cylinder up and drawer cylinder extended all the way.
 switch (State)    {
  case STATEDOWN:  //Drop the press all the way down
    {
 //     Timer = TD * Tfactor;
      Timer = (BaseTime / 1.3) * Tfactor; 
      digitalWrite(solPressDown,HIGH);
      Serial.println("Down");
      break;
    }
  case STATELOAD: //Wait/Jiggle for chamber to fill
    {
      //     Timer = TL * Tfactor;
      Timer = 0; // Chamber loadedduring STATEDOWN already
      Serial.println("Load");
      break;
    }
  case STATECLOSE:  //Move Drawer to closed position - (Move for TC millis)
    {
      // Timer = TC * Tfactor;
      Timer = (((BaseTime / 5.55)) / 2) / 3.6; // Close small cylinder 7 inches, at ½ flow
      digitalWrite(solDrawerIn,HIGH); 
      // In is defined as contraction of drawer cylinder
      Serial.println("Close");
      break;
    }
  case STATEPRESS: //Raise the press
    {
      // Timer = TP * Tfactor;
      Timer = BaseTime * 0.55; // Extend half way plus  a little
      digitalWrite(solPressUp,HIGH);
      Serial.println("Press");
      break;
    }
  case STATERELEASE: //Drop the Press slightly (TR millis)
    {
      // Timer = TR * Tfactor;
      Timer = BaseTime * 0.05;
      digitalWrite(solPressDown,HIGH);
      Serial.println("Release");
      break;
    }
  case STATEOUT:   //Move Drawer to open position - fully Out (cylinder contracted)
    {
      // Timer = TO * Tfactor;
      Timer = (((BaseTime / 5.55)) / 2) / 3.6) + (BaseTime * 0.01); // Close small cylinder fully 
      // digitalWrite(solDrawerOut,HIGH); 
      digitalWrite(solDrawerIn,HIGH); // Need to contract drawer cylinder
      Serial.println("Out");
      break;
    }
  case STATEUP:  //Raise the press all the way Up - lifting the Brick above the eject trough
    {
      // Timer = TU * Tfactor;
      Timer = BaseTime * 0.55;
      digitalWrite(solPressUp,HIGH);
      Serial.println("Up");
      break;
    }
  case STATEEJECT:   //Move Drawer - full extension
    {
      //Timer = TE * Tfactor;
      Timer = BaseTime / 5.55
      digitalWrite(solDrawerIn,HIGH);
      Serial.println("Eject");
      break;
    }
  default:
    Run =0;
    Serial.println("Error on State Case Shut Down");
    break;
  }
}
// End Actual Pressing Steps


void loop()                     // run over and over again
{
  if(Run==1)
  {
    if (Timer>0){
      delay(1);
      Timer--;
    }
    else
    {
      State++;
      if (State > STATEMAX) {
        State=STATEMIN;
      }
      Transition();
    }
  }
  else
  {
    digitalWrite(solPressUp,LOW);
    digitalWrite(solPressDown,LOW);
    digitalWrite(solDrawerIn,LOW);
    digitalWrite(solDrawerOut,LOW);
  }


  //  digitalWrite(ledPin, HIGH);   // sets the LED on
  //  digitalWrite(Chnl0, HIGH);   // sets the LED on
  //  delay(500);                  // waits for a second
  //  digitalWrite(ledPin, LOW);   // sets the LED on
  //  digitalWrite(Chnl0, LOW);   // sets the LED on
  //  delay(500);                  // waits for a second



}