Hi, I have a joystick controlled car. The joystick is read via analog input and the car moves accordingly. I also have a HC-SR04 ultrasonic sensor mounted, which reads distance. When the car is 25cm from an object, the car stops and no longer goes forward. I attempted to code this, but if I hold down the joystick, it skips the disable feature and still hits a wall.
If the car is inert, it considers the disable feature and doesn't allow the car to continue forward (if 25cm or less from wall).
Im wondering if there's a way to constantly check the disable feature while checking the joystick value at the same time. Any feedback would be great! Thanks guys!
Ignore lcd and servo stuff!
/Servo and Echo Variables and such
#include <Servo.h> // include file for servo control
int pin_trig = 11; // trigger pin connected to ultrasonic sensor
int pin_echo = 9; // echo pin connected to ultrasonic sensor
Servo servo1; // create servo object to control servo #1
int position = 25 ; // state variable
boolean forward = false ; // state variable
unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
const long interval = 2000;
int trigState = LOW;
int printState = LOW;
const long interval2 = 1;
const long interval3 = 500;
//LCD STUFF
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(52, 51, 50, 49, 48, 47);
const long interval4 = 1000;
unsigned long previousMillis4 = 0;
//H-Bridge Variables and such
int motorSpeedA = 0;
int motorSpeedB = 0;
int Disable_forward = 0;
#define enA 2
#define in1 3
#define in2 4
#define enB 7
#define in3 5
#define in4 6
void setup() {
//Servo Tower initialization stuff
servo1.attach(10); // connect pin 7 to servo #1
pinMode(pin_trig, OUTPUT);
pinMode(pin_echo, INPUT);
Serial.begin(9600);
//H-Brdige initialization Stuff
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
//LCD (Fun Initialization) STUFF
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Welcome");
delay (2000);
lcd.setCursor(0, 1);
lcd.print("My name is Walle");
delay(3000);
lcd.clear();
delay(1000);
for (int i= 1; i <=3; i++){
int counter = 4;
lcd.setCursor(0, 0);
lcd.print("Starting in...");
lcd.setCursor(0, 1);
lcd.print (counter - i);
delay(1000);
}
}
void loop() {
//H-Bridge Controls
int xAxis = analogRead(A0); // Read Joysticks X-axis
int yAxis = analogRead(A1); // Read Joysticks Y-axis
if (yAxis < 470) {
// Set Motor A forward
if(Disable_forward == 1){
Serial.println("\nForward is disabled");
}
else{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B backward
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
motorSpeedA = map(yAxis, 470, 0, 0, 255);
motorSpeedB = map(yAxis, 470, 0, 0, 255);
}
}
else if (yAxis > 550) {
// Set Motor A backwards
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B forward
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
motorSpeedA = map(yAxis, 550, 1023, 0, 255);
motorSpeedB = map(yAxis, 550, 1023, 0, 255);
}
// If joystick stays in middle the motors are not moving
else {
motorSpeedA = 0;
motorSpeedB = 0;
}
// X-axis used for left and right control
if (xAxis < 470) {
// Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
int xMapped = map(xAxis, 470, 0, 0, 255);
// Move to right
motorSpeedA = motorSpeedA + xMapped;
motorSpeedB = motorSpeedB - xMapped;
// Confine the range from 0 to 255
if (motorSpeedB < 0) {
motorSpeedB = 0;
}
if (motorSpeedA > 255) {
motorSpeedA = 255;
}
}
if (xAxis > 550) {
// Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
int xMapped = map(xAxis, 550, 1023, 0, 255);
// Move left
motorSpeedA = motorSpeedA - xMapped;
motorSpeedB = motorSpeedB + xMapped;
// Confine the range from 0 to 255
if (motorSpeedB > 255) {
motorSpeedB = 255;
}
if (motorSpeedA < 0) {
motorSpeedA = 0;
}
}
// Prevent buzzing at low speeds
if (motorSpeedA < 70) {
motorSpeedA = 0;
}
if (motorSpeedB < 70) {
motorSpeedB = 0;
}
analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
//Servo + Echo Controls
//Servo Section
unsigned long currentMillis = millis();
unsigned long currentMillis2 = millis();
unsigned long currentMillis3 = millis();
int analog_input0; // analog input for pin A0
float voltage0; // voltage input for pin A0
//servo
if (currentMillis - previousMillis >= interval){
previousMillis += interval;
if (forward){
servo1.write (position = position - 15);
if (position ==25)
forward = false;
}else{
servo1.write (position = position +15);
if (position == 70)
forward = true;
}
}
//Ultrasonic Sensor Section
if (currentMillis2-previousMillis2 >= interval2) {
previousMillis2 = currentMillis2;
if (trigState == LOW){ //if no pulse then pulse
(trigState = HIGH);
}
else { //if pulsed, then stop pulse
(trigState = LOW);
}
}
// printing if statement
if (currentMillis2-previousMillis2 >= interval3) {
previousMillis2 = currentMillis2;
if (printState == LOW){
(printState = HIGH);
}
else {
(printState = LOW);
}
}
digitalWrite(pin_trig,trigState);
float duration;
float distance;
float true_distance;
float c = 343; //speed of sounds (m/s)
duration = pulseIn(pin_echo,HIGH,300000);//pulseIn records time between high and low. 300,000us = 300milli sec delay
duration *= 1.0e-6; //Convert to seconds
distance = c*100* duration/2 ; // distance in cm
if (distance != 0.0 && distance <400){ //Range on sensor is 400cm
true_distance = distance;
}
if (printState = HIGH){
Serial.print ("\ndistance (cm) = ");
Serial.print(true_distance);
}
if (true_distance <= 25.0){
Disable_forward = 1;
}
else {
Disable_forward = 0;
}
//LCD STUFF
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
if (Disable_forward != 1){
lcd.setCursor(0, 0);
lcd.print("Distance: ");
lcd.setCursor(0, 1);
if (currentMillis3 - previousMillis4 >= interval4){
previousMillis4 += interval4;
lcd.print(true_distance,1);
lcd.setCursor(5,1);
lcd.print(" ");
}
}
if (Disable_forward == 1){
if (currentMillis3 - previousMillis4 >= interval4){
previousMillis4 += interval4;
lcd.setCursor(0, 0);
lcd.print("Forward disabled");
lcd.setCursor(0,1);
lcd.print("Back Up");
// print the number of seconds since reset:
}
}
}