3

I am trying to build a rover controlled by an Arduino with a Funduino Motor Control Shield, and a Bluetooth HC-05 module. However, whenever I send data to it by pressing a button on the GUI that I have created in Processing, the Bluetooth receives the data, and runs for a very short period of time, and then stops, and disconnects from my laptop.

I have already checked my wiring, and I have a voltage divider. The bluetooth LED blinks at 2Hz, so is receiving power, but not connecting. When I run the Processing sketch, the bluetooth connects, and then disconnects when a button is pressed, and stops doing whatever it was doing. However, when I connect the Arduino via USB to my laptop, it works perfectly.

My code is as follows:

#include <AFMotor.h> 

boolean state = false; 

AF_DCMotor motorA(4); 
AF_DCMotor motorB(3); 
AF_DCMotor motorC(2); 
AF_DCMotor motorD(1); 
 

void setup() { 
  Serial.begin(9600);           // set up Serial library at 9600 bps 
  Serial.println("Motor test!");  // it is just satisfying to see that 'Motor test' message - it means your code is running on the Arduino
  // turn on motor - don't lower the speed otherwise it will make a really annoying noise
  motorA.setSpeed(220); 
  motorB.setSpeed(255); 
  motorC.setSpeed(255); 
  motorD.setSpeed(220); 
  motorA.run(RELEASE); 
  motorB.run(RELEASE); 
  motorC.run(RELEASE); 
  motorD.run(RELEASE); 
}

void loop() { 
  if (Serial.available()) {
    char val = Serial.read();
    Serial.println(val);
    if (val == 'w') {
      Serial.println(state);
      // Forward
      if (state == false) {
        state = true;
        motorA.run(BACKWARD);  // Motor A needs to be opposite - it is actually turning forward yere
        motorB.run(FORWARD); 
        motorC.run(FORWARD); 
        motorD.run(FORWARD);
      } else {
        state = false;
        motorA.run(RELEASE); 
        motorB.run(RELEASE); 
        motorC.run(RELEASE); 
        motorD.run(RELEASE); 
      }
    } else if (val == 'a') {  
      Serial.println(state);// Left
      if (state == false) {
        state = true;
        motorA.run(BACKWARD); 
        motorB.run(BACKWARD); 
        motorC.run(BACKWARD); 
        motorD.run(FORWARD); 
      } else {
        state = false;
        motorA.run(RELEASE); 
        motorB.run(RELEASE); 
        motorC.run(RELEASE); 
        motorD.run(RELEASE); 
      }
    } else if (val == 'd') {     // Right
      Serial.println(state);
      if (state == false) {
        state = true;
        motorA.run(FORWARD); 
        motorB.run(FORWARD); 
        motorC.run(FORWARD); 
        motorD.run(BACKWARD); 
      } else {
        state = false;
        motorA.run(RELEASE); 
        motorB.run(RELEASE); 
        motorC.run(RELEASE); 
        motorD.run(RELEASE); 
      }
    } else if (val == 's') {     // Reverse
      Serial.println(state);
      if (state == false) {
        state = true;
        motorA.run(FORWARD); 
        motorB.run(BACKWARD); 
        motorC.run(BACKWARD); 
        motorD.run(BACKWARD); 
      } else {
        state = false;
        motorA.run(RELEASE); 
        motorB.run(RELEASE); 
        motorC.run(RELEASE); 
        motorD.run(RELEASE); 
      }
    } else if (val == 'm') {     // Stand still
      motorA.run(RELEASE); 
      motorB.run(RELEASE); 
      motorC.run(RELEASE); 
      motorD.run(RELEASE); 
    }
  } else {
    Serial.println(0);
  }
} 

My processing sketch is as follows:

import controlP5.*; //import ControlP5 library
import processing.serial.*;
Serial port;

ControlP5 cp5; //create ControlP5 object

void setup(){ //Same as setup in arduino
  frameRate(50);
  size(1200, 750);                          //Window size, (width, height)
  port = new Serial(this, "COM6", 9600);   //Change this to your port
  cp5 = new ControlP5(this);
  
  Button b1 = cp5.addButton("Forward")  //The button
    .setPosition(90, 100)  //x and y coordinates of upper left corner of button
    .setSize(120, 70)      //(width, height)
  ;
  

b1.addCallback(new CallbackListener() {
    public void controlEvent(CallbackEvent theEvent) {
      switch(theEvent.getAction()) {
        case(ControlP5.ACTION_RELEASED): port.write('w'); println("p"); break;
        
      }
    }
  }
  ); 


  Button b2 = cp5.addButton("Left")  //The button
    .setPosition(90, 200)  //x and y coordinates of upper left corner of button
    .setSize(120, 70)      //(width, height)
  ;
  

b2.addCallback(new CallbackListener() {
    public void controlEvent(CallbackEvent theEvent) {
      switch(theEvent.getAction()) {
        case(ControlP5.ACTION_RELEASED): port.write('a'); println("p"); break;
      }
    }
}  
  ); 


  Button b3 = cp5.addButton("Right")  //The button
    .setPosition(290, 100)  //x and y coordinates of upper left corner of button
    .setSize(120, 70)      //(width, height)
  ;
  

b3.addCallback(new CallbackListener() {
    public void controlEvent(CallbackEvent theEvent) {
      switch(theEvent.getAction()) {
        case(ControlP5.ACTION_RELEASED): port.write('d'); println("p"); break;
        
      }
    }
  }
  ); 


  Button b4 = cp5.addButton("Reverse")  //The button
    .setPosition(290, 200)  //x and y coordinates of upper left corner of button
    .setSize(120, 70)      //(width, height)
  ;
  

b4.addCallback(new CallbackListener() {
    public void controlEvent(CallbackEvent theEvent) {
      switch(theEvent.getAction()) {
        case(ControlP5.ACTION_RELEASED): port.write('s'); println("p"); break;
        
      }
    }
  }
  ); 
}
void draw(){  //Same as loop in arduino

  background(150, 0 , 150); //Background colour of window (r, g, b) or (0 to 255)

}

void Button(){port.write('t');}

Schematic of wiring - in addition to this, a Motor Control Shield is added to the Arduino with four DC motors attached. Schematic of wiring - in addition to this, a Motor Control Shield is added to the Arduino with four DC motors attached.

Thanks so much for your help!

EthanVB123
  • 31
  • 5
  • What is your source of power when not plugged into the laptop? You might not have enough current available, Just a possibility. – edgar_wideman Nov 09 '20 at 05:20
  • Thanks for the tip, but I have plugged my Arduino into mains power, so it is definitely receiving power. Also, it continues to receive power after disconnection, so power isn't an issue. – EthanVB123 Nov 09 '20 at 05:42
  • 3
    Could you add a schematic of all the connections to your question? Preferably a proper schematic, not a Fritzing cartoon. This is not necessarily a software problem. – ocrdu Nov 09 '20 at 10:46
  • Sure, but you may have to wait a bit - I'm busy at the moment, but can provide a schematic tomorrow afternoon. – EthanVB123 Nov 09 '20 at 11:28
  • 1
    Sorry, but the Fritzing cartoon is all that was available for what I want, but the schematic is clear. – EthanVB123 Nov 10 '20 at 11:34
  • You should tag the people you want to inform that you updated the question so they know you've done it even if they aren't monitoring this thread. – laancelot Nov 11 '20 at 00:27
  • 1
    @laancelot Thanks for the tip! I'm still new to this. – EthanVB123 Nov 11 '20 at 08:33
  • @ocrdu I have added a schematic - sorry for the format, but that is all I have – EthanVB123 Nov 11 '20 at 08:34

0 Answers0