The motor start running when power ON, why?

Hi,
The Serial.monitor attached, where the forword_motorSpeedA=131/ forword_motorSpeedB=255 came from even the xAxis=0 / yAxis=0 without Bluetooth connected?
need help please.
Thanks
Adam

#include <Bridge.h>
#include <BridgeClient.h>
#include <BridgeServer.h>
#include <BridgeSSLClient.h>
#include <BridgeUdp.h>
#include <Console.h>
#include <FileIO.h>
#include <HttpClient.h>
#include <Mailbox.h>
#include <Process.h>
#include <YunClient.h>
#include <YunServer.h>

#include <TimedAction.h> //this library is outdated and it won't work with the newest versions of Arduino. After you downloaded the library - https://playground.arduino.cc/Code/TimedAction/ - you must open the libraries folder, open the TimedAction folder, open the TimedAction.h file and change the 33/34 line (from #include "TimedAction.h" to #include "Arduino.h"). If it is already changed, then you shouldn't have any problems.
#include <Servo.h>

#define Start_Relay  12  /// WAS: #define Pin12  12
#define Stop_Relay  13  //// WAS: #define Pin13  13 
char caracter;

uint8_t xAxis = 140, yAxis = 140; 
////uint8_t xAxis, yAxis;  // WAS: 140
uint8_t  x = 0;
uint8_t  y = 0;

uint8_t motorSpeedA = 0;
uint8_t motorSpeedB = 0;

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

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

//char caracter;
int temperature = 0;
int humidity = 0;
String tempera_humidity;

#define DHTPIN 2     // Digital pin connected to the DHT sensor
////////////////////////////////////////////////////////////////////////// 20211207: 
uint8_t V0 = 510;
//// uint8_t Median = V0 / 2;  ///// when connected to Joystick PAD use this line
uint8_t Median = 140;
uint8_t MedianL = Median - 10;
uint8_t MedianH = Median + 10;

////////////////////////////////////////////////////////////////////////// 20211207: 

///////////////////////////////////////////// add MCP4261
/*
  Matthew McMillan
  @matthewmcmillan
  http://matthewcmcmillan.blogspot.com
  Created 12 Mar 2014
  Digital Pot Control (MCP4251)

*/

// set pin 10 as the slave select for the digital pot:
//const int slaveSelectPin = 10;    // Uno
const int slaveSelectPin = 53;    // Mega
const int shutdownPin = 49;
const int wiper0writeAddr = B00000000;
const int wiper1writeAddr = B00010000;
const int tconwriteAddr = B01000000;
const int tcon_0off_1on = B11110000;
const int tcon_0on_1off = B00001111;
const int tcon_0off_1off = B00000000;
const int tcon_0on_1on = B11111111;

uint8_t scurt = 500;
uint8_t lung = 3000;

uint8_t PotWiperVoltage0 = 0;
uint8_t RawVoltage0 = 0;
float Voltage0 = 0;
uint8_t PotWiperVoltage1 = 1;
uint8_t RawVoltage1 = 0;
float Voltage1 = 0;


///////////////////////////////////// OLED

int val, cnt = 0, v[8];  /// was: v[4];
int pos1 = 90, pos2 = 90, pos3 = 90, pos4 = 90;
int val1, val2, val3, val4;

int val5, val6, val7, val8; /// Add: val5, val6 , val7, val8
int timer1, timer2, timer3;

int timer5, timer6, timer7, timer8;  /// add timer4, timer5, timer6, timer7
int Ok1 = 1, Ok2 = 1, Ok3 = 1;

int Ok5 = 1, Ok6 = 1, Ok7 = 1, Ok8 = 1;  //add Ok4 = 1,Ok5=1, Ok6 = 1, Ok7 = 1;
int delayy2, delayx1, delayy1;

int delay5, delay6, delay7, delay8; // ADD: delay4, delay5, delay6, delay7;

void readbt() {
  Serial1.available();
  val = Serial1.read();  ////
  cnt++;
  v[cnt] = val;
  if (v[1] == 1 && cnt == 3) {  // J1 data:  mark '1' + 'x'/'y' datas ;
    cnt = 0;
  }
  if (v[1] != 1 && cnt == 2) {  // J2 mark '2'+ 'y'; slider data: mark '3'+ thumbPosition data;
    cnt = 0;                    // added: button_a: mark '5'+ 'a'; button_b: mark '6'+ 'b'; button_c: mark '7'+ 'c'; button_d: mark '8'+ 'd';
  }
}

void program() {
  timer1++;
  timer2++;
  timer3++;

  timer5++;
  timer6++;
  timer7++;
  timer8++;

}

TimedAction readbtThread = TimedAction(10, readbt);
TimedAction timerThread = TimedAction(1, program);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.print("setup!");
  Serial1.print("setup!");

  Serial.print("File   : "), Serial.println(__FILE__);
  Serial.print("Date   : "), Serial.println(__DATE__);

  pinMode(Start_Relay, OUTPUT);
  pinMode(Stop_Relay, OUTPUT);
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

}

void loop()
{
  Bluetooth();
  Motor();
}

void Bluetooth()
{
  // put your main code here, to run repeatedly:
  if (Serial1.available()) {
    while (Serial1.available() == 0);
    readbtThread.check();
    timerThread.check();
    if (v[1] == 1) {  // got the 'mark' of Joystick1(J1) indicated J1 is sending data
      Ok1 = 0;
      val1 = v[2];
      val2 = v[3];
    }
    if (v[1] == 2) {
      Ok2 = 0;
      val3 = v[2];
    }
    if (v[1] == 3) {
      Ok3 = 0;
      val4 = v[2];
    }
    /////////////////////////////////////////////MDF
    if (v[1] == 5) {
      Ok5 = 0;
      val5 = v[2];
    }
    if (v[1] == 6) {
      Ok6 = 0;
      val6 = v[2];
    }
    if (v[1] == 7) {
      Ok7 = 0;
      val7 = v[2];
    }
    if (v[1] == 8) {
      Ok8 = 0;
      val8 = v[2];
    }

    if (Ok5 == 0) {
      timerThread.check();
      if (v[1] == 5 && v[2] == 'a' ) {  //// got data from: MAI: when {a} Click
        Ok5 = 1;
        Serial1.println("test t and r");
        //   Serial.println("test t and r");
        //Serial.print("l21v[1]=");
        //Serial.println(v[1]);
      }
    }

    if (Ok6 == 0) {
      timerThread.check();
      if (v[1] == 6 && v[2] == 'b' ) {  //// got data from: MAI: when {a} Click
        Ok6 = 1;
        //   Serial1.println(v[2]);
        Serial1.print("v[2]=");
        Serial1.println(v[2]);
      }
    }
    /////////////////////////////////////////////MDF
  }
}

void Motor()
{
  xAxis = val1;
  yAxis = val2;

  //xAxis = 140;
  //yAxis = 140;

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

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

  if (yAxis > MedianL && yAxis < MedianH) {

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

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

  } else {

    if (xAxis > MedianL && xAxis < MedianH) {

      if (yAxis < MedianL) {
        forword();
      }
      if (yAxis > MedianH) {
        backword();
      }

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

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

    } else {

      if (yAxis < MedianL) {
        forword();
      }
      if (yAxis > MedianH) {
        backword();
      }

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

      if (xAxis > MedianH) {
        motorSpeedA = 255;
        motorSpeedB = map(xAxis, MedianH, 220, 255, 50);
      }
    }
  }
  analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}

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

  Serial.print("forword_motorSpeedA=");
  Serial.println(motorSpeedA);
  Serial.print("forword_motorSpeedB=");
  Serial.println(motorSpeedB);
}

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

  Serial.print("backword_motorSpeedA=");
  Serial.println(motorSpeedA);
  Serial.print("forword_motorSpeedB=");
  Serial.println(motorSpeedB);
}

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

  Serial.print("turnRight_motorSpeedA=");
  Serial.println(motorSpeedA);
  Serial.print("forword_motorSpeedB=");
  Serial.println(motorSpeedB);
}

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

  Serial.print("turnLeft_motorSpeedA=");
  Serial.println(motorSpeedA);
  Serial.print("forword_motorSpeedB=");
  Serial.println(motorSpeedB);
}

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

Serial Monitor:

lsetup!File   : E:\ENGINEERING\design_info\DIY\SNOWREMOVEROBOT

\00_A_GPS_BT_Robot\CODE\JMMAGCOR_V5.3_post\JMMAGCOR_V5.3_post.ino
Date   : Dec  9 2021
xAxis=0
yAxis=0
forword
forword_motorSpeedA=0
forword_motorSpeedB=0
xAxis=0
yAxis=0
forword
forword_motorSpeedA=131
forword_motorSpeedB=255
xAxis=0
yAxis=0
forword
forword_motorSpeedA=131
forword_motorSpeedB=255
xAxis=0
yAxis=0
forword
forword_motorSpeedA=131

Without being connected, what does Bluetooth() do?

What are the values used in Motor() and what is supposed to be changing or otherwise maintaining them?

Add some prininting, it’s almost guaranteed rpthat the code is doing exactly what you wrote, perhaps not what you intended.

a7

1 Like

Thanks.
its what made me confused what you asked. The motor start running as long as the code uploaded.

Looks like you're hitting this bit of code:

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

[/quote]

Since 131 is 85% of the way from 255 to 50, 'xAxis' should be 85% of the way from 'MedianL' (130) to 60. That would make 'xAxis' about 70 or 71.

Of course, if 'xAxis' is, indeed, 0, that would be outside the input range.
(0-60)/(130-60) = -85.7%
(255-50) * -85.7% = -175.7
-175.7 + 50 = -125 -> 0xFF83 -> 0x83 -> 131

So the problem is that you are using map() on a value outside the input range.

1 Like

genius johnwasser, thank you.

I commented out : if (xAxis < MedianL) { motorSpeedA = map(xAxis, MedianL, 60, 255, 50); motorSpeedB = 255; }
and the motor doesn't running before Bluetooth operating now.