#include <ezButton.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define outputA 7
#define outputB 6
#define pushbutton 8
#define Pin_lpwm 3 //lpwm
#define Pin_rpwm 4//rpwm
#define Pin_enable 44 //pwm enable
#define buzzer 9
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
ezButton button(pushbutton);
int counter = 0;
int counters = 0;
unsigned long previous_time=0;
unsigned long current_time=0;
unsigned long previous=0;
unsigned long current=0;
unsigned long pre=0;
unsigned long curr=0;
const long onDuration = 5000;// OFF time for LED
const long offDuration = 2000;// ON time for LED
bool cond = false;
int aState;
int aLastState;
int State;
int LastState;
int number = 0;
int seconds = 10;
int Statewash =HIGH;// initial state of LED
bool statestop = false;
int minus = 0;
int rotate = 0;
long rememberTime=0;// this is used by the code//display
int time_end = 0;
bool mode = false;
bool choose = false;
bool set = false;
bool ended = false;
void setup() {
button.setDebounceTime(100);
button.setCountMode(COUNT_FALLING);
pinMode (outputA,INPUT);
pinMode (outputB,INPUT);
pinMode(buzzer, OUTPUT);
pinMode(Pin_lpwm, OUTPUT);
pinMode(Pin_rpwm, OUTPUT);
pinMode(Pin_enable, OUTPUT);
motor_stop();
Serial.begin (9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextColor(WHITE);
// Reads the initial state of the outputA
aLastState = digitalRead(outputA);
LastState = digitalRead(outputA);
display.setTextSize(2);
display.setCursor(20,2);
display.print("WASHING");
display.setCursor(20,23);
display.print("MACHINE");
display.setTextSize(1);
display.setCursor(30,42);
display.print("Controller");
display.display();
delay(3000);
display.clearDisplay();
display.setTextSize(2);
display.setCursor(8,24);
display.print("LOADING...");
display.display();
soundstart();
display.clearDisplay();
display.setTextSize(2);
display.setCursor(18,2);
display.print("Push the ");
display.setCursor(29,23);
display.print("button");
display.setCursor(29,45);
display.print("---->");
display.display();
}
void wash(){
if( Statewash ==HIGH )
{
if( (millis()- rememberTime) >= onDuration){
Statewash = LOW;// change the state of LED
rotate++;
rememberTime=millis();// remember Current millis() time
}
}
else
{
if( (millis()- rememberTime) >= offDuration){
Statewash =HIGH;// change the state of LED
rememberTime=millis();// remember Current millis() time
}
}
if(rotate == 2){
rotate = 0;
}
if(Statewash == HIGH){
if (rotate == 0){
motor_ccw();
}
else{
motor_cw();
}
}
else{
motor_stop();
}
}
void loop() {
button.loop();
unsigned long countthroughPushButton = button.getCount();
if(button.isPressed()){
tone(buzzer, 1109);
}
else{
noTone(buzzer);
}
// Reads the "current" state of the outputA
if(countthroughPushButton == 1){
display.clearDisplay();
display.setTextSize(2);
display.setCursor(39,5);
display.print("MODE");
display.setTextSize(2);
display.setCursor(5,35);
display.print("WASH");
display.setTextSize(2);
display.setCursor(85,35);
display.print("DRY");
display.drawLine(0, 25, 128, 25, WHITE);
mode = true;
statestop = true;
cond = false;
minus = 1;
State = digitalRead(outputA);
if (State != LastState){
// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (digitalRead(outputB) != State) {
counters ++;
} else {
counters --;
}
Serial.print("mode: ");
Serial.println(counters);
}
if(counters <= 0){
counters = 2;
}
else if(counters >= 2){
counters = 0;
}
LastState = State;
if(counters == 1){
display.drawLine(5, 55, 45, 55, WHITE);
}
else{
display.drawLine(85, 55, 120, 55, WHITE);
}
current = millis();
if((current - previous)>=200){
previous = current;
display.display();
}
}
if(countthroughPushButton == 2){
display.clearDisplay();
display.setTextSize(2);
display.setCursor(10,5);
display.print("SET TIMER");
display.setTextSize(2);
display.setCursor(5,35);
display.print(counter);
display.setTextSize(2);
display.setCursor(35,35);
display.print("MINUTES");
display.drawLine(0, 25, 128, 25, WHITE);
cond = false;
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
aState = digitalRead(outputA);
if (aState != aLastState){
if (digitalRead(outputB) != aState) {
counter = counter + 1;
} else {
counter = counter - 1;
}
Serial.println(counter);
}
current = millis();
if((current - previous)>=200){
previous = current;
display.display();
}
aLastState = aState;
}
if(countthroughPushButton == 3){
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
cond = true;
button.resetCount();
}
if(cond == true){
current_time=millis();
if((current_time - previous_time)>=1000){
seconds = seconds - minus;
previous_time = current_time;
Serial.print("timer: ");
Serial.print(counter);
Serial.print(":");
Serial.println(seconds);
if(counters == 1){
wash();
}
else{
motor_cw();
}
}
//
display.clearDisplay();
display.setTextSize(2);
display.setCursor(35,5);
display.print(counter);
display.setTextSize(2);
display.setCursor(70,5);
display.print(seconds);
display.setTextSize(3);
display.setCursor(56,1);
display.print(":");
display.setTextSize(2);
display.setCursor(70,40);
display.print("MODE");
display.drawLine(0, 30, 128, 30, WHITE);
if(counters ==1){
display.setTextSize(2);
display.setCursor(10,40);
display.print("WASH");
}
else{
display.setTextSize(2);
display.setCursor(15,40);
display.print("DRY");
}
if(seconds < 0){
seconds = 59;
counter--;
}
if(counter == 0 && seconds == 0){
statestop = false;
ended = true;
selesai();
}
current = millis();
if((current - previous)>=200){
previous = current;
display.display();
}//cond
}
if(counter > 15){
counter = 0;
}
if(counter < 0){
counter = 15;
}
if(statestop == false){
minus = 0;
}
}//loop end here
void motor_cw()
{
analogWrite(Pin_enable, 50);
digitalWrite(Pin_lpwm, HIGH);
digitalWrite(Pin_rpwm, LOW);
}
void motor_ccw()
{
analogWrite(Pin_enable, 50); //0-255
digitalWrite(Pin_lpwm, LOW);
digitalWrite(Pin_rpwm, HIGH);
}
void motor_stop()
{
digitalWrite(Pin_lpwm, LOW);
digitalWrite(Pin_rpwm, LOW);
analogWrite(Pin_enable, 0);
}
void soundstart(){
tone(buzzer, 831); // Send 1KHz sound signal...
delay(200);
noTone(buzzer);
delay(120);
tone(buzzer, 831); // Send 1KHz sound signal...
delay(200);
noTone(buzzer);
delay(600);
tone(buzzer, 831); // Send 1KHz sound signal...
delay(100); // ...for 1 sec
tone(buzzer, 988); // Send 1KHz sound signal...
delay(100);
tone(buzzer, 1109); // Send 1KHz sound signal...
delay(100);
noTone(buzzer);
// Stop sound...
}
void selesai(){
if (time_end != 3){
tone(buzzer,1200);
delay(500);
noTone(buzzer);
delay(500);
time_end++;
}
else{
noTone(buzzer);
}
}