How do I make my "cases" stop after a short time?

Hello

Im building a small 4w4 car and I need some help with my code.
Im using IR remote and "case" to controll my car. My problem is that when I press "forward", "backward", "left" or "right" on my IR, the car never stops..

Is it possible to make the cases last for only a short time? So that if I press "left" the car will just turn left 45 degrees (for example)?
`

Or maybe someone could show me a finished "arduino IR car code"?

Thanks

Can't you just take your finger off the button?

Without seeing your code we are blind.

I wonder if you are using the delay() function?

...R

When I press a button on my remote, the car starts to drive. That's good for the "forward" and "backward" cases, but I want the car to only turn for a short time when I press "left" or "right".

I was told I should use "millis", not "delay" in my code..

My code is:

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

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

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

int RECV_PIN = 5;
IRrecv irrecv(RECV_PIN);
decode_results results;

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);
 digitalWrite(mhp, LOW);
 pinMode(mhn, OUTPUT);
 digitalWrite(mhn, LOW);
 pinMode(mvp, OUTPUT);
 digitalWrite(mvp, LOW);
 pinMode(mvn, OUTPUT);
 digitalWrite(mvn, LOW);
 
}

void loop(){

//LYS / LIGHTS - 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 / LIGHTS - STLUTT 

//IR, MOTOR - START
  
 if (irrecv.decode(&results)){
  Serial.println(results.value, DEC);
  irrecv.resume();
 }
 delay(100);
 switch (results.value){
 
case 16712445:  //STOPP    >>|
digitalWrite(mhp, LOW);
digitalWrite(mhn, LOW);
digitalWrite(mvp, LOW);
digitalWrite(mvn, LOW);
break;   
   
case 16736925:  //Forward    CH
digitalWrite(mhp, LOW);
digitalWrite(mhn, HIGH);
digitalWrite(mvp, HIGH);
digitalWrite(mvn, LOW);
break;

case 16754775:  //Backward    +
digitalWrite(mhp, HIGH);
digitalWrite(mhn, LOW);
digitalWrite(mvp, LOW);
digitalWrite(mvn, HIGH);
break;

case 16720605:  //Left           |<<
digitalWrite(mhp, HIGH);
digitalWrite(mhn, LOW);
digitalWrite(mvp, HIGH);
digitalWrite(mvn, LOW);
break;

case 16761405:  //Right         >||
digitalWrite(mhp, LOW);
digitalWrite(mhn, HIGH);
digitalWrite(mvp, LOW);
digitalWrite(mvn, HIGH);
break;

case 16769565:  //Lights On    CH+
digitalWrite(led, HIGH);

case 16753245:  //Lights Off    CH-
digitalWrite(led, LOW);
} 
 
//IR, MOTOR - SLUTT
 
}

HansiFansi:
When I press a button on my remote, the car starts to drive. That's good for the "forward" and "backward" cases, but I want the car to only turn for a short time when I press "left" or "right".

What happens if you take your finger off the button after a short time?

KenF:
What happens if you take your finger off the button after a short time?

I don't hold down the buttons on my IR remote.. I just press them for a short time, then let go.
When I press "forward" the car drives forward until I press a new button.. It will never stop unless I press "stop" on my remote.. (or the battery dies)

The easiest way to do it is to use delay in your right and left clauses. You'll also need to set results.value to zero after the delay or it will trigger more turns - the behavior you see now.

Both of those things are nasty hacks though, Better to use the blink without delay technique to do a shorter turn. You'll probably want to look up state machines too.

Ah just looked at your code. You seem to be using 4 independent motors to drive it. You don't have servos to steer it?

HansiFansi:
I don't hold down the buttons on my IR remote.. I just press them for a short time, then let go.
When I press "forward" the car drives forward until I press a new button.. It will never stop unless I press "stop" on my remote.. (or the battery dies)

Is that what you want to happen?
If not, please describe what you would like to happen. You probably need to give a lot of detail so we know what code changes would be needed.
Can you confirm that, with the present code, it does stop when you press stop?

...R

Robin2:
Is that what you want to happen?
If not, please describe what you would like to happen. You probably need to give a lot of detail so we know what code changes would be needed.
Can you confirm that, with the present code, it does stop when you press stop?

...R

This is what I want the IR remote / car to do:

IR remote forward (short press) = Car drives forward (don't stop)
IR remote bacward (short press) = Car drives bacward (don't stop)
IR remote left (short press) = Car turns left (turns around, then stop) (the turning time will decide the angle the car turns)
IR remote right (short press) = Car turns right (turns around, then stop) (the turning time will decide the angle the car turns)
IR remote stop (short press) = Car stops

IR remote lights on/off (short press) = The lights on the car turns on/off (and stays on/off until I press off/on) IR remote overdrives the LDR (light sensor)

OK The problem is, that once it stops turning, it needs to go back to doing what it was doing before you started turning.

So I've altered your code, this takes note of wether you're going Forwards, Reversing or Stopped. in a separate variable.

When it recieves a signal to turn, it turn and wait for a tenth of a second, At this point if no other signal is being recieved, it should resume it's previous operation (forwards, reverse or stop).

You may have to alter some of the details but see how it goes.

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

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

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

int RECV_PIN = 5;
IRrecv irrecv(RECV_PIN);
decode_results results;

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);
 digitalWrite(mhp, LOW);
 pinMode(mhn, OUTPUT);
 digitalWrite(mhn, LOW);
 pinMode(mvp, OUTPUT);
 digitalWrite(mvp, LOW);
 pinMode(mvn, OUTPUT);
 digitalWrite(mvn, LOW);
 
}

char currentDirection='S'; //S for stop, F forwards, R reverse

void loop(){

//LYS / LIGHTS - 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 / LIGHTS - STLUTT 

//IR, MOTOR - START
  
 if (irrecv.decode(&results)){
  Serial.println(results.value, DEC);
  irrecv.resume();
 }
 delay(100);
 switch (results.value){
 
case 16712445:  //STOPP    >>|
digitalWrite(mhp, LOW);
digitalWrite(mhn, LOW);
digitalWrite(mvp, LOW);
digitalWrite(mvn, LOW);
currentDirection='S';
break;   
   
case 16736925:  //Forward    CH
digitalWrite(mhp, LOW);
digitalWrite(mhn, HIGH);
digitalWrite(mvp, HIGH);
digitalWrite(mvn, LOW);
currentDirection='F';
break;

case 16754775:  //Backward    +
digitalWrite(mhp, HIGH);
digitalWrite(mhn, LOW);
digitalWrite(mvp, LOW);
digitalWrite(mvn, HIGH);
currentDirection='R';
break;

case 16720605:  //Left           
digitalWrite(mhp, HIGH);
digitalWrite(mhn, LOW);
digitalWrite(mvp, HIGH);
digitalWrite(mvn, LOW);
delay(100);
break;

case 16761405:  //Right         
digitalWrite(mhp, LOW);
digitalWrite(mhn, HIGH);
digitalWrite(mvp, LOW);
digitalWrite(mvn, HIGH);
delay(100);
break;

case 16769565:  //Lights On    CH+
digitalWrite(led, HIGH);

case 16753245:  //Lights Off    CH-
digitalWrite(led, LOW);

default://if no input continue in current mode
  switch(currentDirection)
    {
     case 'S':  //STOPP    
     digitalWrite(mhp, LOW);
     digitalWrite(mhn, LOW);
     digitalWrite(mvp, LOW);
     digitalWrite(mvn, LOW);
     currentDirection='S';
     break;   
   
     case 'F':  //Forward  
     digitalWrite(mhp, LOW);
     digitalWrite(mhn, HIGH);
     digitalWrite(mvp, HIGH);
     digitalWrite(mvn, LOW);  
     currentDirection='F';
     break;

     case 'R':  //Backward 
     digitalWrite(mhp, HIGH);
     digitalWrite(mhn, LOW);
     digitalWrite(mvp, LOW);
     digitalWrite(mvn, HIGH);
     currentDirection='R';
     break;
    }   
   
}
 
//IR, MOTOR - SLUTT
 
}

Thank you @KenF !
Will test the code later tonight :slight_smile:

KenF:
OK The problem is, that once it stops turning, it needs to go back to doing what it was doing before you started turning.

So I've altered your code, this takes note of wether you're going Forwards, Reversing or Stopped. in a separate variable.

When it recieves a signal to turn, it turn and wait for a tenth of a second, At this point if no other signal is being recieved, it should resume it's previous operation (forwards, reverse or stop).

You may have to alter some of the details but see how it goes.

const int ldr = A7;   //Lyssensor (LDR), Light sensor

const int led = 3;    //Kjørelys, Lights
const int blaa1 = 4; 
const int lys = 300;  //Laveste verdi for lys (styrer kjørelyset), Light value

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

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

int RECV_PIN = 5;
IRrecv irrecv(RECV_PIN);
decode_results results;

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);
digitalWrite(mhp, LOW);
pinMode(mhn, OUTPUT);
digitalWrite(mhn, LOW);
pinMode(mvp, OUTPUT);
digitalWrite(mvp, LOW);
pinMode(mvn, OUTPUT);
digitalWrite(mvn, LOW);

}

char currentDirection='S'; //S for stop, F forwards, R reverse

void loop(){

//LYS / LIGHTS - 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 / LIGHTS - STLUTT

//IR, MOTOR - START
 
if (irrecv.decode(&results)){
  Serial.println(results.value, DEC);
  irrecv.resume();
}
delay(100);
switch (results.value){

case 16712445:  //STOPP    >>|
digitalWrite(mhp, LOW);
digitalWrite(mhn, LOW);
digitalWrite(mvp, LOW);
digitalWrite(mvn, LOW);
currentDirection='S';
break; 
 
case 16736925:  //Forward    CH
digitalWrite(mhp, LOW);
digitalWrite(mhn, HIGH);
digitalWrite(mvp, HIGH);
digitalWrite(mvn, LOW);
currentDirection='F';
break;

case 16754775:  //Backward    +
digitalWrite(mhp, HIGH);
digitalWrite(mhn, LOW);
digitalWrite(mvp, LOW);
digitalWrite(mvn, HIGH);
currentDirection='R';
break;

case 16720605:  //Left         
digitalWrite(mhp, HIGH);
digitalWrite(mhn, LOW);
digitalWrite(mvp, HIGH);
digitalWrite(mvn, LOW);
delay(100);
break;

case 16761405:  //Right       
digitalWrite(mhp, LOW);
digitalWrite(mhn, HIGH);
digitalWrite(mvp, LOW);
digitalWrite(mvn, HIGH);
delay(100);
break;

case 16769565:  //Lights On    CH+
digitalWrite(led, HIGH);

case 16753245:  //Lights Off    CH-
digitalWrite(led, LOW);

default://if no input continue in current mode
  switch(currentDirection)
    {
    case 'S':  //STOPP   
    digitalWrite(mhp, LOW);
    digitalWrite(mhn, LOW);
    digitalWrite(mvp, LOW);
    digitalWrite(mvn, LOW);
    currentDirection='S';
    break; 
 
    case 'F':  //Forward 
    digitalWrite(mhp, LOW);
    digitalWrite(mhn, HIGH);
    digitalWrite(mvp, HIGH);
    digitalWrite(mvn, LOW); 
    currentDirection='F';
    break;

case 'R':  //Backward
    digitalWrite(mhp, HIGH);
    digitalWrite(mhn, LOW);
    digitalWrite(mvp, LOW);
    digitalWrite(mvn, HIGH);
    currentDirection='R';
    break;
    } 
 
}

//IR, MOTOR - SLUTT

}

Hi. The car still doesn't "stop" after turning :confused: I managed to make the car "turn and stop" once, but I haven't managed to recreate that scenario. I have tried every possible combinations on my remote (forward then left, backward then left, left then right, and so on.. )

Is it possible to make the car stop after turning and not go back to the previous case? Would that be easier?

Thanks for all you good help btw!

You could add a case for all off, and when you finish a turn, change the variable that drives the case to that. So if you had a case of 10, just set results.value = 10; as the last part of one of the turn statements.