/*
*Title: My Washing Machine Arduino Program
*Author: Alexandra Rouse
*Date: 01/05/2024
*Version: v1.0
*Purpose: This code runs a miniture model of a washing machine.
*/
#include <Stepper.h>
//declare variables here!
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 1, 2, 3, 4);
#include <LiquidCrystal.h>
LiquidCrystal lcd(13, 11, 10, 9, 8, 7);
//const int redled = 13; //redled is connected to digital pin 13
//const int greybtn = 13; //greybtn is connected to digital pin 13
//Servo myServo; //declare a servo object
// LED and Power Button
int redledPin = A4; // pin for the LED signal
int greybtnPin = A5; // pin for the button signal
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// put your setup code here, to run once:
//pinMode(12, OUTPUT); //set pin 12 as an output for bluebtn
//pinMode(11, OUTPUT); //set pin 11 as an output for blueled
myStepper.setSpeed(60);
lcd.begin(16, 2); //display of 16 letters and 2 rows
// you can now interact with the LCD
pinMode(redledPin, OUTPUT);
pinMode(greybtnPin, INPUT);
}
void Poweron () { //create a function called Poweron
lcd.blink(); //Displays the blinking LCD cursor
delay(4000); //wait 4 seconds
lcd.noBlink(); // Turns off the blinking LCD cursor
lcd.print("Powered On"); //prints Powered On on the LCD
delay(2000);
lcd.setCursor(3,1); //starts text 3 blocks in on the 2nd line
lcd.print("Select washing input");
delay(2000);
lcd.clear(); //clears the screen
}
void rinsecycle () { //create a function called rinsecycle
for (int i = 0; i < 5; i++) {
lcd.clear();
lcd.print("Rinsing"); //prints Powered On on the LCD
myStepper.step(stepsPerRevolution);
delay(500); // Delay between steps, adjust as needed
}
}
void spincycle () { //create a function called spincycle
for (int i = 0; i < 11; i++) {
lcd.clear();
lcd.print("Spinning"); //prints Powered On on the LCD
myStepper.step(stepsPerRevolution);}
delay(50); // Delay between steps, adjust as needed
}
void washcycle () { //create a function called washcycle
for (int i = 0; i < 6; i++) {
for (int i = 0; i < 3; i++) {
// Step the motor 1 revolution clockwise
lcd.clear();
lcd.print("Washing"); //prints Powered On on the LCD
myStepper.step(stepsPerRevolution);
delay(500); // Delay between steps, adjust as needed
}
// Anticlockwise rotation
for (int i = 0; i < 2; i++) {
// Step the motor 1 revolution anticlockwise
myStepper.step(-stepsPerRevolution);
delay(500); // Delay between steps, adjust as needed
}
}
}
void sound() {
// Pin where the buzzer is connected
const int buzzerPin = 5;
// Notes of the melody (frequency in Hz)
const int melody[] = {
262, 294, 330, 349, 392, 440, 494, 523 // C4, D4, E4, F4, G4, A4, B4, C5
};
// Note durations: 4 = quarter note, 8 = eighth note, etc.
const int noteDuration[] = { 4, 4};
// Play the melody
for (int thisNote = 0; thisNote < 8; thisNote++) {
// Calculate the note duration in milliseconds
int noteDuration = 100 / noteDuration;
// Play the note on the buzzer
tone(buzzerPin, melody[thisNote++], noteDuration);
// Pause between notes
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// Stop the tone
noTone(buzzerPin);
}
// Wait for a while before repeating the melody
delay(200); // 0.2 seconds delay
}
void WashingCycle() {
rinsecycle();
washcycle();
rinsecycle();
spincycle();
sound();
}
void loop() {
buttonState = digitalRead(greybtnPin);
if (buttonState != lastButtonState) { // != is Arduino code for Not Equal to
if (buttonState == HIGH) { // == Checks if the value of two operands is equal or not, if yes then condition becomes true.
digitalWrite(redledPin, !digitalRead(redledPin)); // toggle the LED
Poweron();
WashingCycle();
}
}
lastButtonState = buttonState;
}