Using a "Ultrasonic distance sensor" to stop a car (using case's)

Hi

Anyone know how I can get a "ultrasonic distance sensor" to stop a arduino car?
I'm using "case" to control the car (using IR remote) and I would like the car to stop before it crashes.

I have several cases in my code, so I would like the "ultrasonic distance sensor" to activate my "stop" case. How do I do that?

Mabye you could modify my code?

#include <IRremote.h> //Inkluderer biblioteket for IR kontroll

const int mhp = 8;    //MotorHøyrePositiv
const int mhn = 9;    //MotorHøyreNegativ
const int mvp = 10;   //MotorVenstrePositiv
const int mvn = 11;   //MotorVenstreNegativ

const int trigPin = 6;  //
const int echoPin = 7;  //

const int RECV_PIN = 5;
IRrecv irrecv(RECV_PIN);
decode_results results;
const int svingetid = 400;    //Bestemmer hvor lenge skal bilen svinge

void setup(){
  
 Serial.begin(9600);
 pinMode(3, OUTPUT);  //Kjørelys
 pinMode(4, OUTPUT);  //Blått lys (indikerer at kjørelys er PÅ)
   
  
 pinMode(mhp, OUTPUT);
 pinMode(mhn, OUTPUT);
 pinMode(mvp, OUTPUT);
 pinMode(mvn, OUTPUT);
 
 digitalWrite(mhp, LOW);
 digitalWrite(mhn, LOW);  //Bilen står stille når den skrus på
 digitalWrite(mvp, LOW);  //...venter så på kommando
 digitalWrite(mvn, LOW);
 
 irrecv.enableIRIn();

} 
 
void stopp(){    //   >>|
 digitalWrite(mhp, LOW);
 digitalWrite(mhn, LOW);
 digitalWrite(mvp, LOW);
 digitalWrite(mvn, LOW);
}
  
void fremover(){ //   CH
 digitalWrite(mhp, LOW);
 digitalWrite(mhn, HIGH);
 digitalWrite(mvp, HIGH);
 digitalWrite(mvn, LOW);
}

void bakover(){  //   +
 digitalWrite(mhp, HIGH);
 digitalWrite(mhn, LOW);
 digitalWrite(mvp, LOW);
 digitalWrite(mvn, HIGH);
}

void venstre(int svingetid){  //   |<<
 digitalWrite(mhp, HIGH);
 digitalWrite(mhn, LOW);
 digitalWrite(mvp, HIGH);
 digitalWrite(mvn, LOW);
 delay(svingetid);
 digitalWrite(mhp, LOW);
 digitalWrite(mhn, LOW);
 digitalWrite(mvp, LOW);
 digitalWrite(mvn, LOW);

}

void hoyre(int svingetid){    //   >||
 digitalWrite(mhp, LOW);
 digitalWrite(mhn, HIGH);
 digitalWrite(mvp, LOW);
 digitalWrite(mvn, HIGH);
 delay(svingetid);
 digitalWrite(mhp, LOW);
 digitalWrite(mhn, LOW);
 digitalWrite(mvp, LOW);
 digitalWrite(mvn, LOW);
}

//IR, MOTOR - START

void irkontroll(){  
switch (results.value){
 
case 16712445:  //STOPP     >>|
stopp();
break;
   
case 16736925:  //Fremover  CH
fremover();
break;

case 16754775:  //Bakover    +
bakover();
break;

case 16720605:  //Venstre   |<<
venstre(svingetid);
stopp();
break;

case 16761405:  //Høyre     >||
hoyre(svingetid);
stopp();
break;

default:
;
}
} 
 
//IR, MOTOR - SLUTT

void loop(){


}

Thanks

You need to take range measurements, and if the range falls below a threshold, you call your stop function.
There is no need for this code to be any part of your switch/case structure.

You also probably want to get rid of any calls to "delay" - you wouldn't drive a real car by opening your eyes, taking a quick look around and shutting them again.

I can't see where you call "irkontroll", so I don't know how that code works.

Okey.. Can I use

if (distance >= maximumRange || distance <= minimumRange){
  
}

... in my code to set a case?

That has got nothing to do with your switch, so no, you don't need to "set a case", you just call the stop function.

Okey... eehh.. This is quite new to me, could you give me an example?

Thanks

if (distance >= maximumRange || distance <= minimumRange){
  stopp();  
}

Thanks!

The problem I have now is that nothing happens when I use the IR remote.. If I use one of my old codes, the IR works.. Can you see anything in my code that's not correct?

#include <IRremote.h> //Inkluderer biblioteket for IR kontroll

const int ldr = A7;   //Lyssensor (LDR)
const int led = 3;    //Kjørelys
const int blaa1 = 4;  
const int lys = 300;  //Laveste verdi for lys (styrer kjørelyset)

const int mhp = 8;    //MotorHøyrePositiv
const int mhn = 9;    //MotorHøyreNegativ
const int mvp = 10;   //MotorVenstrePositiv
const int mvn = 11;   //MotorVenstreNegativ

const int RECV_PIN = 5;
IRrecv irrecv(RECV_PIN);
decode_results results;
const int svingetid = 400;

void setup(){
  
 Serial.begin(9600);
 pinMode(3, OUTPUT);  //Kjørelys
 pinMode(4, OUTPUT);  //Blått lys (indikerer at kjørelys er PÅ)
 
 irrecv.enableIRIn();  
  
 pinMode(mhp, OUTPUT);
 pinMode(mhn, OUTPUT);
 pinMode(mvp, OUTPUT);
 pinMode(mvn, OUTPUT);
 
 digitalWrite(mhp, LOW);
 digitalWrite(mhn, LOW);
 digitalWrite(mvp, LOW);
 digitalWrite(mvn, LOW);

} 
  
void stopp(){    //   >>|
 digitalWrite(mhp, LOW);
 digitalWrite(mhn, LOW);
 digitalWrite(mvp, LOW);
 digitalWrite(mvn, LOW);
}
  
void fremover(){ //   CH
 digitalWrite(mhp, LOW);
 digitalWrite(mhn, HIGH);
 digitalWrite(mvp, HIGH);
 digitalWrite(mvn, LOW);
}

void bakover(){  //   +
 digitalWrite(mhp, HIGH);
 digitalWrite(mhn, LOW);
 digitalWrite(mvp, LOW);
 digitalWrite(mvn, HIGH);
}

void venstre(int svingetid){  //   |<<
 digitalWrite(mhp, HIGH);
 digitalWrite(mhn, LOW);
 digitalWrite(mvp, HIGH);
 digitalWrite(mvn, LOW);
 delay(svingetid);
 digitalWrite(mhp, LOW);
 digitalWrite(mhn, LOW);
 digitalWrite(mvp, LOW);
 digitalWrite(mvn, LOW);
}

void hoyre(int svingetid){    //   >||
 digitalWrite(mhp, LOW);
 digitalWrite(mhn, HIGH);
 digitalWrite(mvp, LOW);
 digitalWrite(mvn, HIGH);
 delay(svingetid);
 digitalWrite(mhp, LOW);
 digitalWrite(mhn, LOW);
 digitalWrite(mvp, LOW);
 digitalWrite(mvn, LOW);
}

//IR, MOTOR - START

void irkontroll(){  
switch (results.value){
 
  
case 16712445:  //STOPP     >>|
stopp();
break;
   
case 16736925:  //Fremover  CH
fremover();
break;

case 16754775:  //Bakover    +
bakover();
break;

case 16720605:  //Venstre   |<<
venstre(svingetid);
stopp();
break;

case 16761405:  //Høyre     >||
hoyre(svingetid);
stopp();
break;

default:
;
}
} 
 
//IR, MOTOR - SLUTT

void loop(){ 

//LYS - START 
  
int lysverdi = analogRead(ldr);
  Serial.println(lysverdi);
  
  if (lysverdi < lys)
  digitalWrite(led, HIGH);
  else
  digitalWrite(led, LOW);
  
  if (lysverdi < lys)
  digitalWrite(blaa1, HIGH);
  else
  digitalWrite(blaa1, LOW);
  
  delay(200);
  
//LYS - STLUTT 


}

(I have excluded all the "ultrasonic sensor" stuff)
Is the "void loop" in the right place?

Where does "results.value" come from?

I fixed the code :wink: Thanks anyway