I want to add manual mode where user has option to input to call different function like wash, spin, dry etc.If user has not given any input within prescribed time, loop will start as prescribed.
//-------- Relay State Setups -------//
#define CLOCKWISE HIGH
#define ANTICLOCKWISE LOW
#define RELAY_ON LOW
#define RELAY_OFF HIGH
//----------------------------------//
// PORTS //
#define MotorPowerPort 2
#define MototrDirectionPort 6
#define WaterFillValvePort 4
#define WaterDrainValvePort 5
#define Buzzer 8
// GLOBAL PROCESS VARIABLES //
double process_start = 0L;
unsigned long process_duration = 0L;
int motor_status = CLOCKWISE;
int process = 0;
int process_max = 15;
long millis_padding = 0;
// ------------------------ //
void setup() {
Serial.begin(9600);
delay(2000);
processInit();
}
void loop() {
// put your main code here, to run repeatedly:
if (doProcess())
{
processReset();
}
}
//------------------PROCESS AND OPERATIONS ---------------------------------//
boolean doProcess() //Return is Completed !
{
if (process == 0)buzzer(1);
else if (process == 1)drainWater(1);
else if (process == 2)fillWater(3);
else if (process == 3)doWash(4000, 1);
else if (process == 4)doSoak(15);
else if (process == 5)doWash(4000, 10);
else if (process == 6)drainWater(2);
else if (process == 7)fillWater(3);
else if (process == 8)doWash(4000, 8);
else if (process == 9)drainWater(3);
else if (process == 10)fillWater(4);
else if (process == 11)doWash(4000, 8);
else if (process == 12)buzzer(5);
else if (process == 13)doSoak(8);
else if (process == 14)drainWater(5);
else if (process == 15)buzzer(10);
else return true;
processUpdate();
return false;
}
double process_pivot = 0;
void processUpdate()
{
double process_current = millis_padded();
if ((process_current - process_start) >= process_duration)
{
process++;
processReset();
delay(1000);
}
}
void processInit()
{
pinMode(MotorPowerPort, OUTPUT);
pinMode(MototrDirectionPort, OUTPUT);
pinMode(WaterFillValvePort, OUTPUT);
pinMode(WaterDrainValvePort, OUTPUT);
pinMode(Buzzer, OUTPUT);
process_duration = 0;
process = 0;
process_start = millis_padded();
motor_status = CLOCKWISE;
digitalWrite(MotorPowerPort, RELAY_OFF);
digitalWrite(MototrDirectionPort, CLOCKWISE);
digitalWrite(WaterFillValvePort, RELAY_OFF);
digitalWrite(WaterDrainValvePort, RELAY_OFF);
digitalWrite(Buzzer, RELAY_OFF);
}
void processReset()
{
process_start = millis_padded();
process_duration = 0;
motor_status = CLOCKWISE;
digitalWrite(MotorPowerPort, RELAY_OFF);
digitalWrite(MototrDirectionPort, CLOCKWISE);
digitalWrite(WaterFillValvePort, RELAY_OFF);
digitalWrite(WaterDrainValvePort, RELAY_OFF);
digitalWrite(Buzzer, RELAY_OFF);
}
void setProcessDuration(int mins)
{
process_duration = mins2millis(mins);
}
//---------------------Process In Detail --------------------------------------//
void doWash(int cycle_delay, int time_mins)
{
setProcessDuration(time_mins);
if (motor_status == CLOCKWISE)motor_status = ANTICLOCKWISE;
else motor_status = CLOCKWISE;
digitalWrite(MotorPowerPort, RELAY_OFF); //TURN OFF MAIN POWER TO MOTOR
delay(1000);
digitalWrite(MototrDirectionPort, motor_status); //SET MOTOR DIRECTION
delay(1000);
digitalWrite(MotorPowerPort, RELAY_ON); //TURN ON MAIN POWER TO MOTOR
delay(cycle_delay);
}
void fillWater(int time_mins)
{
setProcessDuration(time_mins);
digitalWrite(WaterFillValvePort, RELAY_ON);
}
void drainWater(int time_mins)
{
setProcessDuration(time_mins);
digitalWrite(WaterDrainValvePort, RELAY_ON);
}
void doSoak(int time_mins)
{
setProcessDuration(time_mins);
delay(1000);
//JUST WAIT :)
}
void buzzer(int t)
{
for (int i = 0; i < t; i++)
{
digitalWrite(Buzzer, RELAY_ON);
delay(5000);
digitalWrite(Buzzer, RELAY_OFF);
delay(5000);
}
}
//---------------------------------------------------------------------------------------//
unsigned long mins2millis(int mins)
{
return (((unsigned long)mins) * 60 * 1000);
}
double millis_padded()
{
return (millis() + millis_padding);
}
Welcome to the forum.
Before you continue, please first of all read the sticky, then fix your code so it doesn't contain those smilies, and while you're at it add the rest of the information about your project that we will need but which is missing.
Oh, wait, it seems you actually do know how to post code!
I see a mixed up mashup of attempts at timing. Some of the lifted code has the right end minus start calculation that is useless in the face of the rest of it.
No code tags and should know better, I guess I shouldn't waste time on this either.
Sorry for inconvenience .
This is the corrected code
//-------- Relay State Setups -------//
#define CLOCKWISE HIGH
#define ANTICLOCKWISE LOW
#define RELAY_ON LOW
#define RELAY_OFF HIGH
//----------------------------------//
// PORTS //
#define MotorPowerPort 2
#define MototrDirectionPort 6
#define WaterFillValvePort 4
#define WaterDrainValvePort 5
#define Buzzer 8
// GLOBAL PROCESS VARIABLES //
double process_start = 0L;
unsigned long process_duration = 0L;
int motor_status = CLOCKWISE;
int process = 0;
int process_max = 15;
long millis_padding = 0;
// ------------------------ //
void setup() {
Serial.begin(9600);
delay(2000);
processInit();
}
void loop() {
// put your main code here, to run repeatedly:
if (doProcess())
{
processReset();
}
}
//------------------PROCESS AND OPERATIONS ---------------------------------//
boolean doProcess() //Return is Completed !
{
if (process == 0)buzzer(1);
else if (process == 1)drainWater(1);
else if (process == 2)fillWater(3);
else if (process == 3)doWash(4000, 1);
else if (process == 4)doSoak(15);
else if (process == 5)doWash(4000, 10);
else if (process == 6)drainWater(2);
else if (process == 7)fillWater(3);
else if (process == 8)doWash(4000, 8);
else if (process == 9)drainWater(3);
else if (process == 10)fillWater(4);
else if (process == 11)doWash(4000, 8);
else if (process == 12)buzzer(5);
else if (process == 13)doSoak(8);
else if (process == 14)drainWater(5);
else if (process == 15)buzzer(10);
else return true;
processUpdate();
return false;
}
double process_pivot = 0;
void processUpdate()
{
double process_current = millis_padded();
if ((process_current - process_start) >= process_duration)
{
process++;
processReset();
delay(1000);
}
}
void processInit()
{
pinMode(MotorPowerPort, OUTPUT);
pinMode(MototrDirectionPort, OUTPUT);
pinMode(WaterFillValvePort, OUTPUT);
pinMode(WaterDrainValvePort, OUTPUT);
pinMode(Buzzer, OUTPUT);
process_duration = 0;
process = 0;
process_start = millis_padded();
motor_status = CLOCKWISE;
digitalWrite(MotorPowerPort, RELAY_OFF);
digitalWrite(MototrDirectionPort, CLOCKWISE);
digitalWrite(WaterFillValvePort, RELAY_OFF);
digitalWrite(WaterDrainValvePort, RELAY_OFF);
digitalWrite(Buzzer, RELAY_OFF);
}
void processReset()
{
process_start = millis_padded();
process_duration = 0;
motor_status = CLOCKWISE;
digitalWrite(MotorPowerPort, RELAY_OFF);
digitalWrite(MototrDirectionPort, CLOCKWISE);
digitalWrite(WaterFillValvePort, RELAY_OFF);
digitalWrite(WaterDrainValvePort, RELAY_OFF);
digitalWrite(Buzzer, RELAY_OFF);
}
void setProcessDuration(int mins)
{
process_duration = mins2millis(mins);
}
//---------------------Process In Detail --------------------------------------//
void doWash(int cycle_delay, int time_mins)
{
setProcessDuration(time_mins);
if (motor_status == CLOCKWISE)motor_status = ANTICLOCKWISE;
else motor_status = CLOCKWISE;
digitalWrite(MotorPowerPort, RELAY_OFF); //TURN OFF MAIN POWER TO MOTOR
delay(1000);
digitalWrite(MototrDirectionPort, motor_status); //SET MOTOR DIRECTION
delay(1000);
digitalWrite(MotorPowerPort, RELAY_ON); //TURN ON MAIN POWER TO MOTOR
delay(cycle_delay);
}
void fillWater(int time_mins)
{
setProcessDuration(time_mins);
digitalWrite(WaterFillValvePort, RELAY_ON);
}
void drainWater(int time_mins)
{
setProcessDuration(time_mins);
digitalWrite(WaterDrainValvePort, RELAY_ON);
}
void doSoak(int time_mins)
{
setProcessDuration(time_mins);
delay(1000);
//JUST WAIT :)
}
void buzzer(int t)
{
for (int i = 0; i < t; i++)
{
digitalWrite(Buzzer, RELAY_ON);
delay(5000);
digitalWrite(Buzzer, RELAY_OFF);
delay(5000);
}
}
//---------------------------------------------------------------------------------------//
unsigned long mins2millis(int mins)
{
return (((unsigned long)mins) * 60 * 1000);
}
double millis_padded()
{
return (millis() + millis_padding);
}
So what does this code do, and how is it different from what you want it to do?
Where to start? Maybe the worst is the use of floating point to do timing.
Note that Arduino double is the same 32-bit crap precision floating point variable and that Arduino has no FPU.
Learn to do proper millis timing using unsigned integer variables. Do that with example sketches instead of trying to work it into this project and learn poorly at the same time. If you learn it for real, you won't want let alone need the delay() calls.
wvmarle:
So what does this code do, and how is it different from what you want it to do?
It just execute all function one by one. But , i want to interrupt the loop and wait for input from user to call specific function like dospin or wash.
GoForSmoke:
Where to start? Maybe the worst is the use of floating point to do timing.
Note that Arduino double is the same 32-bit crap precision floating point variable and that Arduino has no FPU.
Learn to do proper millis timing using unsigned integer variables. Do that with example sketches instead of trying to work it into this project and learn poorly at the same time. If you learn it for real, you won't want let alone need the delay() calls.
I'm newbie here and don't know much about programming. I want to add manual mode so that user can call specific function before starting of void loop to do specific task and if no input given by user, program should go for loop.
This is what the do-many-things-at-once lesson is for. When a delay() is running, nothing else runs until it is done so no way for code to see a pin change from a button press.
Here is my favorite site for that lesson taught simply. You may think "turkey bacon" if pork is forbidden.
When you get that down you can add checks for leaks (take too long to fill, not stay full when should) and other problems.
GoForSmoke:
This is what the do-many-things-at-once lesson is for. When a delay() is running, nothing else runs until it is done so no way for code to see a pin change from a button press.
Here is my favorite site for that lesson taught simply. You may think "turkey bacon" if pork is forbidden.
Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs
When you get that down you can add checks for leaks (take too long to fill, not stay full when should) and other problems.
Thanks. I will try to learn from there.