DC Motor Control Help!

Before creating the topic

  1. Search forum for similar topics
  2. Check http://docs.blynk.cc and http://help.blynk.cc/
  3. Add details :
    • Hardware model + communication type. For example: Arduino UNO with Ethernet Shield
    • Smartphone OS (iOS or Android) + version
    • Blynk server or local server
    • Blynk Library version
    • Add your sketch code. :point_up:Code should be formatted as example below.

Simply paste your code between ``` If you don’t format your code, your topic can be deleted by moderators.

Ok, so I am trying to create a little vehicle. 2 dc motors will drive the vehicle. 1 dc motor will engage a conveyor belt system and a servo will be used to raise and lower the conveyor belt system. I have connected to my HM-10 Bluetooth module from my phone and have been able to control the servo with it. I am having trouble controlling the dc motors that are connected to an Adafruit v2.3 motorshield. The dc motors spin when I upload a test code that varies their speed and direction, but I can’t control them with my phone. Take a look at my code and see if there’s anything wrong in there. I’m confident that my wiring is done correctly. At this point, I’m just not sure what’s wrong with it but it has to be Blynk related (or so I am thinking) or the code associated with it. Any advice would be great thanks!


#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
#include <SoftwareSerial.h>
#include <BlynkSimpleSerialBLE.h>
#include <Servo.h>
#define BLYNK_PRINT Serial

Adafruit_MotorShield AFMS=Adafruit_MotorShield();

Adafruit_DCMotor *motor1=AFMS.getMotor(1);
Adafruit_DCMotor *motor2=AFMS.getMotor(2);
Adafruit_DCMotor *motor3=AFMS.getMotor(3);

Servo servo1;

void setup() {
  Serial.begin(9600);
  Serial.println("Adafruit Motorshield v2 - DC Motor test!");

  AFMS.begin();
  servo1.attach(6);
  motor3->setSpeed(100); //digging arm speed test number
}

void loop() {
  Blynk.run();
}

//LEFT MOTOR FORWARD
BLYNK_WRITE(V1) {
  int pinValue=param.asInt();
  Serial.print("Motor 1 Forward: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor1->run(FORWARD);
  }
  if(pinValue==0) {
    motor1->run(RELEASE);    
  }
}

//RIGHT MOTOR FORWARD
BLYNK_WRITE(V2) {
  int pinValue=param.asInt();
  Serial.print("Motor 2 Forward: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor2->run(FORWARD);
  }
  if(pinValue==0) {
    motor2->run(RELEASE);    
  }
}

//LEFT MOTOR BACKWARDS
BLYNK_WRITE(V3) {
  int pinValue=param.asInt();
  Serial.print("Motor 1 Backwards: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor1->run(BACKWARD);
  }
  if(pinValue==0) {
    motor1->run(RELEASE);    
  }
}

//RIGHT MOTOR BACKWARDS
BLYNK_WRITE(V4) {
  int pinValue=param.asInt();
  Serial.print("Motor 2 Backwards: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor2->run(BACKWARD);
  }
  if(pinValue==0) {
    motor2->run(RELEASE);    
  }
}

//MOTOR SPEED
BLYNK_WRITE(V5) {
  int pinValue=param.asInt();
  Serial.print("Motor Speed: ");
  Serial.println(pinValue);
  
  motor1->setSpeed(pinValue);
  motor2->setSpeed(pinValue);
}

//SERVO UP DOWN
BLYNK_WRITE(V6) {
  servo1.write(param.asInt());
 }

//DIG FORWARD
BLYNK_WRITE(V7) {
  int pinValue=param.asInt();
  Serial.print("Motor 3 Forward: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor3->run(BACKWARD);
  }
  if(pinValue==0) {
    motor3->run(RELEASE);    
  }
}

//DIG BACKWARDS
BLYNK_WRITE(V8) {
  int pinValue=param.asInt();
  Serial.print("Motor 3 Backwards: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor3->run(BACKWARD);  
  }
  if(pinValue==0) {
    motor3->run(RELEASE);    
  }
}

//EMERGENCY STOP
BLYNK_WRITE(V9) {
  int pinValue=param.asInt();
  Serial.print("Emergency Stop: ");
  Serial.println(pinValue);
  if(pinValue==1) {
    motor1->run(RELEASE);
    motor2->run(RELEASE);
    motor3->run(RELEASE);
  }
  //if(pinValue==0) {
   // motor3->run(RELEASE);    
  //}
}

A post was merged into an existing topic: DC Motor Control

Duplicate topic