Please help me.... to solve my case....

Dear All,

Please help me to make sketch with the case below..

I use 1 Push Button and 2 relay ( Relay A & B)

The case is

If I push the button switch continuously the Relay A will be ON for 1 minute only and than Relay B will be ON for 30 second only, and this is not repetition...

If in the middle of process the push button was not pressed the process of Relay A and B will be stop and canceled.

and if I need to re start the process again, I have to repeat to push the switch...

Thanks

Regards

Gus Thomas

What have you done so far?

I was use the sketch below… When I use this sketch the process always repeated

Please help me…

if (doorSwitch==0)

{

digitalWrite(6, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(5,LOW);
delay(6000); }

if(doorSwitch==1)

{
digitalWrite(6, LOW); /
digitalWrite(5,HIGH);
delay(3000);

if(doorSwitch==1)
{ digitalWrite(6, LOW);
digitalWrite(5,LOW);}

That’s not a sketch; it won’t compile.

Please use code tags when posting your code.

Your opening brackets, and closing brackets don't match up.

There are 1000ms in a second, so 1 minute is 60000ms, not 6000ms.

1 - you should keep state to see if key is pressed and for how long.
2 - learn to code without delay() (check blink without delay example)

int state = 0;
uint32_t starttime = 0;

void setup()
{
 ...
}

void loop()
{
  doorSwitch = digitalRead(DOORPIN);

  if (doorSwitch == LOW && state == 0)
  {
    state = 1;
    starttime = millis();
  }
  if (doorSwitch == HIGH && state == 1)
  {
    state = 0;
  }

  if (state == 1 && millis() - starttime < 60000UL)
  {
    digitalWrite(RELAYA, HIGH);
    digitalWrite(RELAYB, LOW);
  }
  else if (state == 1 && millis() - starttime < 90000UL)
  {
    digitalWrite(RELAYA, LOW);
    digitalWrite(RELAYB, HIGH);
  }
  else
  {
    digitalWrite(RELAYA, LOW);
    digitalWrite(RELAYB, LOW);
  }

UL forces the compiler to use unsigned long (uint32_t)

Thanks for your Sketch,

It works but I need the process like below :

When I push the Door Switch… the Pin 5 will be ON for 6 minutes and after than PIN 5 will be OFF after than the Pin 6 will be ON for 3 minutes… and than all Pin will be OFF until I re-push the button.

If I use the sketch below the process is different… I need for the first Pin the time when ON is more longer than the second pin.

This is the sketch I was use …
Please help me to solve this problem…

int doorSwitch = A1;
int state = 0;
uint32_t starttime = 0;

 
void setup()
{
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(A1,INPUT);
 
  
}

void loop()
{
  doorSwitch = digitalRead(A1);

  if (doorSwitch == LOW && state == 0)
  {
    state = 1;
    starttime = millis();
  }
  if (doorSwitch == HIGH && state == 1)
  {
    state = 0;
  }

  if (state == 1 && millis() - starttime < 1000UL)
  {
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);
  }
  else if (state == 1 && millis() - starttime < 6000UL)
  {
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
  }
  else
  {
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
  }  }

I’m already try to swap the value of starting time but it does’t works like I need…

Best is to take paper and pencil and write down , step by step, what you should do in what order if you where the computer.

step 1: set every relay in off mode step 2: wait until the door opens. step 3: ... etc

You appear to need 3 states

  1. Turn both outputs off and wait for button to be pressed
  2. When the button is first pressed
  3. When the first output goes off and the second one goes on
    then back to 1) when the second output goes off.

Look up ‘Switch case’ in the reference page of this site. It will make your coding easier.

Thanks for you all support…

I’m already try to modify the sketch and it works… and now I want to improve this case may be somebody have idea… this control I use to make dis washing machine control. and the improvement I need is below…

This machine have 3 program with different time to choose and with different button too.

Program 1

  • Wash Time is 60 second (wash pump ON)
  • Rinse Time is 12 second (rinse pump ON + Solenoid Valve Water Inlet ON)

Program 2

  • Wash Time is 90 second (wash pump ON)
  • Rinse Time is 15 second (rinse pump ON + Solenoid Valve Water Inlet ON)

Program 3

  • wash Time is 120 second (wash pump ON)
  • Rinse Time is 20 second (rinse pump ON + Solenoid Valve Water Inlet ON)

The Process of the machine I need is :

When Machine ON :

  • Solenoid will be goes ON to fill the boiler and tank and the program can’t be start until the boiler and tank full, and solenoid will be work if the door closed, if the door open the filling process will be stop.
  • After Boiler and tank full the heating element will be On until the set point reached. If the tank Low the heating automatic OFF.
  • If TANK and Boiler Full… I can use which program I was choose…

This is the sketch I was make :

#include <OneWire.h>
#include <Wire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#define ONE_WIRE_BUS A0

#define I2C_ADDR    0x3F // <<----- Kode LCD Didapat dari I2C Scanner
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7
LiquidCrystal_I2C  lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

float tempC;
float temperatureBoiler;

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);


DeviceAddress boilerTemperature = {0x28, 0xFF, 0x10, 0x49, 0x69, 0x14, 0x04, 0xDA };
DeviceAddress tankTemperature = { 0x28, 0xFF, 0xB5, 0x2C, 0x69, 0x14, 0x04, 0x2A };

int doorSwitch = A2;
int doorSwitch1 = A3;
int state = 0;
uint32_t starttime = 0;



void setup(void)
{  //Switch on the backlight
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(LOW);

  pinMode(12, OUTPUT);      //Output Mode for Boiler Heater
  pinMode(11, OUTPUT);      //Output Mode for Tank Heater
  pinMode(10, OUTPUT);      //Output Solenoid Valve for Water Fill
  pinMode(9,OUTPUT);        //Output for Wash Pump = wash time
  pinMode(8,OUTPUT);        //Output for Rinse Pump + Solenoid Valve
  pinMode(A1, INPUT);       // Input Floating Switch to control Boiler and Tank Volume, 
  pinMode(A2,INPUT);        // Input Door Switch
  pinMode(A3,INPUT);

digitalWrite(12,HIGH);
digitalWrite(11,HIGH);
digitalWrite(10,HIGH);
digitalWrite(9,HIGH);
digitalWrite(8,HIGH);
  
// Start up the library
sensors.begin();
// set the resolution to 10 bit (good enough?)
sensors.setResolution(boilerTemperature, 10);
sensors.setResolution(tankTemperature, 10);








lcd.begin(20,4); // columns, rows. use 16,2 for a 16x2 LCD, etc.


//--------------------------------

 
//----------------------------------
}



void loop(void)
{ 
sensors.requestTemperatures();
bool floatingSwitch = digitalRead(A1);
doorSwitch = digitalRead(A2);
doorSwitch1 = digitalRead(A3);

float BoilerTemp = sensors.getTempCByIndex(0);
float TankTemp = sensors.getTempCByIndex(1); 



lcd.setCursor(0, 1);
        lcd.print("Tank  : ");
        lcd.print(sensors.getTempCByIndex(0));
        lcd.print("C");
        
        lcd.setCursor(0, 2);
        lcd.print("Boil  : ");
        lcd.print(sensors.getTempCByIndex(1));
        lcd.print("C");

        lcd.setCursor(0, 0);
        lcd.print("Water : ");
        
        lcd.setCursor(0,3 );
        lcd.print("Status: ");
        

   if (floatingSwitch==1)
      { 
        digitalWrite(10,LOW);
        digitalWrite(12,HIGH);
        digitalWrite(11,HIGH);
        lcd.setCursor(8,0);
        lcd.print ("Water Fill");
        lcd.setCursor(16, 1);
        lcd.print("OFF");
        lcd.setCursor(16, 2);
        lcd.print("OFF");  
      }
        
     else if(floatingSwitch==0) 
      { 
        lcd.setCursor(8,0);
        lcd.print ("Water Full");
        digitalWrite(10,HIGH);
       
    if(BoilerTemp < 30)
            {
                lcd.setCursor(16, 1);
                lcd.print("ON  ");
                digitalWrite(12,LOW);
            }
         else if(BoilerTemp > 30)
            {
                lcd.setCursor(16, 1);
                lcd.print("OFF");
                digitalWrite(12,HIGH);
            } 
    

        if(TankTemp < 30)
            {
                lcd.setCursor(16, 2);
                lcd.print("ON  ");
                digitalWrite(11,LOW);
            }
        else if(TankTemp > 30)
            {
                lcd.setCursor(16, 2);
                lcd.print("OFF");
                digitalWrite(11,HIGH);
            } 
      }

         
  

 

  if (doorSwitch == LOW && state == 0)
  {
    state = 1;
    starttime = millis();
    starttime = millis ();
    
  }

     if (doorSwitch == 1 && floatingSwitch == 0)
     {lcd.setCursor(8,3);
     lcd.print ("Ready Use !"); 
     lcd.setCursor(8,3);
     lcd.print ("Door Open !");}
    
  if (doorSwitch == 0 && floatingSwitch == 0)
      {lcd.setCursor(8,3);
     lcd.print ("           ");}


  
  if (doorSwitch == HIGH && state == 1 )
  {
    state = 0;
   
  }

  if (state == 1 && millis() - starttime < 3000UL)
  {
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
    lcd.setCursor(8,3);
    lcd.print ("Wash Cycle");
   
  }
  else if (state == 1 && millis() - (starttime + 4000) < 2000UL)
  {
    digitalWrite(8, LOW);
    digitalWrite(9, HIGH);
    lcd.setCursor(8,3);
    lcd.print ("Rinse      ");  
   
  }
  else
  {
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
   
  }}

May be somebody can improve my project…

Thanks

use CTRL-T in the IDE for auto-formatting your code, improves readability.

You definitely need a "state machine" program to do this. I've never seen an online tutorial that explains it to my satisfaction but Grumpy Mike's tutorial is the best I know of.

Draw the states out on a piece of paper. Each state has a defined set of outputs (eg 'boiler heat on') and it cannot leave that state unless a specific condition is met. If the boiler reaches temperature, then it moves to another state ('wash'?) or it may leave that state if the 'cancel' button is pressed (to 'idle/wait'.)

When you include things like cancel buttons the state diagram rapidly gets complex but it's a much better way of organising your thinking about the system because ordinarily you wouldn't think to check the cancel button while the boiler is heating.