IR Sensor controlling Servo Motor and RGB LED.

Hi all,

I’m trying to incorporate the use of Arduino into my final year university project. I’m very new to it, and have tried really hard to get this right… but there seems to be an issue in combining the two sketches. The motor is very jumpy, and the LED control not as accurate/reliable as the standalone sketch.

I’ve copied the sketch below, and would really appreciate any help to get it working.

#include <IRremote.h> // Include the IRremote library
#include <Servo.h>
#define plus 0x90 //clockwise rotation button
#define minus 0x890 //counter clockwise rotation button

/* Setup constants for SparkFun’s IR Remote: /
#define NUM_BUTTONS 9 // The remote has 9 buttons
/
Define the IR remote button codes. We’re only using the
least signinficant two bytes of these codes. Each one
should actually have 0x10EF in front of it. Find these codes
by running the IRrecvDump example sketch included with
the IRremote library.*/
const uint16_t BUTTON_POWER = 0xA90; // i.e. 0x10EFD827
const uint16_t BUTTON_A = 0x338;
const uint16_t BUTTON_B = 0xB38;
const uint16_t BUTTON_C = 0xF38;
const uint16_t BUTTON_UP = 0x490;
const uint16_t BUTTON_DOWN = 0xC90;
const uint16_t BUTTON_LEFT = 0x62E9;
const uint16_t BUTTON_RIGHT = 0x36E9;
const uint16_t BUTTON_CIRCLE = 0xA70;

/* Connect the output of the IR receiver diode to pin 11. /
int RECV_PIN = 11;
/
Initialize the irrecv part of the IRremote library */
IRrecv irrecv(RECV_PIN);
decode_results results; // This will store our IR received codes
uint16_t lastCode = 0; // This keeps track of the last code RX’d

Servo servo;
int val; //rotation angle
bool cwRotation, ccwRotation; //the states of rotation

/* Setup RGB LED pins: */
enum ledOrder // Make an enum to add some clarity in the code
{
RED, // 0
GREEN, // 1
BLUE // 2
};
const int rgbPins[3] = {5, 9, 6}; // Red, green, blue pins respectively
byte rgbValues[3] = {55, 23, 200}; // This keeps track of channel brightness
byte activeChannel = RED; // Start with RED as the active channel
boolean ledEnable = 1; // Start with the LED on.

void setup()
{
Serial.begin(9600); // Use serial to debug.
irrecv.enableIRIn(); // Start the receiver

servo.attach(10); //servo pin

/* Set up the RGB LED pins: /
for (int i=0; i<3; i++)
{
pinMode(rgbPins
, OUTPUT);*
analogWrite(rgbPins_, rgbValues*);
}
}
// loop() constantly checks for any received IR codes. At the*
// end it updates the RGB LED.
void loop()
{
* if (irrecv.decode(&results))
{
/ read the RX’d IR into a 16-bit variable: /_

uint16_t resultCode = (results.value & 0xFFFF);
_ / The remote will continue to spit out 0xFFFFFFFF if a_

* button is held down. If we get 0xFFFFFFF, let’s just*
_ assume the previously pressed button is being held down /
if (resultCode == 0xFFFF)
resultCode = lastCode;
else*
* lastCode = resultCode;
// This switch statement checks the received IR code against*
* // all of the known codes. Each button press produces a*
* // serial output, and has an effect on the LED output.
switch (resultCode)
{_
case BUTTON_POWER:
_ Serial.println(“Power”);
if (ledEnable) ledEnable = 0;
else ledEnable = 1; // Flip ledEnable*
* break;_
case BUTTON_A:
_ Serial.println(“A”);
activeChannel = RED;
break;_
case BUTTON_B:
_ Serial.println(“B”);
activeChannel = GREEN;
break;_
case BUTTON_C:
_ Serial.println(“C”);
activeChannel = BLUE;
break;_
case BUTTON_UP:
_ Serial.println(“Up”);
rgbValues[activeChannel]++; // Increment brightness*
* break;_
case BUTTON_DOWN:
_ Serial.println(“Down”);
rgbValues[activeChannel]–; // Decrement brightness*
* break;_
case BUTTON_LEFT:
_ Serial.println(“Left”);
rgbValues[activeChannel] = 0; // Min brightness (off)
break;_
case BUTTON_RIGHT:
_ Serial.println(“Right”);
rgbValues[activeChannel] = 255; // Max brightness*
* break;_
case BUTTON_CIRCLE:
_ Serial.println(“Circle”);
rgbValues[activeChannel] = 127; // Medium brightness*
* break;
default:
Serial.print(“Unrecognized code received: 0x”);
Serial.println(results.value, HEX);
break;
}
irrecv.resume(); // Receive the next value*
* }
// Every time through the loop, update the RGB LEDs:
if (ledEnable)
{
for (int i=0; i<3; i++)
{
analogWrite(rgbPins, rgbValues);
}
}
else*

* {
for (int i=0; i<3; i++)
{
analogWrite(rgbPins, 0);
}*_

* if (irrecv.decode(&results)) {*
* Serial.println(results.value, HEX);*
* irrecv.resume(); // Receive the next value*
* if (results.value == plus)*
* {*
* cwRotation = !cwRotation; //toggle the rotation value*
* ccwRotation = false; //no rotation in this direction*
* }*
* if (results.value == minus)*
* {*
* ccwRotation = !ccwRotation; //toggle the rotation value*
* cwRotation = false; //no rotation in this direction*
* }*
* }*
* if (cwRotation && (val != 175)) {*
* val++; //for colockwise button*
* }*
* if (ccwRotation && (val != 0)) {*
* val–; //for counter colockwise button*
* }*
* servo.write(val);*
* delay(50); //General speed*

* }*
}
Cheers,
Michael