TinkerCAD washing machine problems

Hello everyone, I am trying to create a washing machine in TinkerCAD, as shown in the image below.
image

Heres the code:

//Project 02 - Washing Machine - BASIC
//This program will simulate the cycle of a washing machine
//using LEDs, including heavy duty mode

//declare constants for PINS
const int doorSwitch_PIN = 2;
const int stopPB_PIN = 3;
const int rinsePB_PIN = 4;
const int washPB_PIN = 5;
const int heavyDutyPB_PIN = 6;


const int doneLED_PIN = 8;
const int rinseLED_PIN = 9;
const int washLED_PIN = 10;
const int heavyDutyLED_PIN = 11;
const int readyLED_PIN = 12;

const int washcycletime = 5000;
const int rinsecycletime = 3000;

int mode = 0; 	//mode 0 = Standby
				//mode 1 = Ready to Start
				//mode 2 = Wash
				//mode 3 = Rinse
				//mode 4 = Cycle Complete
                                //mode 5 = Heavy Duty

void setup() 
{
  //set pins as inputs and outputs
  pinMode(doorSwitch_PIN, INPUT);
  pinMode(stopPB_PIN, INPUT);
  pinMode(rinsePB_PIN, INPUT);
  pinMode(washPB_PIN, INPUT);
  
  pinMode(doneLED_PIN, OUTPUT);
  pinMode(rinseLED_PIN, OUTPUT);
  pinMode(washLED_PIN, OUTPUT);
  pinMode(readyLED_PIN, OUTPUT);
}

void loop() 
{
  //read button states
  bool doorState = digitalRead(doorSwitch_PIN);
  bool stopPBState = digitalRead(stopPB_PIN);
  bool rinsePBState = digitalRead(rinsePB_PIN);
  bool washPBState = digitalRead(washPB_PIN);
  bool heavyDutyPBState = digitalRead(heavyDutyPB_PIN);
            
  switch (mode)
  {
    case 0: //standby
      //set LEDs
      digitalWrite(readyLED_PIN, LOW);
      digitalWrite(heavyDutyLED_PIN, LOW);
      digitalWrite(washLED_PIN, LOW);
      digitalWrite(rinseLED_PIN, LOW);
      digitalWrite(doneLED_PIN, LOW);
    
      //if door is closed start ready mode
      if (doorState == LOW)
      {
        mode = 1;
      }
      break;
    
    case 1: //ready to start
      //set LEDs
      digitalWrite(readyLED_PIN, HIGH);
      digitalWrite(heavyDutyLED_PIN, LOW);
      digitalWrite(washLED_PIN, LOW);
      digitalWrite(rinseLED_PIN, LOW);
      digitalWrite(doneLED_PIN, LOW);
          
      //if door is opened start standby mode
      if (doorState == HIGH)
      {
        mode = 0;
      }
      
      //if heavy duty button is pressed start heavy duty mode
      if (heavyDutyPBState == LOW)
      {
        mode = 5;
      }
    
      //if wash button is pressed start wash mode
      if (washPBState == LOW)
      {
        mode = 2;
      }
    
      //if rinse button is pressed start rinse mode
      if (rinsePBState == LOW)
      {
        mode = 3;
      }
      break;
    
    case 2: //regular wash
      //set LEDs
      digitalWrite(readyLED_PIN, LOW);
      digitalWrite(heavyDutyLED_PIN, LOW);
      digitalWrite(washLED_PIN, HIGH);
      digitalWrite(rinseLED_PIN, LOW);
      digitalWrite(doneLED_PIN, LOW);  
      
      //complete cycle then start rinse mode
      delay(washcycletime);
      mode = 3;
      break;
    
    case 3: //rinse
      //set LEDs
      digitalWrite(readyLED_PIN, LOW);
      digitalWrite(heavyDutyLED_PIN, LOW);
      digitalWrite(washLED_PIN, LOW);
      digitalWrite(rinseLED_PIN, HIGH);
      digitalWrite(doneLED_PIN, LOW);  

      //complete cycle then start cycle complete mode
      delay(rinsecycletime);
      mode = 4;
      break;
    
    case 4: //cycle complete
      //set LEDs
      digitalWrite(readyLED_PIN, LOW);
      digitalWrite(heavyDutyLED_PIN, LOW);
      digitalWrite(washLED_PIN, LOW);
      digitalWrite(rinseLED_PIN, LOW);
      digitalWrite(doneLED_PIN, HIGH);
        
      //if door is opened or stop button is pressed start standby mode
      if ((doorState == HIGH) || (stopPBState == LOW))
      {
        mode = 0;
      }
      break;
    
    case 5: //heavy duty
      //set LEDs
      digitalWrite(readyLED_PIN, LOW);
      digitalWrite(heavyDutyLED_PIN, HIGH);
      digitalWrite(washLED_PIN, LOW);
      digitalWrite(rinseLED_PIN, LOW);
      digitalWrite(doneLED_PIN, LOW);  

      //complete cycle then start rinse mode
      delay(washcycletime*2);
      mode = 3;
      break;
  }
  delay(10);
}

Now what I need to do is find a way for the stop button and door switch to work at any time during the wash or rinse cycles. The version you're seeing right now doesn't do that sadly. One person told me to look at the "Blink Without Delay" in Arduino Starter, but that doesn't seem to be giving me any hints at all. I greatly appreciate any help that you can provide me, thanks!

You can not use the delay() function. That is the point behind the Blink Without Delay example. You need to let loop() keep running so you can very rapidly track your input buttons along with tracking the elapsed time for your cycle.

It would be good to implement this as a state machine. Keep track of which "state" you are in such as "ready, washing, rinsing, etc." and then, based on which state you are in, you see how long you have been in that state and the finish. You can also look at the several things at the same time

Hello
Well, I guess, it is most common to use a time handler and a smart FSM to do this control job.

oh alright, I will try that thanks!

oh ok I will try that as well!!

Your code is already pretty close.... This should get you closer...
Also note: by defining an ENUM, you can change your mode values from numbers into more human meaningful constants.

//Project 02 - Washing Machine - BASIC
//This program will simulate the cycle of a washing machine
//using LEDs, including heavy duty mode

//declare constants for PINS
const int doorSwitch_PIN = 2;
const int stopPB_PIN = 3;
const int rinsePB_PIN = 4;
const int washPB_PIN = 5;
const int heavyDutyPB_PIN = 6;

const int doneLED_PIN = 8;
const int rinseLED_PIN = 9;
const int washLED_PIN = 10;
const int heavyDutyLED_PIN = 11;
const int readyLED_PIN = 12;

const unsigned long washcycletime = 5000;
const unsigned long rinsecycletime = 3000;
unsigned long startTime;

enum { STANDBY, READY, WASH, RINSE, DONE, HEAVY_DUTY };

int mode = 0;   //mode 0 = Standby
//mode 1 = Ready to Start
//mode 2 = Wash
//mode 3 = Rinse
//mode 4 = Cycle Complete
//mode 5 = Heavy Duty

void setup()
{
  //set pins as inputs and outputs
  pinMode(doorSwitch_PIN, INPUT);
  pinMode(stopPB_PIN, INPUT);
  pinMode(rinsePB_PIN, INPUT);
  pinMode(washPB_PIN, INPUT);

  pinMode(doneLED_PIN, OUTPUT);
  pinMode(rinseLED_PIN, OUTPUT);
  pinMode(washLED_PIN, OUTPUT);
  pinMode(readyLED_PIN, OUTPUT);
}

void loop()
{
  //read button states
  bool doorState = digitalRead(doorSwitch_PIN);
  bool stopPBState = digitalRead(stopPB_PIN);
  bool rinsePBState = digitalRead(rinsePB_PIN);
  bool washPBState = digitalRead(washPB_PIN);
  bool heavyDutyPBState = digitalRead(heavyDutyPB_PIN);

  switch (mode)
  {
    case STANDBY: //standby
      //set LEDs
      digitalWrite(readyLED_PIN, LOW);
      digitalWrite(heavyDutyLED_PIN, LOW);
      digitalWrite(washLED_PIN, LOW);
      digitalWrite(rinseLED_PIN, LOW);
      digitalWrite(doneLED_PIN, LOW);

      //if door is closed start ready mode
      if (doorState == LOW)
      {
        mode = READY;
      }
      break;

    case READY: //ready to start
      //set LEDs
      digitalWrite(readyLED_PIN, HIGH);
      digitalWrite(heavyDutyLED_PIN, LOW);
      digitalWrite(washLED_PIN, LOW);
      digitalWrite(rinseLED_PIN, LOW);
      digitalWrite(doneLED_PIN, LOW);

      //if door is opened start standby mode
      if (doorState == HIGH)
      {
        mode = STANDBY;
        return;
      }

      //if heavy duty button is pressed start heavy duty mode
      if (heavyDutyPBState == LOW)
      {
        mode = HEAVY_DUTY;
        startTime = millis();
      }

      //if wash button is pressed start wash mode
      if (washPBState == LOW)
      {
        mode = WASH;
        startTime = millis();
      }

      //if rinse button is pressed start rinse mode
      if (rinsePBState == LOW)
      {
        mode = RINSE;
        startTime = millis();
      }
      break;

    case WASH: //regular wash
      //set LEDs
      digitalWrite(readyLED_PIN, LOW);
      digitalWrite(heavyDutyLED_PIN, LOW);
      digitalWrite(washLED_PIN, HIGH);
      digitalWrite(rinseLED_PIN, LOW);
      digitalWrite(doneLED_PIN, LOW);

      if ( millis() - startTime >= washcycletime ) {
        //complete cycle then start rinse mode
        mode = RINSE;
        startTime = millis();
      }
      break;

    case RINSE: //rinse
      //set LEDs
      digitalWrite(readyLED_PIN, LOW);
      digitalWrite(heavyDutyLED_PIN, LOW);
      digitalWrite(washLED_PIN, LOW);
      digitalWrite(rinseLED_PIN, HIGH);
      digitalWrite(doneLED_PIN, LOW);

      if ( millis() - startTime >= rinsecycletime ) {
      //complete cycle then start cycle complete mode
        mode = DONE;
      }
      break;

    case DONE: //cycle complete
      //set LEDs
      digitalWrite(readyLED_PIN, LOW);
      digitalWrite(heavyDutyLED_PIN, LOW);
      digitalWrite(washLED_PIN, LOW);
      digitalWrite(rinseLED_PIN, LOW);
      digitalWrite(doneLED_PIN, HIGH);

      //if door is opened or stop button is pressed start standby mode
      if ((doorState == HIGH) || (stopPBState == LOW))
      {
        mode = STANDBY;
      }
      break;

    case HEAVY_DUTY: //heavy duty
      //set LEDs
      digitalWrite(readyLED_PIN, LOW);
      digitalWrite(heavyDutyLED_PIN, HIGH);
      digitalWrite(washLED_PIN, LOW);
      digitalWrite(rinseLED_PIN, LOW);
      digitalWrite(doneLED_PIN, LOW);

      if ( millis() - startTime >= washcycletime * 2 ) {
        //complete cycle then start rinse mode
        mode = RINSE;
        startTime = millis();
      }
      break;
  }
  delay(10);
}

This code still does not react to a stop button, but you can add that code right after the button reads. What should happen when the stop button is pressed? Go back to STANDBY?

yes it should go back to standby, according to how I made it. I shall try this code, thank you so much!!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.