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 text
the 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();
}