Coding Help! Project with IR, Servo, DC motors and ultrasonic

Hello everyone, I am doing a project and was wondering if someone could help me out? I understand circuitry great, wiring schematics and plc programming in line diagram forms. When it comes to coding this kicks my butt, I don't get it. I combined lots of codes together that I found and got nothing but dozens of error codes. Could anyone help me out? If someone could help me and get a flawness code for me and it works, then if you can then walk thru the code for me so I can understand it, I have a reward for you.

Im using the following
2 Arduino unos
2 IR emitters with different frequencies
2 IR receivers to locate the IR ermitters
2 DC motors
1 Servo motor
1 Ultrasonic sensor
and 1 motor controlling chip for the obsticle avoiding detection
and 4 LED lights

The following is what im looking to do.

RIGHT arduino

1.) Turn on power to the arduino and when power is on the LED on the Arduino board will first turn on indicating ready

2.) IR Receiver #1 on pin #2 sensor starts looking for one of the 2 IR signals given by the IR emitter board on the LEFT.

3.) If no signal one of the dc motors will turn slowly to maneuver the car in a 360 degree turn for 60 seconds or until it locates one of the two IR signals.

4.) When IR receiver #1 pin #2 finds IR signal it lowers the servo motor pin #5 from 90 degrees to 0 degrees.

5.) IR receiver #2 on pin #3 which is inside the arm attached to the servo motor then activates by sensing one of the 2 IR signals engages both wheels to move the vehicle forward and attack the IR emitting signal head on.

6.) While moving towards the IR signal the ultrasonic sensor on pin #4 helps avoid any obstacles in its way within 1-3.5 inches (add in there not to retract the arm if signal is lost for 30 seconds.

7.) When the IR emitter is knocked down it stops while it scans for IR signal again as it rotates by spinning one wheel slowly for 20 seconds in a 360 degree as it keeps the arm down for up to 40 seconds once the IR signal is lost. If found then it will attack the other emitting IR signal while the arm is remaining down ( the vehicle cannot move towards the object due to the IR within the arm is what allows the vehicle to move forward therefore the arm shall remain down at 0 degrees). If no IR signal is detected it will rotate for 15 secs then retract the servo arm back to 90 degrees and waits 50 seconds and scans again then follows the previous sequence of attack steps mentioned above. After the last 50 seconds it will shut off until the reset button is pushed. (which I will add one on the bread board with power going in series to a 330 ohm fuse first then the pushbutton switch then to the Analog in #A0).

LEFT arduino

When a pushbutton is pressed (on the left board) it continues the signal turns on a led light bulb indicating power and IR signal emitted and the IR emitter is sending a IR signal.

(The clear LED bulbs in the picture are representing the 2 pin IR emitters I have and will be using)

Hi, @ww23
Welcome to the forum.

Can you please post the code you have?

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

1 Like

Hi @ww23

welcome to the arduino-forum.
Pretty detailed description of the functionality you want.

Still way too less information to really beeing able to help

Some comments:
receive such a complexe code just from a funcionality description for free will not happen.

Most users here don't like tinkercad. Tinkercad requires to register.
Real schematics are highly preferred over Fritzy or (S)tinky-pictures (tinkercad-pictures)
Free hand-drawn schematics are sufficient.

If you are new to programming in C++ you should break down the complete functionality into pretty small pieces

One point to start
make one arduino send an IR-signal
check if signal creation works with a smartphone camera (you will see a violet blinking in the camera picture
make the second arduino receive the IR-signal and just print to the serial monitor or switch on/off onboard LED.

If this works reliably take the next part
a small testcode that switches on off one motor
etc. etc.

With this strategy there are always just a few lines of code that could be wrong which means the time to detect the bug is short.

best regards Stefan

Can you please post a schematic?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

The Fritzy in you first post is not really an informative circuit diagram.

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

what program do people use to test your ideas and fun projects for your kids? I was recommeded to use tinkecad by a few people and I like how it automatically transfers the components drawing to schematics alot easier that going into auto cad and doing it. what other programs do other people use? Im having one heck of a time trying to get any signal out of the remote.

Sorry for the info bomb below but here are the 3 programs I have that I want to compile and tweak a few parts of it, but the IR sensor isn't sensing any IR signal from the remote. So therefore no sense in continuing and compiling the codes together if they don't work seperatly. I figure someone could help me do it then walk me thru it so I could understand it. Thank you.

This is for the IR controlled DC motor circuit on and off.


CODE BELOW/////

#include <IRremote.h>
#define IR_RECEIVER_PIN 2

void setup()
{

pinMode(10, OUTPUT)
PinMode(12, OUTPUT)

Serial.begin(9600);
Serial.println ("brgin serial moniter")
irrecv.enableIRIn();
Serial.println ("ir receiver enabled")

}

void loop()
{

if (irrecv.decode(&results)) {

unsigned int value = results.value;
Serial.println(value);
switch (value) {
  
  case (pin #2 >0);
  	digitalWrite(12, HIGH);
  	digitalWrite(10, HIGH);
  	break;
  
  case (pin #2 <1);
  	digitalWrite(12, LOW);
  	digitalWrite(10, LOW);
  	break;
}
	irrecv.resume();

delay(10); // Delay a little bit to improve simulation performance
}

IR controlled Servo when no IR signal rests at 90 degrees and when it picks up IR signal it rotates the servo to 0 degrees.


CODE BELOW/////

#include <IRremote.h>
#include <Servo.h>
#define plus ___
#define minus ______

int RECV_PIN = 2
Servo servo;
int val;
bool cwRotation, acwRotation;

IRrecv irrecv(RECV_PIN);

decode results results;

void setup()
{
Serial.begin(9600)
irrecv.enableIRIn()
servo.attached(9);
}

void loop() {

if(irrecv.decode(&results)){
Serial.Println(results.value, HEX);
irrecv.resume();

if (results.value== plus)
{ 
cwrRotation=!cwRotation;
  cwRotation = false;

}

if(results.value==minus)
{
  acwRotation =!acwRotation;
  cwRotation = false;      

}
{
if (cwRotation && (val!= 180)){
val++;
}
if(acwRotation(val!=0)){
val--;
}
Servo.write(val);
delay(20);
}

DC motors controlled by Ultrasonic sensor that avoids objects


CODE BELOW/////

// C++ code
//
int Ultrasonicsensor = 0;
int IR_Receiver__1 = 0;
int IR_Receiver__2 = 0;

//sensor pins
#define trig_pin A1 //analog input 1
#define echo_pin A2 //analog input 2

long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}

void setup()
{
Serial.begin(9600);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
}

void loop()
{
// Ultra Sonic obsticle Monovering
Ultrasonicsensor = 0.006783 * readUltrasonicDistance(A1, A2);
Serial.println(Ultrasonicsensor);
if (Ultrasonicsensor <= 4) {
// Obstacle monovering
digitalWrite(4, HIGH);
digitalWrite(6, LOW);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(4, HIGH);
digitalWrite(6, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
} else {
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
}
}

Here is the final circuitry in line diagram form and the overall picture of what it will look like if that matters to people?

Thank you soo much, the person I'm making this for will be soo excited.



RE-Edit all these postings

to properly use

code-sections

like described here

1 Like

Hi, @ww23

In post #6
Don't try and drive motors directly of the UNO output pins, it will destroy them.

Tom.. :grinning: :+1: :coffee: :australia:

1 Like

Similarly, dont power the servo from the Arduino 5v.

Also, those pp3 9V batteries are totally inappropriate for powering a motor, servo, or Uno.

1 Like

Yes, I know the voltage needs to be different for the Arduino and motors. The motors will have a control board which will all be changed after I get the coding done. That part is simple to switch the signal wire to control a motor contactor. I'm not understanding the coding part and I'm getting nothing but error messages and IR signal programming was deleted on tinkercad is what their Tech support emailed. so I'm confused and frustrated at this coding part. Can someone help me? Or someone have a IR controlled lab I can try to understand and then how to lace/weave another code into it?

This sentence says nothing useful. Not a single user has a glas-sphere to look into
to see your code or your error-messages

If you want effective help on this
you have to provide the complete sketch of non-compiling code
and to provide the most detailed error-messages you can get
by adjusting the compiler to log the compiling-process in the most detailed way
which is described here

to me it seems that it would be useful for you to learn some fundamental basics
Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

this tutorial about IR seems to be quite useful
https://www.circuitbasics.com/arduino-ir-remote-receiver-tutorial/

best regards Stefan

Thank you for the information and links, I will try this this week. I assumed this was like PLC programming where you type in the values, delay and output points and items in line diagram format but isn't. Thank you, guys, again for the info and links hopefully I have success this week.

the carrier-frequeny of your IR-sender and IR-receiver must match .
This is the reason why all the datasheets are nescessary.
PLCs are super-standardised plug and play. The microcontrollerworld is not super-standardised.

I'm sorry if I'm not doing this the best or the correct flow as I am learning and am not aware of all of the lingo. Here is i believe all the info as requested sorry for the crappy hand drawing I have problems with my arms do to a injury which stripped me being an electrician away and having to have a triple reconstuction surgery.Do to that I  don't have the best control or feeling in my hands therefore I rely on the computer to help assist in making things more pretty. I have video of it running partially but I need assistance still if someone can please help. 

I understand a PLC can be more simple than an arduino I figured in my beacon code I could tell it to say 12345678 or FDFDFDFD or ABCDEFGH and what ever I have in the .sendNEC() it would only repeat that as fast as the controller can send it or as slow as I write a delay. 

Can someone help me I guess first make it send out a specific code as fast as the microcontroIler #1 can send it? I don't care what it is as long as its a simple one and for my receiver to pick up and turn a motor which signal is found? I would replace the code 0xFFFFFFFF with the code that the IR beacon will send out. 

Here is my code I have on the Arduinio #1 

#include <IRremote.h>
#include <Servo.h>

// Define the pins used for the IR receivers, DC motors, and ultrasonic sensor
const int IR_RECV_PIN_1 = 2;
const int IR_RECV_PIN_2 = 3;
const int MOTOR_PWM_PIN_1 = 11;
const int MOTOR_PWM_PIN_2 = 12;
const int TRIGGER_PIN = 4;
const int ECHO_PIN = 5;
const int servo_motor_PIN = 13;

// Define the IR receiver objects
IRrecv irrecv1(IR_RECV_PIN_1);
IRrecv irrecv2(IR_RECV_PIN_2);
decode_results results;

// Define the servo motor object
Servo servo_motor;

void setup() {
  // Initialize the IR receivers, servo motor, and serial monitor
  irrecv1.enableIRIn();
  irrecv2.enableIRIn();
  servo_motor.attach(13);
  Serial.begin(9600);

  // Set the DC motor pins to output mode
  pinMode(MOTOR_PWM_PIN_1, OUTPUT);
  pinMode(MOTOR_PWM_PIN_2, OUTPUT);

  // Set the ultrasonic sensor pins to input/output mode
  pinMode(TRIGGER_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
}

void loop() {
  // Check for incoming IR signals
  if (irrecv1.decode(&results)) {
    Serial.println(results.value, HEX); // Change back to HEX
    //irrecv1.resume();

    // If the IR signal is to move the servo motor, set the angle to 0 degrees
    if (results.value == 0xFFFFFFFF) {
      Serial.println("Test");
      servo_motor.write(0);
    }

    else {
      delay  (11);
      servo_motor.write(90);
    }

    irrecv1.resume();
  }

  // Check the distance using the ultrasonic sensor
  long duration, distance;
  digitalWrite(TRIGGER_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGGER_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER_PIN, LOW);
  duration = pulseIn(ECHO_PIN, HIGH);
  distance = duration * 0.034 / 2;

  // If the object is too close, stop one motor and wait a moment
  if (distance < 6) {
    digitalWrite(MOTOR_PWM_PIN_1, HIGH);
    digitalWrite(MOTOR_PWM_PIN_2, LOW);
    delay(1);
  }

  // If the object is far enough away and there is an incoming IR signal 2, control the motors
  else if (irrecv2.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv2.resume();
    
    // If the IR signal #2 then will move the motors forward, turn on both motors
    if (results.value == 0xFFFFFFFF) {
      digitalWrite(MOTOR_PWM_PIN_1, HIGH);
      digitalWrite(MOTOR_PWM_PIN_2, HIGH);
    }

    // If no valid IR signal 2 is detected, stop one motor 
    else {
      digitalWrite(MOTOR_PWM_PIN_1, HIGH);
      digitalWrite(MOTOR_PWM_PIN_2, LOW);
    }
  }
}

//______SERIAL MONITER LINE SCHEDULE________

06:44:55.266 -> The function decode(&results)) is deprecated and may not work as expected! Just use decode() without a parameter and IrReceiver.decodedIRData.<fieldname> .


17:04:18.040 -> FFFFFFFF
17:04:18.212 -> 1326963D
17:04:18.259 -> FFFFFFFF
17:04:18.341 -> FFFFFFFF
17:04:18.459 -> FFFFFFFF
17:04:18.570 -> FFFFFFFF
17:04:18.709 -> FFFFFFFF
17:04:18.818 -> FFFFFFFF
17:04:18.930 -> FFFFFFFF
17:04:19.040 -> FFFFFFFF
17:04:19.197 -> E5D548FB
17:04:19.274 -> FFFFFFFF
17:04:19.366 -> FFFFFFFF
17:04:19.476 -> FFFFFFFF
17:04:19.601 -> 3F8D589E
17:04:19.710 -> FFFFFFFF
17:04:19.791 -> FFFFFFFF
17:04:19.901 -> FFFFFFFF
17:04:20.039 -> FFFFFFFF
17:04:20.225 -> EF241EFB
17:04:20.271 -> FFFFFFFF
17:04:20.383 -> FFFFFFFF
17:04:20.491 -> FFFFFFFF
17:04:20.616 -> FFFFFFFF
17:04:20.707 -> FFFFFFFF
17:04:20.786 -> FFFFFFFF
17:04:20.926 -> FFFFFFFF
17:04:21.021 -> FFFFFFFF
17:04:21.195 -> E6D0B1A5
17:04:21.271 -> FFFFFFFF
17:04:21.351 -> FFFFFFFF
17:04:21.462 -> FFFFFFFF
17:04:21.569 -> FFFFFFFF
17:04:21.710 -> FFFFFFFF
17:04:21.785 -> BBCEE266
17:04:21.819 -> FFFFFFFF
17:04:21.930 -> FFFFFFFF
17:04:22.040 -> FFFFFFFF
17:04:22.229 -> 1326963D
17:04:22.260 -> FFFFFFFF
17:04:22.385 -> FFFFFFFF
17:04:22.494 -> FFFFFFFF
17:04:22.576 -> FFFFFFFF
17:04:22.712 -> FFFFFFFF
17:04:22.792 -> FFFFFFFF
17:04:22.903 -> FFFFFFFF
17:04:23.041 -> FFFFFFFF
17:04:23.228 -> 1326963D
17:04:23.275 -> FFFFFFFF
17:04:23.354 -> FFFFFFFF
17:04:23.493 -> FFFFFFFF
17:04:23.617 -> FFFFFFFF
17:04:23.708 -> FFFFFFFF
17:04:23.818 -> FFFFFFFF
17:04:23.929 -> FFFFFFFF
17:04:24.055 -> FFFFFFFF
17:04:24.198 -> E4F29915
17:04:24.275 -> FFFFFFFF
17:04:24.353 -> FFFFFFFF
17:04:24.476 -> FFFFFFFF
17:04:24.575 -> FFFFFFFF
17:04:24.682 -> FFFFFFFF
17:04:24.796 -> FFFFFFFF
17:04:24.931 -> FFFFFFFF
17:04:25.058 -> FFFFFFFF
17:04:25.238 -> 1326963D
17:04:25.277 -> FFFFFFFF
17:04:25.357 -> FFFFFFFF
17:04:25.465 -> FFFFFFFF
17:04:25.603 -> FFFFFFFF
17:04:25.713 -> FFFFFFFF
17:04:25.795 -> FFFFFFFF
17:04:25.931 -> FFFFFFFF
17:04:26.026 -> FFFFFFFFF
17:04:26.242 -> 1326963D
17:04:26.275 -> FFFFFFFF
17:04:26.400 -> FFFFFFFF
17:04:26.493 -> FFFFFFFF
17:04:26.617 -> FFFFFFFF
17:04:26.709 -> FFFFFFFF
17:04:26.819 -> FFFFFFFF
17:04:26.945 -> FFFFFFFF
17:04:27.055 -> FFFFFFFF
17:04:27.242 -> 1326963D
17:04:27.274 -> FFFFFFFF
17:04:27.356 -> FFFFFFFF



//_____BEACON CODE________ on Arduino #2
#include <IRremote.h> // >v3.0.0
                                            
#define PIN_SEND 9

void setup()  
{  
  IrSender.begin(PIN_SEND); // Initializes IR sender
}  
                               
void loop()  
{  
  IrSender.sendNEC(0xFFFFFFFF, 32, 9); // the address 0x0102 with the command 0x34 is sent 
   // wait for one second__delay(0);
}


//_____________SERIAL MONITER_________________ 

BLANK

I have video of it running partially and pictures of it and the hand drawn scematic as requested I'll attempt in uploading it. I would really appreciate it and anyones help. 


Here is the check list in order I am wanting it to do. 
N= not working yet or missing
W= works as of right now 

1.)  When power is applied IR beacon#1 on a different arduino boards emits a exact IR signal
2.)  IR receiver #1 looks for the exact signal IR beacon #1 is sending.
IF matching signal then it lowers servo motor from 90 degrees to 0 degrees (if IR signal is lost it will retract after 5 seconds of signal lost)
Else if no IR signal then it turns one motor on low while the other stays on high to turn for up to 15 seconds, if no signal turns off. ----W
#3 when Servo motor lowers to 0 degrees then IR receiver #2 inside the arm on the servo motor is also looking for the exact IR beacon #1 signal.
If exact signal is found then it engages both wheels on high while using IR utra sonic obsticle avoidance program----- W
else- if no IR signal it rotates one wheel till IR beacon #1 signal is located for up to 30 seconds ----W (except doesn't have a time on it yet)
#4 if IR signal is nowhere to be found and rotated one wheel on high and one on low for 30 seconds and no IR signal then program stops untill a push button is pressed located on pin 7 to restart the program sequence.

what you have posted gives a hint what is happening

look up what version of the library IRremote.h you have installed.
open the library manager and search for "iRremote"
or
search the file IRremote.hpp and look up what version it is
actual version is

#define VERSION_IRREMOTE "4.1.0"

best regards Stefan

There's no ground from the main circuit to the servo and associated 6V battery to the top right of your schematic.

I have the IRremote 4.1.2 by Shirriff