Strange behavior of ESC for brushed motor

Hello all,
This my first post of a topic here. I apologize if i am mistaken the topic. I am making a program
which controls a robot car with a brushed motor via ESC. The robot - car has also an UltraSonic sensor. The goal of the program is to continuously moving until US sensor detects certain distance and when its done to stop the motor. I have used a do- while for the continuous slow movement in the beginning . When the US detects the distance it breaks from the loop and as i expected this should stop the motor. Unfortunately Preformatted textthe motor not only did not stop, it started on a maximum speed with a value which is for central position from the ESC. The ESC itself is calibrated properly because on the value for the slow movement it works fine. Another thing,the function which is Auto_Day_Lights_Check_And_Turn_Lights_Check() is invoking another one called Main_Lights() which is for controling the headlights. I saw that due to the for loops inside - they are causing this strange behaviour but i still dont understand why and how. Also when i put a delay before the calling the function Auto_Day_Lights_Check_And_Turn_Lights_Check() then the motor stop to rotate.

Can anyone help me and how to resolve this issue without delay and of course if it is possible to explain me why the motor starts to run on full speed ?

Components which i have used:
1 x Arduino Mega
1 x HC-SR04 UltraSonic sensor
1 x ESC quicrun 1080

Here is the code of mine:

#include <Adafruit_NeoPixel.h>
#include <Servo.h>
#ifdef ___AVR___
    #include <avr/power.h>
#endif

#define NORMAL_LIGHTS_PIN 34
#define REAR_LIGHTS_PIN 26
#define SIDE_LIGHTS_PIN 24
#define SPECIAL_LIGHTS_PIN 42
#define UPPER_FRONT_LIGHTS_PIN 44
#define ESC_PIN 6

#define NUM_UPPER_FRONT_LIGHTS 2
#define NUM_SPECIAL_LIGHT_CABIN 32
#define NUM_SIDE_LIGHTS 12
#define NUM_NORMAL_LIGHTS 2
#define NUM_REAR_LIGHTS 4

Servo rotateMotor; // rotateMotor is an object which is for he control of the brushed motor

Adafruit_NeoPixel normal_lights = Adafruit_NeoPixel(NUM_NORMAL_LIGHTS, NORMAL_LIGHTS_PIN, NEO_GRBW + NEO_KHZ800);
Adafruit_NeoPixel rear_lights = Adafruit_NeoPixel(NUM_REAR_LIGHTS, REAR_LIGHTS_PIN, NEO_GRBW + NEO_KHZ800);
Adafruit_NeoPixel side_lights = Adafruit_NeoPixel(NUM_SIDE_LIGHTS, SIDE_LIGHTS_PIN, NEO_GRBW + NEO_KHZ800);
Adafruit_NeoPixel special_light_cabin = Adafruit_NeoPixel(NUM_SPECIAL_LIGHTS, SPECIAL_LIGHTS_PIN, NEO_GRBW + NEO_KHZ800);
Adafruit_NeoPixel front_upper_lights = Adafruit_NeoPixel(NUM_UPPER_FRONT_LIGHTS, UPPER_FRONT_LIGHTS_PIN, NEO_GRBW + NEO_KHZ800);

int brightness_neopixel = 40;

int Counter_Turn_On_Or_Off_Hazard_Lights = 0;
boolean Activated_Day_Lights = false;
int counter_normal_lights = 0;
int counter_position_lights = 0;
int Main_Lights_Activated_Via_Bluetooth = 0;
int Position_Lights_Activated_Via_Bluetooth = 0;

float distance; //variable for measuring the distance from ultrasonic sensor
void setup()
{
 normal_lights.setBrightness(brightness_neopixel);
 normal_lights.begin();

 rear_lights.setBrightness(brightness_neopixel);
 rear_lights.begin();

 side_lights.setBrightness(brightness_neopixel);
 side_lights.begin();

 special_light_cabin.setBrightness(brightness_neopixel);
 special_light_cabin.begin();

 front_upper_lights.setBrightness(brightness_neopixel);
 front_upper_lights.begin();

 rotateMotor.attach(ESC_PIN);
 rotateMotor.writeMicroseconds(1500); //--------> CALIBRATING THE ESC IN NEUTRAL POSITION (ALSO STOP IT)
 delay(3000);
}

void Auto_Day_Lights_Check_And_Turn_Lights_Check()
{
 if(Activated_Day_Lights == false && counter_normal_lights % 2 == 0 && counter_position_lights % 2 == 0
    && Main_Lights_Activated_Via_Bluetooth % 2 == 0 && Position_Lights_Activated_Via_Bluetooth % 2 == 0)
 {
   Main_Lights(normal_lights.Color(0,0,0), front_upper_lights.Color(0,0,0), rear_lights.Color(0,0,0), side_lights.Color(0,0,0), special_lights.Color(0,0,0);
   digitalWrite(UPPER_REAR_LIGHTS,LOW);
 }

 if(Counter_Turn_On_Or_Off_Hazard_Lights % 2 != 0)
 {
  //HERE THE HAZARD LIGHTS BLINKING
 } 
}

void Main_Lights(uint32_t front_normal_lights, uint32_t upper_front_lights, uint32_t tail_lights, uint32_t normal_side_lights, uint32_t special_lights)
{
 int color_normal_front_lights = 0;
 int color_upper_front_lights = 0;
 int color_tail_lights = 0;
 int color_side_lights = 0;
 int color_special_lights = 0;
 
 for(; color_normal_front_lights < normal_lights.numPixels(), color_upper_front_lights < front_upper_lights.numPixels(), color_tail_lights < rear_lights.numPixels(); color_side_lights < side_lighs.numPixels(), color_special_lights < special_lights_cabin.numPixels(); color_normal_front_lights++, color_upper_front_lights++, color_tail_lights++, color_side_lights++, color_special_lights++)
 {
  normal_lights.setPixelColor(color_normal_front_lights, front_normal_lights);
  front_upper_lights.setPixelColor(color_upper_front_lights,upper_front_lights);
  rear_lights.setPixelColor(color_tail_lights, tail_lights);
  side_lights.setPixelColor(color_side_lights, normal_side_lights);
  special_light_cabin.setPixelColor(color_special_lights, special_lights);
 }
 normal_lights.show();
 front_upper_lights.show();
 rear_lights.show();
 side_lights.show();
 special_light_cabin.show();
}

void loop()
{
 if(distance > 15 && Counter_Turn_On_Or_Off_Hazard_Lights % 2 == 0)
 {
  do
  {
   rotateMotor.writeMircroseconds(1570); //----> MOVE SLOWLY FRORWARD
   if(distance > 40 && distannce < 60)
   {
    Counter_Turn_On_Or_Off_Hazard_Lights = 1;	
    break;
   }
  }while(distance > 15);
 }
 rotateMotor.writeMircroseconds(1500);
//delay(10) -----> with this delay the motor stops. Without the delay  - motor starts run on full speed
 Auto_Day_Lights_Check_And_Turn_Lights_Check();
}

That isn't the code you're using, it doesn't even compile. Not least because you frequentlyy misspell "Microseconds" as "Mircroseconds"

Steve

I saw that just now.My mistake.This is the code which i have used.Its just typing error from the writing.The problem its in the motor.Because when exits start to run on full speed.I can give the whole remain code

Here is the original code. Sorry about the previous i have tried to minimize it in the previous but it looks like it did not work. Here is the full code which now should be compiled.
I hope that someone can help me with this problem

#include <Adafruit_NeoPixel.h>
#include <Arduino.h>
#include <Servo.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#define NORMAL_LIGHTS_PIN 34
#define REAR_LIGHTS_PIN 26
#define SIDE_LIGHTS_PIN 24
#define UPPER_REAR_LIGHTS 40
#define CABIN_SPECIAL_LIGHT 42
#define UPPER_FRONT_LIGHTS 44

#define SIDE_US_TRIGGER_PIN 29
#define SIDE_US_ECHO_PIN 18 //-----------> FOR INTERRUPT

#define MOVING_SERVO 6

#define NUM_NORMAL_LIGHTS 2
#define NUM_REAR_LIGHTS 4
#define NUM_SIDE_LIGHTS 12
#define NUM_SPECIAL_LIGHT_CABIN 32
#define NUM_UPPER_FRONT_LIGHTS 2

#define MAXHUE 256*6

#define tINT 1 
#define pingDelay 50      // How many milliseconds between each measurement ; keep > 5ms
#define debugDelay 200    // How many milliseconds between each Serial.print ; keep > 200ms
#define soundSpeed 343.0  // Speed of sound in m/s

Servo moveServo;

Adafruit_NeoPixel normal_lights = Adafruit_NeoPixel(NUM_NORMAL_LIGHTS, NORMAL_LIGHTS_PIN, NEO_GRBW + NEO_KHZ800);
Adafruit_NeoPixel rear_lights = Adafruit_NeoPixel(NUM_REAR_LIGHTS, REAR_LIGHTS_PIN, NEO_GRBW + NEO_KHZ800);
Adafruit_NeoPixel side_lights = Adafruit_NeoPixel(NUM_SIDE_LIGHTS, SIDE_LIGHTS_PIN, NEO_GRBW + NEO_KHZ800);
Adafruit_NeoPixel special_light_cabin = Adafruit_NeoPixel(NUM_SPECIAL_LIGHT_CABIN, CABIN_SPECIAL_LIGHT, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel front_upper_lights = Adafruit_NeoPixel(NUM_UPPER_FRONT_LIGHTS, UPPER_FRONT_LIGHTS, NEO_GRB + NEO_KHZ800);


byte neopix_gamma[] = {
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,
    1,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  2,  2,
    2,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  5,  5,  5,
    5,  6,  6,  6,  6,  7,  7,  7,  7,  8,  8,  8,  9,  9,  9, 10,
   10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
   17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
   25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
   37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
   51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
   69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
   90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
  115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
  144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
  177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
  215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 };

int bright_normal_lights = 150;
int bright_rear_lights = 70;
int bright_side_ligts = 25;
int bright_special_lights = 10;
int bright_front_upper_lights = 15;

//STATES ABOUT LEFT TURN SIGNAL, RIGHT TURN SIGNAL AND HAZARD LIGHTS IN NORMAL WAY - IF THERE IS NO STEERING PROCESS
int blink_state_hazard = LOW;

//TIMER INTERVALS FOR BLINKING THE TURN SIGNALS AND HAZARD LIGHTS
long previousMillis_Hazard = 0;
long interval_Hazard = 350;

//START ON/OFF COUNTERS
int counter_normal_lights = 0;
int counter_auto_day_lights = 0;
int counter_position_lights = 0;

//COUNTER FOR LEFT TURN SIGNAL, RIGHT TURN SIGNAL AND HAZARD LIGHTS
int Counter_Turn_On_And_Off_Hazard_Lights_Signal = 0;


//ACTIVATE OR NOT DAY LIGHTS
boolean Activated_Day_Lights = false;

int Main_Lights_Activated_Via_Bluetooth = 0;
int Long_Lights_Activated_Via_Bluetooth = 0;
int Fog_Lights_Activated_Via_Bluetooth = 0;
int Position_Lights_Activated_Via_Bluetooth = 0;
int Left_Turn_Signal_Activated_Via_Bluetooth = 0;
int Right_Turn_Signal_Activated_Via_Bluetooth = 0;
int Hazard_Lights_Activated_Via_Bluetooth = 0;
int Previous_Left_Via_Bluetooth = 0;
int Previous_Right_Via_Bluetooth = 0;

float distanceSide;

volatile unsigned long travelTime[tINT];  // Place to store traveltime of the pusle
volatile unsigned long startTime[tINT];   // Place to store ping times (interrupt)
float distance[tINT];                     // Calculated distances in cm
unsigned long lastPollMillis;
unsigned long lastDebugMillis;

int sensor_us_counter;

void setup()
{
 #if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
 #endif
 Serial.begin(57600);
 normal_lights.setBrightness(bright_normal_lights);
 normal_lights.begin();
 normal_lights.show();

 front_upper_lights.setBrightness(bright_front_upper_lights);
 front_upper_lights.begin();
 front_upper_lights.show();

 rear_lights.setBrightness(bright_rear_lights);
 rear_lights.begin();
 rear_lights.show();

 side_lights.setBrightness(bright_side_ligts);
 side_lights.begin();
 side_lights.show();

 special_light_cabin.setBrightness(bright_special_lights);
 special_light_cabin.begin();
 special_light_cabin.show();

 pinMode(UPPER_REAR_LIGHTS,OUTPUT);

 pinMode(SIDE_US_TRIGGER_PIN,OUTPUT);

 pinMode(18,INPUT);
 
 attachInterrupt(digitalPinToInterrupt(18), call_INT0, CHANGE );   // ISR for INT0

 moveServo.attach(MOVING_SERVO);
 moveServo.writeMicroseconds(1500); //--------> CALIBRATING THE ESC IN NEUTRAL POSITION (ALSO STOP IT)
 delay(3000);
}


void Main_Lights(uint32_t front_normal_lights,uint32_t front_upper_normal_lights, uint32_t rear_normal_lights, uint32_t side_normal_lights, uint32_t special_lights_bar)
{
 int color_front = 0;
 int color_upper_front = 0;
 int color_rear = 0;
 int color_side = 0;
 int color_special_lights = 0;

 for(; color_front < normal_lights.numPixels(), color_upper_front < front_upper_lights.numPixels(), color_rear < rear_lights.numPixels(), color_side < side_lights.numPixels(), color_special_lights < special_light_cabin.numPixels(); color_front++, color_upper_front++, color_rear++, color_side++, color_special_lights+=3)
 {
  normal_lights.setPixelColor(color_front, front_normal_lights);
  front_upper_lights.setPixelColor(color_upper_front, front_upper_normal_lights);
  rear_lights.setPixelColor(color_rear, rear_normal_lights);
  side_lights.setPixelColor(color_side, side_normal_lights);
  special_light_cabin.setPixelColor(color_special_lights, special_lights_bar); 
 }

 normal_lights.show();
 front_upper_lights.show();
 rear_lights.show();
 side_lights.show();
 special_light_cabin.show();
 digitalWrite(UPPER_REAR_LIGHTS,HIGH);
}

void call_INT0()
{
  byte pinRead;
   pinRead = digitalRead(18);      // Slower ; Read pin 2
  //pinRead = PIND >> 18 & B00000001;  // Faster ; Read pin 2/PD2
  interruptHandler(pinRead, 0);
}

// Common function for interrupts
void interruptHandler(bool pinState, int nIRQ)
{
  unsigned long currentTime = micros();  // Get current time (in µs)
  if (pinState)
  {
    // If pin state has changed to HIGH -> remember start time (in µs)
    startTime[nIRQ] = currentTime;
  }
  else
  {
    // If pin state has changed to LOW -> calculate time passed (in µs)
    travelTime[nIRQ] = currentTime - startTime[nIRQ];
  }
}

void doMeasurement()
{
  // First read will be 0 (no distance  calculated yet)

  // Read the previous result (pause interrupts while doing so)
  noInterrupts();   // cli()
  for (int i = 0; i < tINT; i++)
  {
    distance[i] = travelTime[i] / 2.0 * (float)soundSpeed / 10000.0;   // in cm
  }
  interrupts();   // sei();

  // Initiate next trigger
  // digitalWrite(triggerPin, LOW);  // rest of loop already takes > 2µs
  // delayMicroseconds(2);
  digitalWrite(SIDE_US_TRIGGER_PIN, HIGH);
  delayMicroseconds(1);
  digitalWrite(SIDE_US_TRIGGER_PIN, LOW);
}

void Auto_Day_Lights_Check_And_Turn_Lights_Check()
{
 if(Activated_Day_Lights == false && counter_normal_lights % 2 == 0 && counter_position_lights % 2 == 0 && Main_Lights_Activated_Via_Bluetooth % 2 == 0 && Position_Lights_Activated_Via_Bluetooth % 2 == 0)
 {
  Main_Lights(normal_lights.Color(0,0,0), front_upper_lights.Color(0,0,0), rear_lights.Color(0,0,0), side_lights.Color(0,0,0), special_light_cabin.Color(0,0,0));
  digitalWrite(UPPER_REAR_LIGHTS,LOW);
 }
//
}

void loop()
{
 if (millis() - lastPollMillis >= pingDelay)
  {
    doMeasurement();
    lastPollMillis = millis();
  }

  // Print every y ms (comment out in production)
  if (millis() - lastDebugMillis >= debugDelay)
  {
    for (sensor_us_counter = 0; sensor_us_counter < tINT; sensor_us_counter++) {
      Serial.print(distance[sensor_us_counter]);
      Serial.print(" - ");
    }
    Serial.println();
    lastDebugMillis = millis();
  }
if(distance[0] > 15 && Counter_Turn_On_And_Off_Hazard_Lights_Signal % 2 == 0)
  {
  do
  {
   if (millis() - lastPollMillis >= pingDelay)
   {
    doMeasurement();
    lastPollMillis = millis();
   }
   if (millis() - lastDebugMillis >= debugDelay)
   {
    for (sensor_us_counter = 0; sensor_us_counter < tINT; sensor_us_counter++) 
   {
      Serial.print(distance[sensor_us_counter]);
      Serial.print(" - ");
   }
    Serial.println();
    lastDebugMillis = millis();
  }
   moveServo.writeMicroseconds(1570); //----> MOVE SLOWLY FORWARD
   if(distance[0] > 40 && distance[0] < 60)
   {
    Counter_Turn_On_And_Off_Hazard_Lights_Signal = 1;
    break;
   }else
   if(distance[0] < 15)
   {
    break;
   }
  }while(distance[0] > 15);
  }
  moveServo.writeMicroseconds(1500);
  //delay(10); -----> with this delay the motor stops. Without the delay  - motor starts run on full speed
  Auto_Day_Lights_Check_And_Turn_Lights_Check();
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.