//This is the code for the finalised washing machine!
//The code has been split into segments so that it is easier to read and understand.
//start of the code!
//------------------------------------------------Include|
#include <Stepper.h>
#include <ezButton.h>
#include <LiquidCrystal.h>
//------------------------------------------------LED/LCD|
#define light A4
#define medium A3
#define heavy A2
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
void LED(int on, int off1, int off2){
digitalWrite(on, HIGH);
digitalWrite(off1, LOW);
digitalWrite(off2, LOW);
}
void LCD(int led1){
lcd.setCursor(0,0);
lcd.print("Level Selected:");
lcd.setCursor(0,1);
lcd.print(led1);
}
//------------------------------------------------Buzzer|
#define NOTE_C4 262
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392
const int buzzerPin = 2;
int buzz = 0;
int melody[] = {
NOTE_C4, NOTE_E4, NOTE_F4, NOTE_G4};
int melody2[] = {
NOTE_G4, NOTE_F4, NOTE_E4, NOTE_C4};
int noteDuration = 60;
void buzzer(){
for (int i = 0; i < sizeof(melody) / sizeof(melody[0]); i++) {
tone(2, melody[i], noteDuration);
delay(noteDuration);
}
}
void buzzer2(){
for (int i = 0; i < sizeof(melody2) / sizeof(melody2[0]); i++) {
tone(2, melody2[i], noteDuration);
delay(noteDuration);
}
}
//------------------------------------------------Button|
#define SWITCH_OFF 0
#define SWITCH_ON 1
ezButton button(A1); // create ezButton object that attach to pin 9;
int switch_state = SWITCH_OFF; // initial state
void check(){ //------------------- checks if the button is pressed and changes it from 0-1/1-0
button.loop(); // MUST call the loop() function first
if (button.isPressed()) {
// change state of switch
if (switch_state == SWITCH_OFF)
switch_state = SWITCH_ON;
else
switch_state = SWITCH_OFF;
}
}
//------------------------------------------------Stepper|
int speed = 1;
int started = 0;
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 3, 4, 5, 6);
void stepper(int delayy){
if(switch_state == 1){ //if button pressed
if(buzz == 0){ //start tune
buzzer();
buzz = 1;
}
started = 1;
lcd.clear(); //set the LCD
lcd.setCursor(0,0);
lcd.print("Washing machine");
lcd.setCursor(0,1);
lcd.print("started!");
for (int step = 0; step < 200; step++){ //clockwise
if(started == 1){
myStepper.step(speed);
delay(delayy); // Delay between rotations.
check();
int step1 = step;
if(switch_state == 0 & started == 1){ //if turned off, reset stepper
myStepper.step(0-step);
step = 0;
started = 0;
buzz = 0;
buzzer2();
lcd.clear();
}
}
}
for (int step = 0; step < 200; step++){ //anticlockwise
if(started == 1){
myStepper.step(-speed);
delay(delayy);
check();
int step1 = step;
if(switch_state == 0 & started == 1){ //if turned off, reset stepper
myStepper.step(step);
step = 0;
started = 0;
buzz = 0;
buzzer2();
lcd.clear();
}
}
}
}
}
//------------------------------------------------Potentiometer|
#define pot = A0
void checkpot(){
int value = analogRead(A0);
if (value < 341){ // Lowest third of the potentiometer's range (0-340)
value = (value * 3) / 4; // Normalize to 0-255
LED(light, medium, heavy);
lcd.setCursor(0,0);
lcd.print("Level Selected:");
lcd.setCursor(0,1);
lcd.print("Light ");
stepper(7);
}else if (value < 682){ // Middle third of potentiometer's range (341-681)
value = ( (value-341) * 3) / 4; // Normalize to 0-255
LED(medium, light, heavy);
lcd.setCursor(0,0);
lcd.print("Level Selected:");
lcd.setCursor(0,1);
lcd.print("Medium");
stepper(5);
}else{ // Upper third of potentiometer"s range (682-1023)
value = ( (value-683) * 3) / 4; // Normalize to 0-255
LED(heavy, light, medium);
lcd.setCursor(0,0);
lcd.print("Level Selected:");
lcd.setCursor(0,1);
lcd.print("Heavy ");
stepper(3);
}
}
//------------------------------------------------Setup|
void setup() {
lcd.begin(16, 2);
pinMode(A0, INPUT);
}
//------------------------------------------------Loop|
void loop() {
check();
checkpot();
}