int speedPinM1A = 3; // PWM pin for speed control
int directionPinM1B = 9; // PWM pin for direction control
int speedPinM2A = 10; // PWM pin for speed control
int directionPinM2B = 11; // PWM pin for direction control
int userInput;
int vpInputSpeed;
int dcMotorSpeed;
void serialFlush(){
while(Serial.available() > 0) {
char t = Serial.read();
}
}
void setup() {
Serial.begin(9600);
pinMode(speedPinM1A, OUTPUT);
pinMode(directionPinM1B, OUTPUT);
pinMode(speedPinM2A, OUTPUT);
pinMode(directionPinM2B, OUTPUT);
}
void loop(){
Serial.print("Choose any one option: ");
Serial.println("1. Run vacuum pump only ");
Serial.println("2. Run DC motor only ");
Serial.println("3. Both");
Serial.println("Write 1,2,3 to select any one of these according to your need/choice");
serialFlush();
while (Serial.available()<1){}
if (Serial.available() > 0) // reply only when you receive data:
{
Serial.print("Loop Serial Available: ");
Serial.println(Serial.available());
userInput=Serial.parseInt(); //Reading the data from serial and storing the data
serialFlush();
Serial.print("User Input: ");
Serial.println(userInput);
switch(userInput)
{
case 1:
runvacuumpump();
break;
case 2:
rundcmotor();
break;
case 3:
runvacuumpump();
rundcmotor();
break;
default:
Serial.print("Please enter a valid selection");
}
}
}
void runvacuumpump()
{
Serial.println("Choose any one");
Serial.println("1. Run");
Serial.println("2. Stop");
Serial.println("Write R or S according to your need");
while (Serial.available()<1){}
if(Serial.available() > 0){
Serial.print("Vacuum pump Serial Available: ");
Serial.println(Serial.available());
char vpCommand = Serial.read();
Serial.print("Vacuum pump command -");
Serial.println(vpCommand);
serialFlush();
if (vpCommand=='R')
{
Serial.println("Enter Speed for vacuum Pump within the range of 1 to 255");
serialFlush();
while (Serial.available ()<1){}
if (Serial.available() > 0)
{
vpInputSpeed = Serial.parseInt();
Serial.print("Vacuum Pump input speed-");
Serial.println(vpInputSpeed);
serialFlush();
if(vpInputSpeed > 0 && vpInputSpeed <=255)
{
analogWrite(speedPinM2A, vpInputSpeed);
digitalWrite(directionPinM2B, LOW);
Serial.println("Vacuum Pump is Running..");
}
else{
Serial.println("Please enter value between 1 to 255..");
runvacuumpump();
}
}
}
else if(vpCommand=='S'){
analogWrite(speedPinM2A, 0);
digitalWrite(directionPinM2B,LOW);
Serial.println("Vacuum Pump STOPPED");
}
else{
Serial.println("Please enter valid Input");
runvacuumpump();
}
}
}
void rundcmotor(){
Serial.print("Choose any one option: ");
Serial.println("1. FORWARD ");
Serial.println("2. BACKWARD ");
Serial.println("3. STOP");
Serial.println("WRITE F for Forward, Write B for Backward and Write S to stop the Motor");
while (Serial.available()<1){}
if (Serial.available() > 0) {
Serial.print(" Vacuum pump Serial Available: ");
Serial.println(Serial.available());
char command = Serial.read();
Serial.print("Command-");
Serial.println(command);
serialFlush();
if (command == 'B'){
//Serial.println("RUN");
Serial.println("Enter the value for speed within the range of 0 to 255 ");
serialFlush();
while (Serial.available()<1){}
if (Serial.available() > 0){
dcMotorSpeed=255-Serial.parseInt(); //Reading the data from serial and storing the data
Serial.print("DC Motor Input SPEED: ");
Serial.println(dcMotorSpeed);
serialFlush();
if (dcMotorSpeed >= 0 && dcMotorSpeed <255){
analogWrite(speedPinM1A, dcMotorSpeed);
digitalWrite(directionPinM1B, HIGH); // Seting direction for Backward
Serial.println("MOTOR IS RUNNING IN BACKWARD DIRECTION");
}
}
}
else if(command == 'F'){
// Serial.println("RUN1");
Serial.println("Enter the value for speed within the range of 0 to 255 ");
serialFlush();
while (Serial.available ()<1){}
if (Serial.available() > 0){
dcMotorSpeed=Serial.parseInt(); //Reading the data from serial and storing the data
serialFlush();
Serial.println("User Input SPEED: ");
Serial.println(dcMotorSpeed);
if(dcMotorSpeed > 0 && dcMotorSpeed <=255){
analogWrite(speedPinM1A, dcMotorSpeed);
digitalWrite(directionPinM1B, LOW); // Set direction for Forward
Serial.println("MOTOR IS RUNNING IN FORWARD DIRECTION");
}
}
}
else if(command == 'S'){
analogWrite(speedPinM1A, 0);
digitalWrite(directionPinM1B,LOW);
Serial.println("DC Motor Stopped");
}
else{
Serial.println("Please enter valid Input");
rundcmotor();
}
}
}