Uno w/ distance sensor & IR remote override

hello everyone,
i am looking for some help with a project i am working on with my ten year old son. we are new to the Arduino community and have been working on some fun projects. My son wanted to try and take three of the projects we did (found them on instructables) and put them together as one. Basically we have and HC-SR04 ultrasonic distance sensor, L298N (H-bridge), IR remote (and 1838 receiver), and an Arduino uno.

We have a program that uses the ultra HC-SR04 ultrasonic distance sensor and the L298N (H-bridge) as an obstacle avoidance system that works by itself.

Unfortunately we do not know how to adjust the code for speed control and we want to add the IR remote and receiver as an override so that if the 0 (zero) button on the remote is pressed then instead of doing the obstacle avoidance it just uses the <>^... keys on the remote to move, and then if you press the # key on the remote it goes back to the obstacle avoidance part of the program.

i know that for the speed control to make it go slower i need to do something like, analogWrite(variable, 125) //value between 0-255 but i am not sure how to connect it all to the arduino uno and exactly how to write the program.

also for the obstacle avoidance program i am going to paste below, i do not want to use the LED or buzzer, just the L298N driver and the sensor. I want to make the build and the code as simple as possible where it has a slower speed (maybe 125/255), HC-SR04 distance sensor, IR remote, and receiver and thats it. I could imagine this may be a lot to ask but if someone could take the time to help my son and I, we would really appreciate it.

ps there is a pushbutton If statement with the obstacle avoidance i think that for this project we may need to use something like that to constant checking the status of the IR remote to see if the 0 (zero) button or # button have been pressed.

P.S.-#2: if you know of an instructables for this type of project we would appreciate it if you could add the link to this thread.

THANK YOU ALL IN ADVANCE

i have auto formatted it to make it easier to use. also, i forgot but i already figured out the speed control thing for the irremote project that i did with just the ir remote. lastly i got the library for the IRremote from github; below is the zip file for the library. (i did not install the standard arduino library). i have a program that serial prints responses to every button on my remote and have also attached that below.

IR_Remote_Control.ino (4.12 KB)

Obstacle_Avoidance.ino (4.7 KB)

Arduino-IRremote-master (1).zip (79.4 KB)

ir_remote_test.ino (2.13 KB)

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

When your code requires a library that's not included with the Arduino IDE please always post a link(using the chain link icon on the toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger(Sketch > Include Library > Manage Libraries) then say so and state the full name of the library.

You have two new features you want to add to the project. Don't attempt to do both at the same time. Do one at a time and only move on to the next one after getting the first working.

For the speed control, first write a minimal sketch that simply changes the speed of one motor. Once that's working perfectly and you understand what all the code does you will be ready to begin incorporating that code into your project.

For the IR control, write a minimal sketch that prints a message to the Serial Monitor when the 0 button on the remote is pressed. After that is working perfectly and you understand every line of code in that sketch you will be ready to begin incorporating that code into your project.

pert:
Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

When your code requires a library that's not included with the Arduino IDE please always post a link(using the chain link icon on the toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger(Sketch > Include Library > Manage Libraries) then say so and state the full name of the library.

You have two new features you want to add to the project. Don't attempt to do both at the same time. Do one at a time and only move on to the next one after getting the first working.

For the speed control, first write a minimal sketch that simply changes the speed of one motor. Once that's working perfectly and you understand what all the code does you will be ready to begin incorporating that code into your project.

For the IR control, write a minimal sketch that prints a message to the Serial Monitor when the 0 button on the remote is pressed. After that is working perfectly and you understand every line of code in that sketch you will be ready to begin incorporating that code into your project.

i have made the changes you asked for and added the additional files as well.

Is the speed control test sketch and the IR control test sketch working as expected?

pert:
Is the speed control test sketch and the IR control test sketch working as expected?

yes

I don't see the speed control test sketch.

pert:
I don't see the speed control test sketch.

in the program that uses the ir remote only, you will see that i can adjust the speed. i remembered all i had to do was connect the enA and enB (enable) pins from the L298N motor driver to the arduino in addition to the other 4 pins on the L298N and then rewrite the program using analogWrite.

So if you have the speed control figured out I would recommend adding that feature to the project. If you are going to be using the same analogWrite() value throughout the sketch then you should set a global speed variable, for example:

const speed = 100;
analogWrite(enB, speed);

this will allow you to adjust the speed by editing one line instead of having to change every analogWrite() statement in the code.

The next step is to make your code non-blocking. Things like:

      if (checkLeft < checkRight) {
        left();
        delay(400); // delay, change value if necessary to make robot turn.

will cause your device to be unresponsive to keypad input. Instead of delay you need to set a flag and a timestamp and then compare millis() to the timestamp to see when the interval is complete. This allows the loop to continue to run, rather than being blocked by delay(). For a simple example of how this is done see File > Examples > 02.Digital > BlinkWithoutDelay and the tutorial page for that example:

So it would look something like:

      if (checkLeft < checkRight) {
        left();
        turning = true;
        turnTimestamp = millis();
      }
if (turning == true && millis() - turnTimestamp > turnDuration) {
  turning = false;

pert:
So if you have the speed control figured out I would recommend adding that feature to the project. If you are going to be using the same analogWrite() value throughout the sketch then you should set a global speed variable, for example:

const speed = 100;
analogWrite(enB, speed);

this will allow you to adjust the speed by editing one line instead of having to change every analogWrite() statement in the code.

i don't want to do this because i want the turning speed to be different then the forward speed, also i want the reverse speed to be slower then the forward speed. though i suppose i can three different variables one for turn, one for forward, and one for reverse.

as for the second part, i don't understand how to do that even after looking at the blink without delay example in the Arduino IDE.

if anyone has time to further explain how to use the millis function i would greatly appreciate it.

ralphjs:
though i suppose i can three different variables one for turn, one for forward, and one for reverse.

Exactly!

ralphjs:
as for the second part, i don't understand how to do that even after looking at the blink without delay example in the Arduino IDE.

if anyone has time to further explain how to use the millis function i would greatly appreciate it.

It's a very important concept. You should definitely take the time to understand it. You should never use long delays in your code, they will almost always come back to bite you later. It's so much easier to write a program as non-blocking from the start than to go back and rewrite it to get rid of the delays later when you find that blocking code is preventing you from adding a new feature. Just keep trying at it and you'll get it eventually. You may want to start with a more simplified version of your program first so it's not so overwhelming. It's very important that you understand what every line of your sketch and of the BlinkWithoutDelay sketch does.

pert:
Exactly!
It's a very important concept. You should definitely take the time to understand it. You should never use long delays in your code, they will almost always come back to bite you later. It's so much easier to write a program as non-blocking from the start than to go back and rewrite it to get rid of the delays later when you find that blocking code is preventing you from adding a new feature. Just keep trying at it and you'll get it eventually. You may want to start with a more simplified version of your program first so it's not so overwhelming. It's very important that you understand what every line of your sketch and of the BlinkWithoutDelay sketch does.

to be honest i was just looking for someone to how me exactly how to do this, because i work two jobs and dont have much time to figure all this out, but i still want to find a fun activity to do with my son when i am around.