Help! How to code this (START SWITCH)

Sir/Mam,
I have a problem on coding in my start button, when i pushed the start button, the process still working and need to finish its task given on the code then OFF,

here the code without the switch :

//WATERnLYE PROCESS
int RELAY9 = 9; //LYEPOWDERMOTOR
int RELAY10 = 10; //WATERinlet
int RELAY11 = 11; //WATERnLYEMIXER
int RELAY12 = 12; //START SWITCH


//MAIN PROCESS
int RELAY1 = 1; //Mixer MOTOR
int RELAY2 = 2; //PAPAYA EXTRACT
int RELAY3 = 3; //CANOLA OIL
int RELAY4 = 4; //COCONUT OIL
int RELAY5 = 5; //PALM OIL WITH COLORANT
int RELAY6 = 6; //PAPAYA PERFUME
int RELAY7 = 7; //WATERnLYE
int RELAY8 = 8; //FINAL PRODUCT POURING
int RELAY13 = 13; //START SWITCH

void setup() {
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
pinMode(RELAY4, OUTPUT);
pinMode(RELAY5, OUTPUT);
pinMode(RELAY6, OUTPUT);
pinMode(RELAY7, OUTPUT);
pinMode(RELAY8, OUTPUT);
pinMode(RELAY9, OUTPUT);
pinMode(RELAY10, OUTPUT);
pinMode(RELAY11, OUTPUT);
pinMode(RELAY12, INPUT);
pinMode(RELAY13, INPUT);

digitalWrite(RELAY9, LOW);
digitalWrite(RELAY10, LOW);
delay(1000);

digitalWrite(RELAY1, LOW);
digitalWrite(RELAY2, LOW);
digitalWrite(RELAY3, LOW);
digitalWrite(RELAY4, LOW);
digitalWrite(RELAY5, LOW);
digitalWrite(RELAY7, LOW);
digitalWrite(RELAY8, LOW);
delay(1000);



}

void loop() {
 //WATERnLYE PROCESS
//digitalWrite(RELAY9, HIGH); //WATER POURING
//delay(15000);
//digitalWrite(RELAY9, LOW);
//delay(500);

//digitalWrite(RELAY10, HIGH); //LYEPOURING
//delay(15000);
//digitalWrite(RELAY10, LOW);
//delay(500);

//digitalWrite(RELAY11, HIGH); //START OF MIXING
//delay(20000);
//digitalWrite(RELAY11, LOW);
//delay(500);

//MAIN PROGRAM

 digitalWrite(RELAY6, HIGH); //PAPAYA PERFUME
 digitalWrite(RELAY2, HIGH); //PAPAYA EXTRACT
 digitalWrite(RELAY3, HIGH); //CANOLA OIL
 digitalWrite(RELAY4, HIGH); //COCONUT OIL
 digitalWrite(RELAY5, HIGH); //PALM OIL WITH COLORANT
 delay(5000);
  
 digitalWrite(RELAY6, LOW);
 digitalWrite(RELAY2, LOW);
 delay(2000);
 digitalWrite(RELAY3, LOW);
 delay(2000);
 digitalWrite(RELAY4, LOW);
 delay(2000);
 digitalWrite(RELAY5, LOW);
 delay(2000);
 
 //MOTOR
 digitalWrite(RELAY1, HIGH); //MOTOR
 digitalWrite(RELAY7, HIGH); //WATERnLYE
 delay(5000);
 
 digitalWrite(RELAY7, LOW);
 delay(500);
 
 digitalWrite(RELAY1, HIGH);
 delay(5000);
 digitalWrite(RELAY1, LOW);
 delay(500);
 
 //MOLDPOURING
 digitalWrite(RELAY8, HIGH);
 delay(10000);
 digitalWrite(RELAY8, LOW);
 delay(500);
 
 
  
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Hi,

Please use code tags.. See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Can you please post a copy of your sketch, using code tags?
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

Can you please explain the process that it is supposed to do, then what the problems are.

Thnaks Tom......... :slight_smile:

Just to confirm: you want the code to run only once and then only when a button is pushed?

Try this:

Firstly, move everything from loop() into setup() after what you have in setup() already.

Leave a "dummy" void loop() {} to keep the compiler happy.

Then, above the part you just moved into setup, put:

while (startSwitch == LOW)  //or whatever you called the switch
{}

(or == HIGH depending on how you wired the switch (active high or active low), the while will loop while it's "normal" and let you go through once it's "pressed")

To Sir Tom,
Sorry sir I'm newbie in this forum, but btw thanks for the info :slight_smile:

To sir Jim,
Yes i want to run the code once, i already move the entire code from void loop into void setup but the problem is, when i pressed the start the button(ON), the process starts, but when i press the start button (OFF) , the Arduino finishes the entire code before it turns off. what i need is when i turn off the switch, the entire microcontroller(ARDUINO) is turn off, and when i start again the start button(ON), the process starts from the top.

I really need to solve this problem to move on,

Thanks!,
MrMakel :slight_smile:

but when i press the start button (OFF) , the Arduino finishes the entire code before it turns off

Can you confirm that you want to be able to stop the process at any stage but restart at the beginning ?

To Mr.Bob,
Yes, I want the Process to start again from the beginning whenever i stop it at any stage. I try to code If statements but the result is Arduino finishes the process on the if statements before going to the end if wherein the button is OFF. so if i turn off the switch it didn't totally turns off in an instant.

I'm not sure I quite understand the requirement - it seems clear that if the switch is turned off, the processing must end immediately. Should the process restart at that point or wait for another switch input?

In any event, the solution is similar. As ever, you need to get rid of the calls to delay(). The arduino will not respond to switch inputs while delay is going on. A state machine is a common solution - you can find examples in the forum - there are even a few written by me.

As an alternative method, you can take a data driven approach. You can declare three arrays (or better an array of struct) and have the arrays store what needs to happen to your relay pins. Array one tells you which relay pin you need to act on. Array two tells you what state to set the pin to. Array three tells you how long to wait after setting the pin. Finally, you need a variable to tell you which step you're on.

Then your sketch simply loops around checking whether it's time to move on to the next step using millis and it can monitor your switch continuously to see if it's necessary to abort.

Yes sir, I want to have a result wherein when i pressed the start button it will start then when in the middle of the process i want it to turn off the result must be instant off.., but i experience when i turn off the switch in the middle of the process, the arduino finishes the process until the end before it totally turns off.

i make some coding here, i dont know if this is right to achieve my goal:

/* sketch 3
   turn on a LED when the button is pressed and let it on
   until the button is pressed again
   */ 
   
//WATERnLYE PROCESS
int RELAY9 = 9; //LYEPOWDERMOTOR
int RELAY10 = 10; //WATERinlet
int RELAY11 = 11; //WATERnLYEMIXER

//MAIN PROCESS
int RELAY1 = 1; //Mixer MOTOR
int RELAY2 = 2; //PAPAYA EXTRACT
int RELAY3 = 3; //CANOLA OIL
int RELAY4 = 4; //COCONUT OIL
int RELAY5 = 5; //PALM OIL WITH COLORANT
int RELAY6 = 6; //PAPAYA PERFUME
int RELAY7 = 7; //WATERnLYE
int RELAY8 = 8; //FINAL PRODUCT POURING

   
//SWITCH WATERnLYE START PROCESS
int pinButton = 12;
int LED = 13;
int stateLED = LOW;
int stateButton;
int previous = LOW;
long time = 0;
long debounce = 200;
    


void setup() {
//RELAY DECLARATION
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
pinMode(RELAY4, OUTPUT);
pinMode(RELAY5, OUTPUT);
pinMode(RELAY6, OUTPUT);
pinMode(RELAY7, OUTPUT);
pinMode(RELAY8, OUTPUT);
pinMode(RELAY9, OUTPUT);
pinMode(RELAY10, OUTPUT);
pinMode(RELAY11, OUTPUT);
pinMode(RELAY12, INPUT);

//RELAY ACTIVE LOW SETTINGS
digitalWrite(RELAY9, LOW);
digitalWrite(RELAY10, LOW);
delay(1000);

digitalWrite(RELAY1, LOW);
digitalWrite(RELAY2, LOW);
digitalWrite(RELAY3, LOW);
digitalWrite(RELAY4, LOW);
digitalWrite(RELAY5, LOW);
digitalWrite(RELAY7, LOW);
digitalWrite(RELAY8, LOW);
delay(1000);

//SWITCH CONFIGURATION
pinMode(pinButton, INPUT);
pinMode(LED, OUTPUT);
}
    
void loop() {
//NO CODE YET JUST A LED AND A SWITCH  

 stateButton = digitalRead(pinButton);
 if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
 if(stateLED == HIGH){
 stateLED = LOW;
 } else {
 stateLED = HIGH;
 }
 time = millis();
 }
 digitalWrite(LED, stateLED);
 previous == stateButton;
 }

Some small things ---

Your debounce code probably won't work because it needs both the state change and time passed to happen together. What works better is a 2 independent step process:

  1. detect pin change means start the debounce interval, every time pin changes which bounce does.

  2. if/when the interval ends, that is when the switch/pin is stable.

Make the interval zero when finished and only ever non-zero after change, interval value becomes a flag to tell when to run step 2.

In Arduino IDE, the Tools menu, use Autoformat. If you have unbalanced () or {} it will give an error. This is a quick check that also keeps your indents neat and easier to read.

Don't use int when char or byte will do.
Type int can be -32768 to +32767
Type char can be -128 to +127
Type byte can be 0 to +255

Learn about arrays and loops.
RELAY1
RELAY2
RELAY3
etc
------- requires separate code lines naming each

byte relay[8] = { 2,3,4,5,6,7,8,9 }; // start with 2 because pins 0 and 1 are for Serial
------- allows generalized code to access relay[0] through relay[7] where the number can be a variable.

What wildbill posted is probably your best path.

You can code the steps of your process as pieces that only run when triggered.
Triggers can be time is up or pin event (HIGH or LOW or change) or a variable has a certain value or bit set or combinations.

So maybe I have a led that has start code watching for a start trigger and stop code watching for a stop trigger. Neither code depends on the other. Neither code is inside of braces except for these:

// extremely simple example
void loop()
{
// code for some processes ..........

// led start
if ( ledState == 1 )
{
digitalWrite( ledPin, HIGH );
ledState = 0;
}

// led stop
if ( ledState == 2 )
{
digitalWrite( ledPin, LOW );
ledState = 0;
}
}

So here you don't have the on and off inside of other logic. Multiple parts of your code can examine and change ledState without any of those being inside of any other. How you work the triggers lets you run the machine. As long as loop() never has to take long (1 millisecond is 16000 cycles long) you can change the machine as "instantly" as your loop() is short.

Be sure to plan your steps/stages/states out well before you write any code. Don't rush.

Sorry sir but im newbie in Arduino, :frowning: Btw.., i think it will take time to understand that kind of things.., can you give me alternatives when it comes to my problem?.., as stated above, i only need a switch that start the process, and stop the process instantly whenever it is in the middle of its executing or not.., i tried using if statements (THE SAMPLE CODE in the arduino GUI) but i end up in the wrong result, it finishes the entire process before it gets turn off.

:frowning:

Using Autoformat, you can start that now.

I think the biggest language barrier now may be the base C Language.

You need to learn beginner level C code including some practice before you can be much good at this, even to be able to understand examples, to trace what they should be doing.

Here is the Foundations page:

Except for making your own Libraries and Arduino on a breadboard (the last two parts), all of that you should at least not be confused by the terms before writing your own code. Certainly it ill take time to learn but trust me, Engineer, that will save you many times longer in mistakes and confusions you will not be making.

Do you know the Reference page of the Arduino site?

What commands you see in code but do not understand, look it up here first.

...........................................................

In your code the switch is not the big part. The big part is arranging your process so that it can be interrupted and putting in the hooks to do that. I can give you switch code all packaged up, making the rest dynamic will be plenty enough as it is.

Perhaps here we can walk you through the process and you can pick up some C at the same time?
Start by explaining the process itself in detailed steps if you will?

You are Engineer so I trust that you can get through this. :slight_smile:

GoForSmoke:
Using Autoformat, you can start that now.

I think the biggest language barrier now may be the base C Language.

You need to learn beginner level C code including some practice before you can be much good at this, even to be able to understand examples, to trace what they should be doing.

Here is the Foundations page:
http://arduino.cc/en/Tutorial/Foundations

Except for making your own Libraries and Arduino on a breadboard (the last two parts), all of that you should at least not be confused by the terms before writing your own code. Certainly it ill take time to learn but trust me, Engineer, that will save you many times longer in mistakes and confusions you will not be making.

Do you know the Reference page of the Arduino site?
Arduino - Home

What commands you see in code but do not understand, look it up here first.

...........................................................

In your code the switch is not the big part. The big part is arranging your process so that it can be interrupted and putting in the hooks to do that. I can give you switch code all packaged up, making the rest dynamic will be plenty enough as it is.

Perhaps here we can walk you through the process and you can pick up some C at the same time?
Start by explaining the process itself in detailed steps if you will?

You are Engineer so I trust that you can get through this. :slight_smile:

Sir, What do you mean by Arranging my process.., actually i already arrange what is needed to be done on each process, the problem is when i turn off the switch (GOING INTO IF ELSE STATEMENT) it didnt turn off instantly, in short, i need an emergency stop wherein i can stop the process whether it is in the middle of the process or not( SIMPLY TURN ON and SIMPLY TURN OFF(BUT INSTANT TURN OFF)),

heres the whole code sir :

// CODE :

//WATERnLYE PROCESS SEPERATE ARDUINO(ATMEGA328)
int RELAY9 = 9; //LYEPOWDERMOTOR
int RELAY10 = 10; //WATERinlet
int RELAY11 = 11; //WATERnLYEMIXER
int RELAY12 = 12; //START SWITCH

//MAIN PROCESS
int RELAY1 = 1; //Mixer MOTOR
int RELAY2 = 2; //PAPAYA EXTRACT
int RELAY3 = 3; //CANOLA OIL
int RELAY4 = 4; //COCONUT OIL
int RELAY5 = 5; //PALM OIL WITH COLORANT
int RELAY6 = 6; //PAPAYA PERFUME
int RELAY7 = 7; //WATERnLYE
int RELAY8 = 8; //FINAL PRODUCT POURING
int RELAY13 = 13; //START SWITCH

void setup() {
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
pinMode(RELAY4, OUTPUT);
pinMode(RELAY5, OUTPUT);
pinMode(RELAY6, OUTPUT);
pinMode(RELAY7, OUTPUT);
pinMode(RELAY8, OUTPUT);
pinMode(RELAY9, OUTPUT);
pinMode(RELAY10, OUTPUT);
pinMode(RELAY11, OUTPUT);
pinMode(RELAY12, INPUT);
pinMode(RELAY13, INPUT);

digitalWrite(RELAY9, LOW);
digitalWrite(RELAY10, LOW);
delay(1000);

digitalWrite(RELAY1, LOW);
digitalWrite(RELAY2, LOW);
digitalWrite(RELAY3, LOW);
digitalWrite(RELAY4, LOW);
digitalWrite(RELAY5, LOW);
digitalWrite(RELAY7, LOW);
digitalWrite(RELAY8, LOW);
delay(1000);

}

void loop() {
//WATERnLYE PROCESS
//digitalWrite(RELAY9, HIGH); //WATER POURING
//delay(15000);
//digitalWrite(RELAY9, LOW);
//delay(500);

//digitalWrite(RELAY10, HIGH); //LYEPOURING
//delay(15000);
//digitalWrite(RELAY10, LOW);
//delay(500);

//digitalWrite(RELAY11, HIGH); //START OF MIXING
//delay(20000);
//digitalWrite(RELAY11, LOW);
//delay(500);

//MAIN PROGRAM

//WHERE CAN I PUT THE SWITCH CODE HERE HELP ME PLEASE, THANKS!
digitalWrite(RELAY6, HIGH); //PAPAYA PERFUME
digitalWrite(RELAY2, HIGH); //PAPAYA EXTRACT
digitalWrite(RELAY3, HIGH); //CANOLA OIL
digitalWrite(RELAY4, HIGH); //COCONUT OIL
digitalWrite(RELAY5, HIGH); //PALM OIL WITH COLORANT
delay(5000);

digitalWrite(RELAY6, LOW);
digitalWrite(RELAY2, LOW);
delay(2000);
digitalWrite(RELAY3, LOW);
delay(2000);
digitalWrite(RELAY4, LOW);
delay(2000);
digitalWrite(RELAY5, LOW);
delay(2000);

//MOTOR
digitalWrite(RELAY1, HIGH); //MOTOR
digitalWrite(RELAY7, HIGH); //WATERnLYE
delay(5000);

digitalWrite(RELAY7, LOW);
delay(500);

digitalWrite(RELAY1, HIGH);
delay(5000);
digitalWrite(RELAY1, LOW);
delay(500);

//MOLDPOURING
digitalWrite(RELAY8, HIGH);
delay(10000);
digitalWrite(RELAY8, LOW);
delay(500);

}

During every one of those delays, your board will not be able to see a switch change let alone be able to stop the action. With that form of code your emergency stop it to cut power.

As you have it, your process can't do what you you want because of how it is arranged.
But it's fixable in essentially the same order!
It's just going to take a while even knowing what to do.

Please reply to this to give me a link back to the thread and I'll post alternative when I am done.
Trust me, the step is so big you will plenty to do understanding what was done.

CHANGED CODE BELOW -- FIXED ONE SET OF ERRORS

That didn't take as long as I thought it would so back before you.

This compiles but without all the hardware I can't run it.
It should at least give you a good idea of what to do.

I set it up so pin 12 is your start switch and 13 is your stop switch but you will have to rewire those since my button objects sets the pins as INPUT_PULLUP. All they need is a switch wired directly to ground, not power. I don't even bother with a switch, I just stick a jumper in the pin hole and ground it to "press". So wire your switches to ground.

The stop button as coded has a 20 ms debounce. If you want near zero debounce then use a capacitor to debounce your switch in hardware. OTOH consider 20 ms on the scale of how slow your finger moves and 20 ms being 1/50th of a second and what's the point? You can't notice just 20 ms.

// changed 1/17/15

// program name, version, author and date here

// this below is an enumerator. It associates labels with values
__attribute__ ((__packed__)) enum  RELAYS // makes bytes 
{
  MIXMOT = 1, EXTRACT, CANOLA, COCO, PALM, // 1 to 5 
  PERFUME, LYE, POUR, DRYLYEMOT, INLET, // 6 to 10
  WETLYEMOT, PROCSTART, PROCSTOP
}; 

byte pins[ 13 ] = // if you rewire to clear some usefule Arduino pins, change this
{ 
  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 // pins[ 0 ] to pins[ 12 ]
};

class iobutton
{
private:
  unsigned char arduPin;
  unsigned char stateHistory; // bit 0 = now, bit 1 = prev
  unsigned long startMillis;
  unsigned long debounceMillis; 
  char buttonOut; // 5-state as below

public:
  iobutton( char, unsigned long ); // pin, debounce millis
  void startButton( void );
  void runButton( void );
  char readOutput( void ); // 3-state UNDECIDED = -1, OFF = 0, ON = 1
  // 5-state UNDECIDED = -1, OFF = 0, ON = 1, justOFF = 2, justON = 3
};

//    button ================================================

iobutton::iobutton( char ap, unsigned long dbm )
{
  arduPin = ap;
  debounceMillis = dbm;
  buttonOut = -1;
};

void iobutton::startButton( void )
{
  pinMode( arduPin, INPUT_PULLUP );
}

void iobutton::runButton( void )
{
  stateHistory &= 1;  // clears all but the last read
  stateHistory <<= 1; // shifts last read to bit 1
  stateHistory += digitalRead( arduPin ); // current state to bit 0

  switch ( stateHistory ) // set for INPUT_PULLUP 
  {
  case 0 : // low - low     pressed
  case 3 : // high - high   released
    if ( buttonOut < 0 )
    {
      if ( millis() - startMillis >= debounceMillis )
      {
        if ( stateHistory == 0 )
        {
          buttonOut = 3; // button pressed - just changed
        }
        else
        {
          buttonOut = 2; // button released - just changed
        }
      }
    }
    break;
  case 1 : // high - low   if state change, debounce!
  case 2 : // low - high 
    buttonOut = -1;
    startMillis = millis() & 0xFF;
  }
};

char iobutton::readOutput( void )
{
  if ( buttonOut < 0 )  return buttonOut;
  startMillis = buttonOut;
  if ( buttonOut > 1 )  buttonOut -= 2; // see change only once 
  return startMillis; // debounce over, it's not being used
};

//    end button ============================================

iobutton  startProcess( PROCSTART, 20 ); // make button with 20ms debounce 
iobutton  stopProcess( PROCSTOP, 20 );


byte procState;
unsigned long timerStart, timerWait;

void setDelay( unsigned long waitval )
{
    timerStart = millis();
    timerWait = waitval;
}

void inline runProc() 
{
  if ( timerWait > 0 )
  {
    if ( millis() - timerStart < timerWait )  return; // non-blocking delay
    timerWait = 0;
  }

  switch ( procState )
  {
    /*
  NULL = 0, MIXMOT, EXTRACT, CANOLA, COCO, PALM, // 0 to 5 
     PERFUME, LYE, POUR, DRYLYEMOT, INLET, // 6 to 10
     WETLYEMOT, START, STOP
     */
  case 0 :  
    digitalWrite( pins[ PERFUME ], HIGH ); //PAPAYA PERFUME
    digitalWrite( pins[ EXTRACT ], HIGH); //PAPAYA EXTRACT
    digitalWrite( pins[ CANOLA ], HIGH); //CANOLA OIL
    digitalWrite( pins[ COCO ], HIGH); //COCONUT OIL
    digitalWrite( pins[ PALM ], HIGH); //PALM OIL WITH COLORANT
    setDelay( 5000 );
    procState = 1; // run case 1 next time
    break;

  case 1 :
    digitalWrite( pins[ PERFUME ], LOW);
    digitalWrite( pins[ EXTRACT ], LOW);
    setDelay( 2000 );
    procState = 2; 
    break;

  case 2 :
    digitalWrite( pins[ CANOLA ], LOW);
    setDelay( 2000 );
    procState = 3; 
    break;

  case 3 :
    digitalWrite( pins[ COCO ], LOW);
    setDelay( 2000 );
    procState = 4; 
    break;

  case 4 :
    digitalWrite( pins[ PALM ], LOW);
    setDelay( 2000 );
    procState = 5; // run case 1 next time
    break;

  case 5 :  //MOTOR
    digitalWrite( pins[ MIXMOT ], HIGH); //MOTOR
    digitalWrite( pins[ LYE ], HIGH); //WATERnLYE
    setDelay( 5000 );
    procState = 6; // run case 1 next time
    break;

  case 6 :
    digitalWrite( pins[ LYE ], LOW);
    setDelay( 500 );
    procState = 7; // run case 1 next time
    break;

  case 7 :
    digitalWrite( pins[ MIXMOT ], HIGH);
    setDelay( 5000 );
    procState = 8; // run case 1 next time
    break;

  case 8 :
    digitalWrite( pins[ MIXMOT ], LOW);
    setDelay( 500 );
    procState = 9; // run case 1 next time
    break;

  case 9 :  //MOLDPOURING
    digitalWrite( pins[ POUR ], HIGH);
    setDelay( 10000 );
    procState = 10; // run case 1 next time
    break;

  case 10 :
    digitalWrite( pins[ POUR ], LOW);
    setDelay( 500 );
    procState = 11; // run case 1 next time
    break;

  default : // procState > 10 does nothing
  break;
  } // end of switch( procState )
}


void setup() 
{
  byte i; // index
  for ( i = 0; i < 11; i++ ) // arrays start at element 0
  {
    pinMode( pins[ i ], OUTPUT );
  }

  startProcess.startButton(); // pin mode set
  stopProcess.startButton(); // pin mode set

  /*
  NULL = 0, MIXMOT, EXTRACT, CANOLA, COCO, PALM, // 0 to 5 
   PERFUME, LYE, POUR, DRYLYEMOT, INLET, // 6 to 10
   WETLYEMOT, PROCSTART, PROCSTOP
   */
  digitalWrite( pins[ DRYLYEMOT ], LOW ); // using enums to name the pins
  digitalWrite( pins[ INLET ], LOW );
  delay(1000);   

  for ( i = 0; i < 8; i++ )
  {
    digitalWrite( pins[ i ], LOW ); // using numbers to iterate through pins
  }

  delay(1000);
}

void loop()
{
  if ( stopProcess.readOutput() == 3 ) // stop switch press detected
  {
    digitalWrite( pins[ MIXMOT ], LOW ); // using enums to name the pins
    digitalWrite( pins[ DRYLYEMOT ], LOW ); 
    digitalWrite( pins[ WETLYEMOT ], LOW ); // using enums to name the pins
    procState = 99; // quit processing
  }

  runProc();

  if ( procState > 10 ) // process not running, watch for start button
  {
    if ( startProcess.readOutput() == 3 )
    {
      procState = 0; // this starts the process
    }
  }
}

Mr Go smoke,
Thanks sir! btw, i forgot to tell you that the waterNlye PROCESS is different process, means different start and stop button..., and sir can i join the waterNlye process into the main process(USING ONE ARDUINO ATMEGA328)? like in the code your already make, but the pinouts of the arduino(atmega328) is only 13 in that case the switches will be using 4 pin outs? 2 for mainprocess and 2 for waterNlye process, i think ill be short in pinouts, ..,because I have a total of 4 switches (2 for start , and 2 for stop (1 for main process, 1 for waternlye process))

Sir there's a problem in the INPUT_PULLUP, i didn't change anything. it say it is not declared, i wonder why.., because in the statement in the code you said that "i dont care what below and above".

What version of the IDE are you using ?

arduino 1.0 connected with the arduino clone (ACEDUINO)

I run 1.05 r2.

With an older IDE you can make INPUT_HIGH by:

pinMode( pin, INPUT );
digitalWrite( pin, HIGH );

I was doing that since IDE 0022 until I jumped to 1.03.

The ACEDUINO's are variants. I see the version with the 328P has 14 I/O pins where UNO has 20.
It's the same chip. Alexan didn't put in traces for 6 pins maybe to have the solder pads there.
Be sure I won't be buying any of those!

If/when you are ready to add pins, the Alexan page does say that the ISP pins are usable on the 328;

The ISP (In System Programmer) pins are to the SPI (Serial Peripheral Interface) port. SPI is a bus (one of a few) that you can run external devices on. SPI is the high-speed bus.

You can get and connect shift registers to add pins to an Arduino. Typical cheap shift registers either take serial data and present it on their pins or read their pins as serial data. You can daisy-chain shift register chips to add many pins. Just take care, they may need external power.

You can learn to use the Arduino as an ISP and program an ATmega328P chip on a breadboard and have 20 usable pins at 16 MHz or 22 pins at 8 MHz but that is a learning curve in itself too.

Still, when you're done you will know how to make a stand-alone AVR chip which is an Arduino goal. Arduino boards are development boards. That is covered on the Arduino Fundamentals page.


For your project -----

I would have a mode selector, one button and a led.

The mode selector would be a pot connected to an analog pin. The pot dial would point to a label on a round background card. The resistance would correspond to the label. Each label would be for a different process.
The led would tell the status of the machine by on/off/blinking and blink rate.
The button would serve for both start and stop and possibly the pot could have STOP at both ends.

Yes the code would change.

You would probably change the pins[] assignments to clear an analog pin for the dial pot. Easy.

In the procState switch-case where each case changes procState would need to be array elements where are now hard coded numbers.....

  case 1 :
    digitalWrite( pins[ PERFUME ], LOW);
    digitalWrite( pins[ EXTRACT ], LOW);
    //  delay(2000);
    timerWait = 2000;
    procState = 2;        // This line would be something like:   procState = procMode[ 2 ];
    break;

In the above case, procMode would be a byte array that get filled according to what the dial is set.
This would allow the states to be run in different order depending on the chosen process.

You have to decide what happens if the dial is turned while a process is running and that has to take in consideration how easily that can happen and accidents.

There would also be more code to add but what I gave you will allow that done easier.

As it is, this project uses less than 1% of the 328P chip power. It could run at 1 MHz just as well, though the wait values would need to be divided by 16.

I just put a set of fixes for lines I forgot to put in that code. Chance it will work is now higher.

What was wrong:

When runProc() set timerWait to a value, it didn't set timerStart = millis();
It can be very hard to check an interval with the wrong start time!

So that is done, fixed in the code on page 1 of this thread.

Well anyway that's a bit of debugging done without having to run the actual code.
It really helps to have done that un-delay tactic and got it right on running machines before.