Close

Test Code

A project log for Washing Machine Conversion

Converting a Manual switched washing machine in to a Arduino powered smart washer.

boris-van-galvinBoris van Galvin 03/08/2017 at 09:230 Comments

Now for some code, I will add the final version later as there are a pile of additional timer routines but for now I am testing the stability of the system. There seems to still be some RFI issues when the spin cycle ends and the actuator drops the brake but I also ran out of parts to make a snubber for the actuator so this fault should go away once the additional snubber is n place.

Yes i know its full of while loops but I wanted this to be as simple as possible for testing purposes.

#include <Bounce2.h>

// Constants for Control Pins
const int LedPin =  13;         // LED Pin
                                // Washingmachine Sensor Pins
const int WaterLevelPin = 2;    // N/C Switch to sense the water level If it goes open then stop everyting.
const int LindSwitchPin = 3;    // Goes OPEN circut if the lit is opend. E-STOP
const int SpinSensorPin = 12;   // When the spin locking pin is in place this is short
                                // IN# are the relay inputs for the Duinotech 8 Channel Board
const int CWPin = 4;            // IN1 - Clockwise Pin
const int CCWPin = 5;           // IN2 - Counter Clockwise / FAST SPIN Pin.
const int SpinActuatorPin = 6;  // IN3 - Pull the locking pin in for the spin cycle
const int ColdWaterValve = 7;   // IN4 - Cold Water Valve
const int HotWaterValve = 8;    // IN5 - Cold Water Valve
const int WaterPumpPin = 9;     // IN6 - Water Pump
                                // Control Pannel Pins
const int StartPin = 10;        // Restart After and Error
const int RestartPin = 11;      // Restart After and Error

// Control Variables
int ledState = LOW;             // ledState used to set the LED
int CWState = LOW;              // Clockwise Relay State
int CCWState = LOW;             // Counter Clockwise Relay State
int SpinState = LOW;            // Spint Actuator State
int ColdState = LOW;            // Cold Water Valve State
int HotState = LOW;             // Hot Water Valve State
int PumpState = LOW;            // Water Pump State
int incomingByte = 0;

int MachineCycle = 0;
int WaterLevel = 0;
int SpinEnabled = 0;
long AgitationCounter = 0;
long BallanceCounter = 0;
long RinseCounter = 0;
long SpinCounter = 0;
long EmptyWater = 0;
long SoakCounter = 0;


// Instantiate a Bounce object
Bounce debouncer = Bounce();

void setup() {
  Serial.begin(9600);                   // Serial Communications for Debuging
  pinMode(StartPin,INPUT_PULLUP);       // Pin to use for the Start Button
  debouncer.attach(StartPin);           // After setting up the button, setup the Bounce instance :
  debouncer.interval(40);                // interval in ms

 // set the digital pin as output:
  pinMode(LedPin, OUTPUT);              // Onboard LED on pin 13
  pinMode(WaterLevelPin, INPUT_PULLUP); // Enable Pullup for WaterLevel Sensor
  pinMode(LindSwitchPin, INPUT_PULLUP); // Enable Pullup for Lid Switch
  
  pinMode(CWPin, OUTPUT);               // Clockwise Motor Control
  pinMode(CCWPin, OUTPUT);              // Counter Closkwise Motor Control
  pinMode(SpinActuatorPin, OUTPUT);     // Spin Actuator Solinoid
  pinMode(ColdWaterValve, OUTPUT);      // Cold Water Valve
  pinMode(HotWaterValve, OUTPUT);       // Hot Water Valve
  pinMode(WaterPumpPin, OUTPUT);        // Water Pump
  pinMode(StartPin, INPUT_PULLUP);      // Start Button
  pinMode(RestartPin, INPUT_PULLUP);    // Restart/Resume after Error
  pinMode(SpinSensorPin, INPUT_PULLUP); // Enable Pullup for Spin Sensor
  
}

void loop() {
  debouncer.update();                   // Update the Bounce instance :
  
  int value = debouncer.read();         // Get the updated value :
  if ( value == LOW ) {
    Serial.println("Washing Cycle Started");
    MachineCycle = 1;                    // If the button is pushed start the machine  
  }

   // send data only when you receive data:
  if (Serial.available() > 0) {
    incomingByte = Serial.read();       // read the incoming byte:
    Serial.print("I received: ");       // say what you got:
    Serial.println(incomingByte, DEC);
  }

if(incomingByte == 48){
  Serial.println("Stop Machine");
  MachineCycle = 0;
}
  // If we receive a 1 run this
  if(incomingByte == 49){
    Serial.println("Start Machiner");
    MachineCycle = 1;
  }

  if(MachineCycle == 1){

  Serial.println("Filling Machine with Cold Water for Wash Cycle");
    while(digitalRead(WaterLevelPin) != 1){
      //Fill the machine
          digitalWrite(LedPin, HIGH);                   //Start Filing
          digitalWrite(ColdWaterValve, HIGH);           //turn on the cold water valve
    }
    if(digitalRead(WaterLevelPin) == 1){
          digitalWrite(LedPin, LOW);                   //Start Filing
          digitalWrite(ColdWaterValve, LOW);           //turn on the cold water valve
          Serial.println("Machine filled with water");
          WaterLevel = 1;
    }
    //3 minutes = 180000
    Serial.println("Agitating Washing");
AgitationCounter = 0;
    while(AgitationCounter <= 80){
        digitalWrite(CWPin, HIGH);
        digitalWrite(CCWPin, LOW);
        digitalWrite(LedPin, HIGH);
       // Serial.println("Agitate CW");
        delay(800);
        digitalWrite(CWPin, LOW);
        digitalWrite(CCWPin, LOW);
        digitalWrite(LedPin, LOW);
      //  Serial.println("PAUSE");
        delay(800);
        digitalWrite(CWPin, LOW);
        digitalWrite(CCWPin, HIGH);
        digitalWrite(LedPin, HIGH);
       // Serial.println("Agitate CCW");
        delay(800);
        digitalWrite(CWPin, LOW);
        digitalWrite(CCWPin, LOW);
        digitalWrite(LedPin, LOW);
      //  Serial.println("PAUSE");
        delay(800);
        AgitationCounter++;
//Serial.println("Agitating Washing");
//Serial.println(AgitationCounter);
    }


Serial.println("Soaking for 1 minute");
SoakCounter = 0;
    while(SoakCounter != 60){
        delay(1000);
        SoakCounter++;
    }
AgitationCounter = 0;
    Serial.println("Agitating after soak");
    while(AgitationCounter <= 60){
        digitalWrite(CWPin, HIGH);
        digitalWrite(CCWPin, LOW);
        digitalWrite(LedPin, HIGH);
//        Serial.println("Agitate CW");
        delay(700);
        digitalWrite(CWPin, LOW);
        digitalWrite(CCWPin, LOW);
        digitalWrite(LedPin, LOW);
//        Serial.println("PAUSE");
        delay(700);
        digitalWrite(CWPin, LOW);
        digitalWrite(CCWPin, HIGH);
        digitalWrite(LedPin, HIGH);
//        Serial.println("Agitate CCW");
        delay(700);
        digitalWrite(CWPin, LOW);
        digitalWrite(CCWPin, LOW);
        digitalWrite(LedPin, LOW);
//        Serial.println("PAUSE");
        delay(700);
        AgitationCounter++;
//Serial.println("Agitating Washing");
//Serial.println(AgitationCounter);
    } 
    Serial.println("Preparing to Spin");
    while(digitalRead(SpinSensorPin) != 0){
        digitalWrite(SpinActuatorPin, HIGH);
//        Serial.println(digitalRead(SpinSensorPin));
    }
    //This will deal with the weird bounce issue from the actuator
    while(digitalRead(SpinSensorPin) != 0){
        delay(1000); 
    } 
    if(digitalRead(SpinSensorPin) == 0){
      SpinEnabled = 1;
      digitalWrite(LedPin, HIGH);
//      Serial.println("Spin Enabled");
    }
      Serial.println("Ballance Load");
BallanceCounter = 0;
    while(BallanceCounter <= 10){
      delay(1000);
      digitalWrite(CWPin, HIGH);
      digitalWrite(LedPin, HIGH);
      delay(1500);
      digitalWrite(CWPin, LOW);
      digitalWrite(LedPin, LOW);
      BallanceCounter++;      
    }
// Start emptying the water
      Serial.println("Ballance Load and Drain");
EmptyWater = 0;
    while(EmptyWater != 70){
      digitalWrite(WaterPumpPin, HIGH);
      delay(1000);
      digitalWrite(CWPin, HIGH);
      digitalWrite(LedPin, HIGH);
      delay(1000);
      digitalWrite(CWPin, LOW);
      digitalWrite(LedPin, LOW);
      EmptyWater++;
    }
// Start a Full Speed Spin Cycle
      Serial.println("Spin and Drain");
SpinCounter = 0;
    while(SpinCounter != 70){
      digitalWrite(CWPin, HIGH);
      digitalWrite(LedPin, HIGH);
      delay(1000);
      SpinCounter++;
    }
// Start Spin Rinse
      Serial.println("Spin and Rinse");
RinseCounter = 0;
    while(RinseCounter != 40){
      digitalWrite(ColdWaterValve, HIGH);
      delay(1000);
      digitalWrite(ColdWaterValve, LOW);
      delay(1000);
      RinseCounter++;
    }
    //Make sure the water is OFF
    digitalWrite(ColdWaterValve, LOW);
    
// Finish high speed spin
SpinCounter = 0;
      Serial.println("Final Spin");
    while(SpinCounter != 70){
      digitalWrite(CWPin, HIGH);
      digitalWrite(LedPin, HIGH);
      delay(1000);
      SpinCounter++;
    }
// Spin Down and turn pump off
SpinCounter = 0;
      Serial.println("Slow Down");
    while(SpinCounter != 20){
      digitalWrite(CWPin, LOW);
      digitalWrite(LedPin, LOW);
      digitalWrite(WaterPumpPin, LOW);
      digitalWrite(ColdWaterValve, LOW);
      delay(1000);
      SpinCounter++;
    }
// Release Spin Actuator    
digitalWrite(SpinActuatorPin, LOW);
//Reset the water level
WaterLevel = 0;
//MachineCycle = 2;

Serial.println("Washing Machine Stopped"); 
Serial.println("Go top the final wash cycle"); 

  Serial.println("Filling Machine with Cold Water for Final Wash Cycle");
    while(digitalRead(WaterLevelPin) != 1){
      //Fill the machine
          digitalWrite(LedPin, HIGH);                   //Start Filing
          digitalWrite(ColdWaterValve, HIGH);           //turn on the cold water valve
    }
    if(digitalRead(WaterLevelPin) == 1){
          digitalWrite(LedPin, LOW);                   //Start Filing
          digitalWrite(ColdWaterValve, LOW);           //turn on the cold water valve
          Serial.println("Machine filled with water");
          WaterLevel = 1;
    }

    //3 minutes = 180000
    Serial.println("Agitating Washing");
AgitationCounter = 0;
    while(AgitationCounter <= 80){
        digitalWrite(CWPin, HIGH);
        digitalWrite(CCWPin, LOW);
        digitalWrite(LedPin, HIGH);
//        Serial.println("Agitate CW");
        delay(700);
        digitalWrite(CWPin, LOW);
        digitalWrite(CCWPin, LOW);
        digitalWrite(LedPin, LOW);
//        Serial.println("PAUSE");
        delay(700);
        digitalWrite(CWPin, LOW);
        digitalWrite(CCWPin, HIGH);
        digitalWrite(LedPin, HIGH);
//        Serial.println("Agitate CCW");
        delay(700);
        digitalWrite(CWPin, LOW);
        digitalWrite(CCWPin, LOW);
        digitalWrite(LedPin, LOW);
//        Serial.println("PAUSE");
        delay(700);
        AgitationCounter++;
//Serial.println("Agitating Washing");
//Serial.println(AgitationCounter);
    }


Serial.println("Soaking for 1 minute");
SoakCounter = 0;
    while(SoakCounter != 12){
        delay(1000);
        SoakCounter++;
    }

    
AgitationCounter = 0;
    Serial.println("Agitating after soak");
    while(AgitationCounter <= 60){
        digitalWrite(CWPin, HIGH);
        digitalWrite(CCWPin, LOW);
        digitalWrite(LedPin, HIGH);
//        Serial.println("Agitate CW");
        delay(700);
        digitalWrite(CWPin, LOW);
        digitalWrite(CCWPin, LOW);
        digitalWrite(LedPin, LOW);
//        Serial.println("PAUSE");
        delay(700);
        digitalWrite(CWPin, LOW);
        digitalWrite(CCWPin, HIGH);
        digitalWrite(LedPin, HIGH);
//        Serial.println("Agitate CCW");
        delay(700);
        digitalWrite(CWPin, LOW);
        digitalWrite(CCWPin, LOW);
        digitalWrite(LedPin, LOW);
//        Serial.println("PAUSE");
        delay(700);
        AgitationCounter++;
//Serial.println("Agitating Washing");
//Serial.println(AgitationCounter);
    }
  
    Serial.println("Preparing to Spin");
    while(digitalRead(SpinSensorPin) != 0){
        digitalWrite(SpinActuatorPin, HIGH);
//        Serial.println(digitalRead(SpinSensorPin));
    }
    //This will deal with the weird bounce issue from the actuator
    while(digitalRead(SpinSensorPin) != 0){
        delay(1000); 
    } 
    if(digitalRead(SpinSensorPin) == 0){
      SpinEnabled = 1;
      digitalWrite(LedPin, HIGH);
//      Serial.println("Spin Enabled");
    }
      Serial.println("Ballance Load");
BallanceCounter = 0;
    while(BallanceCounter <= 10){
      delay(1000);
      digitalWrite(CWPin, HIGH);
      digitalWrite(LedPin, HIGH);
      delay(1000);
      digitalWrite(CWPin, LOW);
      digitalWrite(LedPin, LOW);
      BallanceCounter++;      
    }
// Start emptying the water
      Serial.println("Ballance Load and Drain");
EmptyWater = 0;
    while(EmptyWater != 80){
      digitalWrite(WaterPumpPin, HIGH);
      delay(1000);
      digitalWrite(CWPin, HIGH);
      digitalWrite(LedPin, HIGH);
      delay(1000);
      digitalWrite(CWPin, LOW);
      digitalWrite(LedPin, LOW);
      EmptyWater++;
    }
// Start a Full Speed Spin Cycle
      Serial.println("Spin and Drain");
SpinCounter = 0;
    while(SpinCounter != 80){
      digitalWrite(CWPin, HIGH);
      digitalWrite(LedPin, HIGH);
      delay(1000);
      SpinCounter++;
    }
// Start Spin Rinse
      Serial.println("Spin and Rinse");
RinseCounter = 0;
    while(RinseCounter != 20){
      digitalWrite(ColdWaterValve, HIGH);
      delay(1000);
      digitalWrite(ColdWaterValve, LOW);
      delay(1000);
      RinseCounter++;
    }
    //Make sure the water is OFF
    digitalWrite(ColdWaterValve, LOW);
    
// Finish high speed spin
SpinCounter = 0;
      Serial.println("Final Spin");
    while(SpinCounter != 60){
      digitalWrite(CWPin, HIGH);
      digitalWrite(LedPin, HIGH);
      delay(1000);
      SpinCounter++;
    }
    
// Spin Down and turn pump off
SpinCounter = 0;
      Serial.println("Slow Down");
    while(SpinCounter != 20){
      digitalWrite(CWPin, LOW);
      digitalWrite(LedPin, LOW);
      digitalWrite(WaterPumpPin, LOW);
      digitalWrite(ColdWaterValve, LOW);
      delay(1000);
      SpinCounter++;
    }
// Release Spin Actuator    
digitalWrite(SpinActuatorPin, LOW);
//Reset the water level
WaterLevel = 0;
MachineCycle = 0;
Serial.println("Washing Machine Stopped"); 
  }//Machine == 2

}

Discussions