Can someone Please help me solve this issue regarding my TinkerCad Sketch?
// C++ code
//
#include <LiquidCrystal_I2C.h>
#include <IRremote.h>
#define LEDG 2
#define BUZZER 3
#define IRPIN 4
#define VMOTOR 5
#define PINGPIN 10
#define DCMOTOR 11
#define SENSORM A1
//define motor rotation int
int rotate;
// LCD setup
LiquidCrystal_I2C lcd(0x26, 16, 2);
// set up variable for ultrasonic distance sensor
int dur, dist;
//Predefined Moisture levels
const int LEAKRISK =17;
const int LEAKDETECTED=40;
//Predefined Water Levels
const int RISKLVL = 30;
const int MINLVL = 60;
//Define 2 Buttons for controlling washing mahine
const int BTN0 = 0X0C;
const int BTN1 = 0X10;
int t = 0;
int x = 0;
void setup()
{
Serial.begin(115200);
pinMode(LEDG, OUTPUT); //LED
pinMode(BUZZER, OUTPUT); //Buzzer
IrReceiver.begin(IRPIN, ENABLE_LED_FEEDBACK); //IR Receiver
pinMode(VMOTOR,OUTPUT); //Vibration Motor
pinMode(DCMOTOR,OUTPUT); //DC Motor
lcd.init(); // initialize LCD
lcd.backlight(); // Turn on LCD
}
void loop()
{
digitalWrite(LEDG, LOW);
if (IrReceiver.decode()){
int act = IrReceiver.decodedIRData.command;
IrReceiver.resume();
switch (act){
case BTN0:
if(x==2){
normalWash();
x++;
t=1;
}
else if (x==3){
delicateWash();
x=1;
t=1;
}
else{
quickWash();
x=2;
t=1;
}
break;
case BTN1:
reset();
t=1;
x=0;
break;
}
}
else
;
monitoring();
t++;
//To start the Washing
if (t>30 && x!=0){
Serial.println("WASHING STARTED");
switch (x){
case 2:
quickWashCycle();
x=0;
t=0;
break;
case 3:
normalWashCycle();
x=0;
t=0;
break;
case 1:
delicateWashCycle();
x=0;
t=0;
break;
}
}
else
;
if (x==0)
;
else
delay(400);
}
//-------------------------Conditional Response----------------------------
//Alert sound
void softbeep(){
tone(BUZZER, 1000, 120);
delay(150);
tone(BUZZER, 1400, 150);
delay(180);
noTone(BUZZER);
}
//Warning response
void warning(){
int moisture = map(analogRead(SENSORM), 0, 876, 0, 100);
if (readWaterLevel() <= MINLVL){
waterWarning();
}
else if (moisture >= LEAKRISK){
moistureWarning();
}
for(int i=0;i<2;i++){
tone(BUZZER, 900);
delay(200);
noTone(BUZZER);
delay(200);
}
digitalWrite(VMOTOR, LOW);
analogWrite(DCMOTOR, 0);
delay(500);
}
//Emergency response
void emergency(){
analogWrite(DCMOTOR, 0);
int moisture = map(analogRead(SENSORM), 0, 876, 0, 100);
if (readWaterLevel() <= RISKLVL){
waterLow();
}
else if (moisture > LEAKDETECTED){
moistureDetected();
}
for(int i=0;i<5;i++){
tone(BUZZER, 1500);
delay(120);
tone(BUZZER, 600);
delay(120);
}
noTone(BUZZER);
digitalWrite(VMOTOR, HIGH);
delay(500);
lcd.clear();
while (t!=0){
lcd.setCursor(0,0);
lcd.print("Press 1 to RESET");
buttonReading();
}
}
//--------------DISPLAY WASH SELECTION------------------------
//quick wash selected
void quickWash(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Mode:");
lcd.setCursor(7, 0);
lcd.print("Quick");
}
//normal wash selected
void normalWash(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Mode:");
lcd.setCursor(7, 0);
lcd.print("Normal");
}
//delicate wash selected
void delicateWash(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Mode:");
lcd.setCursor(7, 0);
lcd.print("Delicate");
}
//--------------Display Water Levels--------------------
void waterOk(){
lcd.setCursor(0,1);
lcd.print("Water Level OK");
delay(300);
}
void waterWarning(){
lcd.setCursor(0,1);
lcd.print("Check Water Lvl");
delay(300);
}
void waterLow(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("LOW WATER LEVEL");
delay(300);
}
//--------------Display Mositure Levels--------------------
void moistureWarning(){
lcd.clear();
lcd.setCursor(0,1);
lcd.print("MOISTURE WARNING");
delay(300);
}
void moistureDetected(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("LEAK DETECTED");
delay(300);
}
//-----------RESET BUTTON!!!--------------
//In case the reset button is pressed
void buttonReading(){
if (IrReceiver.decode()){
int act = IrReceiver.decodedIRData.command;
IrReceiver.resume();
switch (act){
case BTN0:
break;
case BTN1:
reset();
t=1;
x=0;
break;
}
}
}
//Reset button action
void reset(){
noTone(BUZZER);
digitalWrite(VMOTOR, LOW);
analogWrite(DCMOTOR, 0);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Machine Reset");
delay(500);
}
//Cycle Completion
void cycleComplete(){
analogWrite(DCMOTOR, 0);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Cycle Completed!!");
digitalWrite(LEDG, HIGH);
softbeep();
delay(1000);
}
//--------WASHING MODES--------
//quick wash cycle
void quickWashCycle(){
softbeep();
int moisture = map(analogRead(SENSORM), 0, 876, 0, 100);
if (readWaterLevel() >= MINLVL && moisture < LEAKRISK){
waterOk();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WASHING... ");
analogWrite(DCMOTOR, 220);
for (int aa = 50; aa >0 ; aa++){
//To reset Machine
buttonReading();
if (t==1)
break;
else;
int moisture = map(analogRead(SENSORM), 0, 876, 0, 100);
if (readWaterLevel() < RISKLVL || moisture > LEAKDETECTED) {
emergency();
break;
}
else if (readWaterLevel() < MINLVL || moisture > LEAKRISK) {
warning();
break;
}
else;
delay (200);
if (aa==1){
cycleComplete();
}
else
;
}
}
}
//Normal wash cycle
void normalWashCycle(){
}
//Delicate wash cycle
void delicateWashCycle(){
}
//-----------------------READINGS-----------------------
int readWaterLevel() {
pinMode(PINGPIN, OUTPUT);
digitalWrite(PINGPIN, LOW);
delayMicroseconds(2);
digitalWrite(PINGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(PINGPIN, LOW);
pinMode(PINGPIN, INPUT);
dur = pulseIn(PINGPIN, HIGH, 30000);
if (dur == 0) return 200;
dist = ((dur * .0343) / 2);
return map(dist, 0, 208, 100, 0);
}
//---------------------Monitoring mode--------------------------
// to hold reading from moisture sensor and gave new range of 0-99
void monitoring(){
int moisture = map(analogRead(SENSORM), 0, 876, 0, 100);
if (readWaterLevel() < RISKLVL)
waterOk();
else if(readWaterLevel() < MINLVL)
waterWarning();
else if(moisture > LEAKDETECTED)
moistureDetected();
}
//UPDATED!!!

