The sketch works well but

Hi,
I tested the sketch from:

with MIT APP on my phone, that works well.
I added two lines of:

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

at line 131, and void digitalPotWrite(int address, int value) at line 168, no led on at all, why?
Thanks
Adam

#include "CONFIG.h" 

#define enA 8
#define in1 10
#define in2 11

#define in3 12
#define in4 13
#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);
  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);

    Serial.print("xAxis=");
    Serial.print(xAxis);

    Serial1.print("yAxis=");
    Serial1.print(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);
      }

    }
  }

  analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(enB, motorSpeedB); // Send PWM signal to motor B

  Serial.print("motorSpeedA128=");
  Serial.print(motorSpeedA);

  digitalPotWrite(wiper0writeAddr, motorSpeedA); // Send PWM signal to motor A // THE CODE doesn't work when added these two lines ? why? 
 // digitalPotWrite(wiper1writeAddr, motorSpeedB); // Send PWM signal to motor B

}

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");
}

void digitalPotWrite(int address, int value) {

  digitalWrite(slaveSelectPin, LOW);
  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);
}

Missing an SPI deselect in your last function.

1 Like

Thanks.
I added a line digitalWrite(slaveSelectPin, HIGH); still not work.
is the line right?

It rather depends on where you put it.

1 Like

last line:

void digitalPotWrite(int address, int value) {

  digitalWrite(slaveSelectPin, LOW);
  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);

}