The MEGA2560_MCP4261_Bluetooth sketch need help please

Hi,
The MEGA2560_MCP4261_Bluetooth sketch working well, with the line128 comment out; does't work as long as remove the '//' , why?

The code and diagram attached, I used MEGA and did relative modify with pins, and checked the MCP4261 and motors separately and they work well.

Thanks for help.
Adam

#include <SPI.h>

//MCP cinfigrition
const int slaveSelectPin = 53; //WAS 10 on Arduino uno.
const int shutdownPin = 49; // was 7 on uno.

const int wiper0writeAddr = B00000000;  //these addresses can be found in your chip's datasheet
const int wiper1writeAddr = B00010000;

uint8_t receivedValue_x = 0;
uint8_t receivedValue_y = 0;

#define enA 8
#define in1 4
#define in2 5

#define in3 6
#define in4 7
#define enB 9

int xAxis = 140, yAxis = 140;

int motorSpeedA = 0;
int motorSpeedB = 0;

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  pinMode (slaveSelectPin, OUTPUT);
  pinMode (shutdownPin, OUTPUT);
  
  Serial.begin(9600);
  Serial1.begin(9600); // Default communication rate of the Bluetooth module
  delay(500);
}

void loop() {
  // Default value - no movement when the Joystick stays in the center
  //xAxis = 140;
  //yAxis = 140;

  // Read the incoming data from the Smartphone Android App
  while (Serial1.available() >= 2) {
    xAxis = Serial1.read();
    delay(10);
    yAxis = Serial1.read();
    Serial.print(xAxis);
    Serial.print(",");
    Serial.println(yAxis);
  }
  delay(10);

  // Makes sure we receive corrent values

  if (xAxis > 130 && xAxis < 150 && yAxis > 130 && yAxis < 150)
  {
    Stop();
  }

  if (yAxis > 130 && yAxis < 150) {

    if (xAxis < 130) {
      turnRight();
      motorSpeedA = map(xAxis, 130, 60, 0, 255);
      motorSpeedB = map(xAxis, 130, 60, 0, 255);
    }

    if (xAxis > 150) {
      turnLeft();
      motorSpeedA = map(xAxis, 150, 220, 0, 255);
      motorSpeedB = map(xAxis, 150, 220, 0, 255);
    }

  } else {

    if (xAxis > 130 && xAxis < 150) {

      if (yAxis < 130) {
        forword();
      }
      if (yAxis > 150) {
        backword();
      }

      if (yAxis < 130) {
        motorSpeedA = map(yAxis, 130, 60, 0, 255);
        motorSpeedB = map(yAxis, 130, 60, 0, 255);
      }

      if (yAxis > 150) {
        motorSpeedA = map(yAxis, 150, 220, 0, 255);
        motorSpeedB = map(yAxis, 150, 220, 0, 255);
      }

    } else {

      if (yAxis < 130) {
        forword();
      }
      if (yAxis > 150) {
        backword();
      }

      if (xAxis < 130) {
        motorSpeedA = map(xAxis, 130, 60, 255, 50);
        motorSpeedB = 255;
      }

      if (xAxis > 150) {
        motorSpeedA = 255;
        motorSpeedB = map(xAxis, 150, 220, 255, 50);
      }

    }
  }

  //Serial.print(motorSpeedA);
  //Serial.print(",");
  //Serial.println(motorSpeedA);

  analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
  // mcpDrive();
  Serial.print("motorSpeedA122=");
  Serial.print(motorSpeedA);

}

void mcpDrive ()
{
  digitalPotWrite(wiper0writeAddr, motorSpeedA); // Send PWM signal to motor A
  digitalPotWrite(wiper1writeAddr, motorSpeedB); // Send PWM signal to motor B
}

void digitalPotWrite(int address, int value) {

  digitalWrite(slaveSelectPin, LOW);
  delay(5);
  /// digitalWrite(shutdownPin, HIGH); //Turn off shutdown THIS MAKE WRITEABLE OR DISABLE THIS CAN'T WRITE
  //  send in the address and value via SPI:
  SPI.transfer(address);
  SPI.transfer(value);
  digitalWrite(slaveSelectPin, HIGH);
  delay(5);
}

void forword() {
  Serial.println("forword");
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
}

void backword() {
  Serial.println("backword");
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}

void turnRight() {
  Serial.println("turnRight");
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}

void turnLeft() {
  Serial.println("turnLeft");
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
}

void Stop() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  Serial.println("stop");
}

Did you test the Bluetooth by itself? With an app like Bluetooth serial terminal? With your app?

What is printing as a result of the code below?

That conveys no useful information. What does the code actually do? How is that different from what you want?

The serial input basics tutorial shows robust methods of sending and receiving serial data.

1 Like

Your schematic is useless. You are writing from a Mega2560. However, your schematic shows a UNO. However, they are not too completely pin compatible.

Yes, the Bluetooth works well.

The project used MIT APP of Joystick to control motors by Bluetooth, the motors speed controlled by APP signal received of xAxis/yAxis through enA/enB pin. I moved the xAxis/yAxis signals into MCP's witer0/witer1 and used it to control the motor speed which not work.

Please provide some documentation of the MCP 4261 driver. Your schematic is not showing wiring for an SPI device.

void mcpDrive ()
{
  digitalPotWrite(wiper0writeAddr, motorSpeedA); // Send PWM signal to motor A
  digitalPotWrite(wiper1writeAddr, motorSpeedB); // Send PWM signal to motor B
}

void digitalPotWrite(int address, int value) {

  digitalWrite(slaveSelectPin, LOW);
  delay(5);
  /// digitalWrite(shutdownPin, HIGH); //Turn off shutdown THIS MAKE WRITEABLE OR DISABLE THIS CAN'T WRITE
  //  send in the address and value via SPI:
  SPI.transfer(address);
  SPI.transfer(value);
  digitalWrite(slaveSelectPin, HIGH);
  delay(5);
}

Assuming that your code is not only what you want but also kosher, you may find that all you need do is move Bluetooth two pins to the right, so that it is actually connected to the serial port pins 0,1. This should give some result even if you don't know whether you have a Uno or a Mega..

the mega+mcp: