Arduino 101

I am new so be nice…=)

I am working with my son on the following project. Powering a tank with two dc motors, Arduino 101, Adafruit Motor Shield V2.3, and the Blynk app.

I have uploaded the sketch…which is okay. Then I connect to the app…again okay. Then I press button V1 to move one motor forward. The motor continues to spin after I release the button instead of stopping and then won’t work again. I get a “Device is Offline” message in the app.

I would appreciate any help you can provide.

/**************************************************************
 * This sketch is for an Arduino 101 BLE rover using Blynk and Adafruit Motor Shield V2
 * Code was copied from both Adafruit and Blynk librarie examples
 * Full documentation on building this rover yourself on Hackster. IO and electronhacks. com
 * Sketch is released under MIT license
 **************************************************************/


#define BLYNK_PRINT Serial
#include <BlynkSimpleCurieBLE.h>
#include <CurieBLE.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "52e5dbb5ab2144d1ac0b8725715c617d";

BLEPeripheral  blePeripheral;

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 

// Select which 'port' M1, M2, M3 or M4.
Adafruit_DCMotor *motor1 = AFMS.getMotor(1);
Adafruit_DCMotor *motor2 = AFMS.getMotor(2);
Adafruit_DCMotor *motor3 = AFMS.getMotor(3);
Adafruit_DCMotor *motor4 = AFMS.getMotor(4);

//######### SETUP ######################################
void setup() {
  Serial.begin(9600);
  delay(1000);

  // The name your bluetooth service will show up as, customize this if you have multiple devices
  blePeripheral.setLocalName("Arduino101Blynk");
  blePeripheral.setDeviceName("Arduino101Blynk");
  blePeripheral.setAppearance(384);

  Blynk.begin(blePeripheral, auth);

  blePeripheral.begin();
  Serial.println("Waiting for connections...");
    
  Serial.println("Adafruit Motorshield v2 - DC Motor");

  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  
  // Set the speed to start, from 0 (off) to 255 (max speed)
  motor1->setSpeed(255);
  motor2->setSpeed(255);
  motor3->setSpeed(255);
  motor4->setSpeed(255);  

}


//########## LOOP ######################################
void loop() {
  Blynk.run();
  blePeripheral.poll();
}


//######### Subrutines ################################

// This function will set the speed
BLYNK_WRITE(V0)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  // You can also use:
  // String i = param.asStr();
  // double d = param.asDouble();
  Serial.print("V0 Slider value is: ");
  Serial.println(pinValue);
  motor1->setSpeed(pinValue);
  motor2->setSpeed(pinValue); 
  motor3->setSpeed(pinValue);
  motor4->setSpeed(pinValue);   
}

// Motor 1 Forward
BLYNK_WRITE(V1)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  Serial.print("Motor 1 Forward: ");
  Serial.println(pinValue);
  if(pinValue == 1) {
    motor1->run(FORWARD);
    motor3->run(FORWARD);
  }
  if(pinValue == 0) {
    motor1->run(RELEASE);
    motor3->run(RELEASE);    
  }
}


// Motor 2 Forward
BLYNK_WRITE(V2)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  Serial.print("Motor 2 Forward: ");
  Serial.println(pinValue);
  if(pinValue == 1) {
    motor2->run(FORWARD);
    motor4->run(FORWARD);   
  }
  if(pinValue == 0) {
    motor2->run(RELEASE);
    motor4->run(RELEASE);
  }
}

// Motor 1 Backward
BLYNK_WRITE(V3)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  Serial.print("Motor 1 Backward: ");
  Serial.println(pinValue);
  if(pinValue == 1) {
    motor1->run(BACKWARD);
    motor3->run(BACKWARD);    
  }
  if(pinValue == 0) {
    motor1->run(RELEASE);
    motor3->run(RELEASE);    
  }
}

// Motor 2 Backward
BLYNK_WRITE(V4)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  Serial.print("Motor 2 Backward: ");
  Serial.println(pinValue);
  if(pinValue == 1) {
    motor2->run(BACKWARD);
    motor4->run(BACKWARD);
  }
  if(pinValue == 0) {
    motor2->run(RELEASE);
    motor4->run(RELEASE);    
  }
}

@Negatte welcome new Blynker.

Let’s explain the term int pinValue in the context of your code and the examples you might have seen.

It’s an integer variable assigned locally, not globally, for a particular widget and given the name pinValue.

In a basic example pinValue doesn’t really have a context, it could be anything like controlLED, startMotorSeven, loopCounter etc. Don’t use “pinValue” as a variable name because it makes it very difficult to understand what your code does. Use a descriptive name that gives a good indication of what the variable is for, so even thismovesthetankBackwards etc.

In an example it’s common to see the variable defined locally because it’s just to demonstrate one feature and a local variable is fine. However in real code, like yours, it will quite often need to be a global variable.

So with the libraries etc we define it globally as:

int thismovesthetankBackwards; // no real need for a comment but why not

And then in the rest of the sketch you don’t use int for the variable as that would redefine it as a local variable.

You copied the comment // assigning incoming value from pin V1 to a variable for V1 to V4 but you didn’t edit the comment, which again makes it difficult to read your code.

Make the changes regarding global variables and see how the tank performs.

2 Likes

If you check the docs, you will see that Arduino 101 currently has bugs in it, and thus not working.

1 Like

Thanks Costas. I will complete your recommendations and try again.

Made those changes and it did improve. But, I can use one button at a time. There are four buttons, V1 (Motor 1 forward), V2 (Motor 2 Forward), V3 (Motor 1 Backwards), and V4 (Motor 2 Backwards). If I hold one button at a time it works for a while but eventually looses connection. If I hold two buttons together, both motors turn even after releasing the button and then the device disconnects immediately. Could this be a power issue?

See if any of the code in this project helps Robot with Esp8266 Shield and Arduino Nano
Do remember, some aspects of how certain widgets work may have changed since that project was built.

1 Like

i would rather use the joystick widget instead, i do not think the multiple button presses are implemented fully in the app.

also, consider the post from @vshymanskyy.

as for the power issue, without a shared schematic, you are the only person here who can answer this…

1 Like