/*
const int mosfet_Valve_1_Pin = 9; // Connect the MOSFET MODULE (Red One) Sig Pin to Arduino pin 9
const int mosfet_Valve_2_Pin = 10; // Connect the MOSFET MODULE (Red One) Sig Pin to Arduino pin 10
const int mosfet_Valve_3_Pin = 11; // Connect the MOSFET MODULE (Red One) Sig Pin to Arduino pin 11
char commandBuffer[10]; // Buffer to store the received command
int valveNumber; // Stores the valve number (1, 2, or 3)
int valveSpeed; // Stores the valve speed (0-255)
void setup()
{
Serial.begin(115200);
Serial.println("Turar Solenoid Valves Controlling");
Serial.println();
Serial.println("Please Input the commands [As: X,n] X : Which Valve (1-3) & n for Flow Speed (0-255)");
Serial.println();
pinMode(mosfet_Valve_1_Pin, OUTPUT);
pinMode(mosfet_Valve_2_Pin, OUTPUT);
pinMode(mosfet_Valve_3_Pin, OUTPUT);
}
void loop()
{
if (Serial.available() > 0)
{
// Read the incoming command into the buffer
Serial.readBytesUntil(',', commandBuffer, sizeof(commandBuffer));
// Parse the command into valveNumber and valveSpeed
valveNumber = atoi(commandBuffer);
valveSpeed = Serial.parseInt();
// Check if valveNumber is within the valid range (1-3) and valveSpeed is within the valid range (0-255)
if (valveNumber >= 1 && valveNumber <= 3 && valveSpeed >= 0 && valveSpeed <= 255)
{
// Act based on the parsed command
switch (valveNumber)
{
case 1:
analogWrite(mosfet_Valve_1_Pin, valveSpeed); // Set the speed for valve 1
break;
case 2:
analogWrite(mosfet_Valve_2_Pin, valveSpeed); // Set the speed for valve 2
break;
case 3:
analogWrite(mosfet_Valve_3_Pin, valveSpeed); // Set the speed for valve 3
break;
}
}
}
}
*/
const int mosfet_Valve_1_Pin = 9; // Connect the MOSFET MODULE (Red One) Sig Pin to Arduino pin 9
const int mosfet_Valve_2_Pin = 10; // Connect the MOSFET MODULE (Red One) Sig Pin to Arduino pin 10
const int mosfet_Valve_3_Pin = 11; // Connect the MOSFET MODULE (Red One) Sig Pin to Arduino pin 11
char commandBuffer[20]; // Buffer to store the received command
int valveNumber; // Stores the valve number (1, 2, or 3)
int valveSpeed; // Stores the valve speed (0-255)
unsigned long duration; // Stores the duration in milliseconds
unsigned long startTime = 0; // Stores the start time
boolean isRamping = false; // Flag to indicate whether the valve is ramping up
boolean isRampingDown = false; // Flag to indicate whether the valve is ramping down
void setup()
{
Serial.begin(115200);
Serial.println("Turar Solenoid Valves Controlling");
Serial.println();
pinMode(mosfet_Valve_1_Pin, OUTPUT);
pinMode(mosfet_Valve_2_Pin, OUTPUT);
pinMode(mosfet_Valve_3_Pin, OUTPUT);
}
void loop()
{
if (Serial.available() > 0)
{
// Read the incoming command into the buffer
Serial.readBytesUntil('}', commandBuffer, sizeof(commandBuffer));
// Parse the command into valveNumber, valveSpeed, and duration
sscanf(commandBuffer, "{%d,%d,%lu}", &valveNumber, &valveSpeed, &duration);
// Check if valveNumber is within the valid range (1-3), valveSpeed is within the valid range (0-255), and duration is greater than 0
if (valveNumber >= 1 && valveNumber <= 3 && valveSpeed >= 0 && valveSpeed <= 255 && duration > 0)
{
// Start ramping up the valve
analogWrite(mosfet_Valve_1_Pin, 0);
analogWrite(mosfet_Valve_2_Pin, 0);
analogWrite(mosfet_Valve_3_Pin, 0);
startTime = millis();
isRamping = true;
isRampingDown = false;
}
}
// Check if we are currently ramping up
if (isRamping)
{
unsigned long currentTime = millis();
unsigned long elapsedTime = currentTime - startTime;
if (elapsedTime < duration)
{
// Calculate the current speed based on elapsed time
int currentSpeed = map(elapsedTime, 0, duration, 0, valveSpeed);
// Set the current speed for the corresponding valve
switch (valveNumber)
{
case 1:
analogWrite(mosfet_Valve_1_Pin, currentSpeed);
break;
case 2:
analogWrite(mosfet_Valve_2_Pin, currentSpeed);
break;
case 3:
analogWrite(mosfet_Valve_3_Pin, currentSpeed);
break;
}
}
else
{
// Reached the desired speed, start ramping down
isRamping = false;
isRampingDown = true;
startTime = currentTime;
}
}
// Check if we are currently ramping down
if (isRampingDown)
{
unsigned long currentTime = millis();
unsigned long elapsedTime = currentTime - startTime;
if (elapsedTime < duration)
{
// Calculate the current speed based on elapsed time
int currentSpeed = map(elapsedTime, 0, duration, valveSpeed, 0);
// Set the current speed for the corresponding valve
switch (valveNumber)
{
case 1:
analogWrite(mosfet_Valve_1_Pin, currentSpeed);
break;
case 2:
analogWrite(mosfet_Valve_2_Pin, currentSpeed);
break;
case 3:
analogWrite(mosfet_Valve_3_Pin, currentSpeed);
break;
}
}
else
{
// Reached the end of ramping down, turn off the valve
switch (valveNumber)
{
case 1:
analogWrite(mosfet_Valve_1_Pin, 0);
break;
case 2:
analogWrite(mosfet_Valve_2_Pin, 0);
break;
case 3:
analogWrite(mosfet_Valve_3_Pin, 0);
break;
}
isRampingDown = false;
}
}
}