Trying to use joystick app for arduino bluetooth controlled car with HC-SR04 sensor

I wanted to impliment a joystick instead of the normal wasd movement to my arduino bluetooth conrolled car that has a HC SR04 sensor. The joystick by itself works perfectly but whenever i try to implement the code for the sensor, my joystick inputs take 20 to 30 seconds to register, if they even register. Does anyone know if this is possible to implement and how to do so.

//

int dataIn[5] {0,0,0,0}; 
int in_byte = 0;
int array_index = 0;

int TRIGGER=3;
int echo=4;
int last;
long duration;
int distance;
 unsigned long previousMicros=0;

#define in1 10 
#define in2 11
#define in3 12
#define in4 13



void setup() {
Serial.begin (9600); 

   pinMode (TRIGGER,OUTPUT);
  pinMode (echo, INPUT);
    pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  
  
}

void loop() {

/*
  unsigned long currentMicros = micros();
digitalWrite(TRIGGER,LOW);
if (currentMicros - previousMicros > 4){
  digitalWrite (TRIGGER,HIGH);
  previousMicros = currentMicros;
}
if (currentMicros - previousMicros > 12){
  digitalWrite (TRIGGER,LOW);
  previousMicros = currentMicros;
}

duration= pulseIn(echo,HIGH);
distance=duration / 58;
int trenutno = distance;
Serial.println(last);

if (trenutno!=0)
last=trenutno;
if (last<20 && digitalRead(in1)==HIGH && digitalRead(in2)==LOW &&digitalRead(in3)==HIGH &&digitalRead(in4)==LOW){
 digitalWrite(in1, LOW);
        digitalWrite(in2, LOW);
        digitalWrite(in3, LOW);
        digitalWrite(in4, LOW);
}*/

 if (Serial.available() > 0) {  
  in_byte = Serial.read(); 
  if (in_byte == (255)) { 
    array_index = 0;
  }
  dataIn[array_index] = in_byte;  
  array_index = array_index +1;
}
//print the array
Serial.print (", X:");
Serial.print (dataIn[2]);
Serial.print (", Y:");
Serial.print (dataIn[3]);
Serial.println (",");


int x = dataIn[2];
int y = dataIn[3];
unsigned long currentMicros = micros();
digitalWrite(TRIGGER,LOW);
if (currentMicros - previousMicros > 4){
  digitalWrite (TRIGGER,HIGH);
  previousMicros = currentMicros;
}
if (currentMicros - previousMicros > 12){
  digitalWrite (TRIGGER,LOW);
  previousMicros = currentMicros;
}

duration= pulseIn(echo,HIGH);
distance=duration / 58;
int trenutno = distance;
Serial.println(last);




if (x==100 && y==100){  //stoji
   digitalWrite(in1, LOW);
        digitalWrite(in2, LOW);
        digitalWrite(in3, LOW);
        digitalWrite(in4, LOW);
}
else if (y>=0 && y<=50 && x>=50 && x<=150 && last>20){  
   digitalWrite(in1, HIGH);
        digitalWrite(in2, LOW);
        digitalWrite(in3, HIGH);
        digitalWrite(in4, LOW);
}
else if (y>=200 && y<=250 && x>=50 && x<=150) {
   digitalWrite(in1, LOW);
        digitalWrite(in2, HIGH);
        digitalWrite(in3, LOW);
        digitalWrite(in4, HIGH);
}
else if (y>=50 && y<=150 && x>=200 && x<=250) {
 digitalWrite(in1, HIGH);
        digitalWrite(in2, LOW);
        digitalWrite(in3, LOW);
        digitalWrite(in4, HIGH);

}
else if (y>50 && y<=150 && x>=0 && x<=50){ 

        digitalWrite(in1, LOW);
        digitalWrite(in2, HIGH);
        digitalWrite(in3, HIGH);
        digitalWrite(in4, LOW);
}
else if (y>0 && y<=50 && x>=200 && x<=250){ 

        digitalWrite(in1, HIGH);
        digitalWrite(in2, LOW);
        digitalWrite(in3, LOW);
        digitalWrite(in4, LOW);
}
else if (y>0 && y<=50 && x>=0 && x<=50){ 

        digitalWrite(in1, LOW);
        digitalWrite(in2, LOW);
        digitalWrite(in3, HIGH);
        digitalWrite(in4, LOW);
}
else if (y>200 && y<=250 && x>=200 && x<=250){ 

        digitalWrite(in1, LOW);
        digitalWrite(in2, HIGH);
        digitalWrite(in3, LOW);
        digitalWrite(in4, LOW);
}
else if (y>200 && y<=250 && x>=0 && x<=50){ 
        digitalWrite(in1, LOW);
        digitalWrite(in2, LOW);
        digitalWrite(in3, LOW);
        digitalWrite(in4, HIGH);
}

}

Without looking further, I see much delay here because you did not put a timeout value in the call to pluseln(). Therefore if no pulse end is detected, the function will wait for a second. Furthermore, if the end of the pulse is not detected, ZERO is returned, but you go right ahead and compute as if some real value was returned.
Some of this may be the time problems you are experiencing.

Thank you bro, i have been stuck on this for about 8 hours, never knew that the pulseIn function has a 1 sec delay and that it can return 0. The program now works perfectly.

That is great! Always program with the documentation close by!

1 Like

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