Sir, I have a challenge I've been trying to fix. I want to mount an IR receiver sensor on a cylindrical shaft (plastic but 3D printed) which will be mounted on a servomotor. The servo is to be rotating the IR sensor while it listens for IR signals from nearby transmitters. Each transmitter sends a unique code. I want the servo to rotate by an increment of 1degree and at the same time, the IR sensor should be listening for a unique code, such that once that unique code is detected, the servo is to stop rotating and give the value of its current servo position. I am having difficulty doing that...
Below is a code I wrote to do that but it is badly written. because it is no responding well.
#include <IRremote.h>
#include <Servo.h>
Servo IRservomotor;
#define motorspin 5
int angle = 0;
int current_angle;
int RECV_PIN = 3;
IRrecv irrecv(RECV_PIN);
decode_results results;
int phi1, phi2, phi3;
int detecting_beacon1();
int detecting_beacon2();
int detecting_beacon3();
void detect_IR_Signal();
int detect_beacons();
void setup()
{
Serial.begin(9600);
Serial.println("Starting");
irrecv.enableIRIn();
irrecv.blink13(true);
IRservomotor.attach(5);
}
int detecting_beacon1()
{
for(angle = 0; angle >= 180; angle += 1)
{
IRservomotor.write(angle);
detect_IR_Signal();
if(results.value == 0x18b){break;}
}
current_angle = angle;
phi1 = 2*current_angle;
delay(20);
return phi1;
}
int detecting_beacon2()
{
for(angle = 0; angle >= 180; angle += 1)
{
IRservomotor.write (angle);
detect_IR_Signal();
if(results.value == 0x58b){break;}
}
current_angle = angle;
phi2 = 2*current_angle;
return phi2;
}
int detecting_beacon3()
{
for(angle = 0; angle >= 180; angle += 1)
{
IRservomotor.write(angle);
detect_IR_Signal();
if(results.value == 0x98b){break;}
}
current_angle = angle;
phi3 = 2*current_angle;
return phi3;
}
void detect_IR_Signal()
{
if (irrecv.decode(&results))
{
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
}
delay(10);
}
void return_servoPos()
{
for(angle = current_angle; angle == 0; angle -= 5)
{
IRservomotor.write(angle);
delay(20);
}
}
/*
void loop()
{
for(angle = 0; results.value != 0x18b; angle += PI/180)
{
IRservomotor.write(angle);
detect_IR_Signal();
current_angle = angle;
}
Serial.println("Wow... I have detected beacon1... Praise God!!! Alleluia");
phi1 = 2*current_angle;
Serial.println("And the current servo position is = ");
Serial.print(phi1);
delay(10000);
}
void loop()
{
for(angle = 0.0; results.value == 0x18b; angle += 2.0)
{
IRservomotor.write(angle);
// detect_IR_Signal();
// current_angle = angle;
delay(100);
}
Serial.println("Wow... I have detected beacon1... Praise God!!! Alleluia");
phi1 = 2*current_angle;
Serial.println("And the current servo position is = ");
Serial.print(phi1);
delay(10000);
}*/
void loop()
{
for(angle = 0; angle >= 180; angle += 1)
{
IRservomotor.write(angle);
detect_IR_Signal();
if(results.value == 0x18b){break;}
}
current_angle = angle;
phi1 = 2*current_angle;
delay(20);
return phi1;
}
I really need help