How to control 12v dc motor by using serial input

Hello arduino community, i'm new arduino here, and i'm still learning. I'm working my project for my final exam. So im building a device that can control ph and tds of the water, i'm already can control the sensor and send the data to node-red via mqtt, and now i want to control the peristaltic pump, and controlling it based on the input.

My plan is to control the pump to turn on, and how long the pump turned on is based on the input. And the peristaltic pumps are used to transport PH and TDS liquid. TDS is a liquid that contained minerals.

int ml;
int nyala;
int input;
int dc_pin = D1;

void setup() {
  pinMode(dc_pin, OUTPUT);
  Serial.begin(115200);
}
  
void loop() {
  input = Serial.parseInt();
  ml = input/10;
  nyala = ml * 500;

  if(nyala == 500){
    digitalWrite(dc_pin, LOW);
  delay(500);
  MotorStop();
  }
  else if(nyala == 1000){
    digitalWrite(dc_pin, LOW);
  delay(1000);
  MotorStop();
  }
  else if(nyala == 2000){
    digitalWrite(dc_pin, LOW);
  delay(2000);
  MotorStop();
  }
}

void MotorStop(){
  digitalWrite(dc_pin, LOW);
  digitalWrite(dc_pin, LOW);
}

ml is the mililiter for how many the liquids needed.

nyala is how long the pump must be turned on.

Will my code work?

Thank you for your time to answer my question.

Hardly. What if the input is anything else than 10, 20 or 40? If you start the motor by setting a pin low, are you really going to stop it by setting the same pin low again... twice?

Okay then, what if i use the "nyala" for the delay? and how to make the pump only turn on from the input, and after it done it will standby for another input?

I've try some codes and now its working, turns out im using nyala as the perimeter, and im using the input right now.

here is the codes

int nyala;
int input;
int dc_pin = D1;

void setup() {
  pinMode(dc_pin, OUTPUT);
  Serial.begin(115200);
}
  
void loop() {
 while(Serial.available()){
   input = Serial.parseInt();
   nyala = (input/10) * 500;

  if(input == 0){
  digitalWrite(dc_pin, HIGH);
  Serial.print("Masukan : ");
  Serial.println(nyala);
  }
  else if(input > 0){
  digitalWrite(dc_pin, LOW);
  Serial.print("Masukan : ");
  Serial.println(nyala);
  delay(nyala);
  MotorStop();
  }
 }
}

void MotorStop(){
  digitalWrite(dc_pin, LOW);
  digitalWrite(dc_pin, LOW);
}

Its working but when it turned on the motor started it self so i have to input 0 to the serial monitor, but what i want is the motor is only turned on when there is an input since the first upload.

Ttry this.

int nyala;
int input;
int dc_pin = D1;

void setup ()
{
  pinMode ( dc_pin, OUTPUT );
  digitalWrite ( dc_pin, HIGH );
  Serial.begin ( 115200 );
}

void loop ()
{
  while ( Serial.available () )
  {
    input = Serial.parseInt ();
    nyala = ( input / 10 ) * 500;
    
    if ( input == 0 )
    {
      digitalWrite ( dc_pin, HIGH );
      Serial.print ( "Masukan : " );
      Serial.println ( nyala );
    }
    else if ( input > 0 )
    {
      digitalWrite ( dc_pin, LOW );
      Serial.print ( "Masukan : " );
      Serial.println ( nyala );
      delay ( nyala );
      digitalWrite ( dc_pin, HIGH );
    }
  }
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.