Hello.I have a robot and I have a main function (name: AUTO_FUNCTION() ) where the robot goes forward and if the ultrasonic find an obstacle ,the robot turns right or left accordingly the distances that sensor "read".
Also,I have 2 buttons and when I press the left button(for example) ,I call the AUTO_FUNCTION() ,and when I press the right button,I call the STOP() funtion.
but the problem is that the sensor does nothing .
After ,when I press the right button ,the robot stops(as I said) ,but after that when I press agai any button ,nothig happens...
// My problem is only the buttons ,nothing else... <<<---------
// Do not give much importance to other things...
Here is my code:
const int IN_1_back_left = 53;
const int IN_2_back_left = 51;
const int PWM_back_left = 2;
const int IN_1_back_right = 49;
const int IN_2_back_right = 47;
const int PWM_back_right = 3;
const int IN_1_front_left = 45;
const int IN_2_front_left = 43;
const int PWM_front_left = 4;
const int IN_1_front_right = 41;
const int IN_2_front_right = 39;
const int PWM_front_right = 3;
int prev_button_state_auto = LOW;
int prev_button_state_stop = LOW;
const int button_for_auto = 13;
const int button_for_stop = 37;
const int led = 9;
int ledState = LOW;
boolean flash_enabled = false;
boolean enable_forward = true;
boolean enable_stop;
boolean enable_right;
boolean enable_left;
boolean enable_rotate_AUTO = false;
const int Trig_pin_front = 8;
const int Echo_pin_front = 10;
const int Trig_pin_right = 6;
const int Echo_pin_right = 7;
const int Trig_pin_left = 11;
const int Echo_pin_left = 12;
long duration_front , duration_right , duration_left;
float distance_front , distance_right , distance_left;
unsigned long presentMillis = 0;
unsigned long prev_Millis = 0;
void setup()
{
pinMode(button_for_auto, INPUT);
pinMode(button_for_stop, INPUT);
pinMode(led, OUTPUT);
pinMode(Echo_pin_front , INPUT);
pinMode(Trig_pin_front , OUTPUT);
pinMode(Echo_pin_right , INPUT);
pinMode(Trig_pin_right , OUTPUT);
pinMode(Echo_pin_left , INPUT);
pinMode(Trig_pin_left , OUTPUT);
analogWrite(PWM_back_left , 255);
analogWrite(PWM_back_right , 255);
analogWrite(PWM_front_left , 255);
analogWrite(PWM_front_right , 255);
pinMode(PWM_back_left, OUTPUT);
pinMode(PWM_back_right, OUTPUT);
pinMode(PWM_front_left, OUTPUT);
pinMode(PWM_front_right, OUTPUT);
pinMode(IN_1_back_left, OUTPUT);
pinMode(IN_2_back_left, OUTPUT);
pinMode(IN_1_back_right, OUTPUT);
pinMode(IN_2_back_right, OUTPUT);
pinMode(IN_1_front_left, OUTPUT);
pinMode(IN_2_front_left, OUTPUT);
pinMode(IN_1_front_right, OUTPUT);
pinMode(IN_2_front_right, OUTPUT);
}
void ultrasonic_sensor_forward()
{
digitalWrite(Trig_pin_front , HIGH);
delayMicroseconds(11);
digitalWrite(Trig_pin_front , LOW);
duration_front = pulseIn(Echo_pin_front , HIGH);
distance_front = duration_front * 0.034 / 2;
}
void ultrasonic_sensor_right()
{
digitalWrite(Trig_pin_right , HIGH);
delayMicroseconds(11);
digitalWrite(Trig_pin_right , LOW);
duration_right = pulseIn(Echo_pin_right , HIGH);
distance_right = duration_right * 0.034 / 2;
}
void ultrasonic_sensor_left()
{
digitalWrite(Trig_pin_left , HIGH);
delayMicroseconds(11);
digitalWrite(Trig_pin_left , LOW);
duration_left = pulseIn(Echo_pin_left , HIGH);
distance_left = duration_left * 0.034 / 2;
}
void led_on_off()
{
unsigned long currentTime = millis();
if (currentTime - prev_Millis >= 100)
{
prev_Millis = currentTime;
if (ledState == LOW)
{
ledState = HIGH;
}
else
{
ledState = LOW;
}
digitalWrite(led, ledState);
}
}
void check_blink_led()
{
if (flash_enabled)
{
led_on_off();
}
else
{
led_off();
}
}
void led_off()
{
ledState = LOW;
digitalWrite(led, ledState);
}
void FORWARD()
{
flash_enabled = false;
digitalWrite(IN_1_back_left , HIGH); //fordward
digitalWrite(IN_2_back_left , LOW);
digitalWrite(IN_1_back_right , HIGH);
digitalWrite(IN_2_back_right , LOW);
digitalWrite(IN_1_front_left , HIGH);
digitalWrite(IN_2_front_left , LOW);
digitalWrite(IN_1_front_right , HIGH);
digitalWrite(IN_2_front_right , LOW);
}
void BACKWARD()
{
flash_enabled = false;
digitalWrite(IN_1_back_left , LOW); //backward
digitalWrite(IN_2_back_left , HIGH);
digitalWrite(IN_1_back_right , LOW);
digitalWrite(IN_2_back_right , HIGH);
digitalWrite(IN_1_front_left , LOW);
digitalWrite(IN_2_front_left , HIGH);
digitalWrite(IN_1_front_right , LOW);
digitalWrite(IN_2_front_right , HIGH);
}
void STOP()
{
flash_enabled = true;
digitalWrite(IN_1_back_left , LOW); //stop
digitalWrite(IN_2_back_left , LOW);
digitalWrite(IN_1_back_right , LOW);
digitalWrite(IN_2_back_right , LOW);
digitalWrite(IN_1_front_left , LOW);
digitalWrite(IN_2_front_left , LOW);
digitalWrite(IN_1_front_right , LOW);
digitalWrite(IN_2_front_right , LOW);
}
void RIGHT()
{
flash_enabled = false;
digitalWrite(IN_1_back_left , HIGH); //right
digitalWrite(IN_2_back_left , LOW);
digitalWrite(IN_1_back_right , LOW);
digitalWrite(IN_2_back_right , HIGH);
digitalWrite(IN_1_front_left , HIGH);
digitalWrite(IN_2_front_left , LOW);
digitalWrite(IN_1_front_right , LOW);
digitalWrite(IN_2_front_right , HIGH);
}
void LEFT()
{
flash_enabled = false;
digitalWrite(IN_1_back_left , LOW); //left
digitalWrite(IN_2_back_left , HIGH);
digitalWrite(IN_1_back_right , HIGH);
digitalWrite(IN_2_back_right , LOW);
digitalWrite(IN_1_front_left , LOW);
digitalWrite(IN_2_front_left , HIGH);
digitalWrite(IN_1_front_right , HIGH);
digitalWrite(IN_2_front_right , LOW);
}
void AUTO_FUNCTION()
{
ultrasonic_sensor_forward();
ultrasonic_sensor_right();
ultrasonic_sensor_left();
check_blink_led();
unsigned long currentMillis = millis();
if (enable_forward)
{
if (currentMillis - presentMillis >= 500)
{
FORWARD();
enable_forward = false;
enable_stop = true;
presentMillis = currentMillis;
}
}
if (enable_stop && distance_front <= 10)
{
if (currentMillis - presentMillis >= 600)
{
STOP();
enable_stop = false;
enable_rotate_AUTO = true;
presentMillis = currentMillis;
}
}
if (enable_rotate_AUTO)
{
if (currentMillis - presentMillis >= 500)
{
presentMillis = currentMillis;
if (distance_right > distance_left)
{
RIGHT();
enable_forward = true;
enable_rotate_AUTO = false;
}
else if (distance_right < distance_left)
{
LEFT();
enable_forward = true;
enable_rotate_AUTO = false;
}
}
}
}
void loop()
{
int button_after_auto = digitalRead(button_for_auto);
int button_after_stop = digitalRead(button_for_stop);
if (prev_button_state_auto == LOW && button_after_auto == HIGH)
{
AUTO_FUNCTION();
}
else if (prev_button_state_stop == LOW && button_after_stop == HIGH)
{
STOP();
}
}
Before put the buttons in the circuit ,the code with the AUTO_FUNCTION was running normal
So ,the problem is the buttons...
Thanks a lot and I hope you gonna help me!!!