Infrared Servo Controller

Hello everybody my name is Aston and I am a student at college.
next semester I am required to do a project and have decided to create a robotic arm such as the one in the picture below.


The robotic arm uses 7 servos to control it, two of the servos will be running on the same signal.
To control the servos I have decided to use two arduinos which will communicate using infrared.
I have created a quick diagram to help put across my ideas

This circuit will be sending IR signals

This circuit will be receiving IR signals and control the servos

I have all the parts required to build it but i'm totally stuck with the coding I have been searching for hours but had no luck.
I've have found several bits of coding that needs improving upon and merging.

The first is controlling a servo using two buttons (Forum Link - http://forum.arduino.cc/index.php/topic,3595.0.html)

#include <Servo.h> 
 
Servo myservo; 
#define leftPin 2
#define rightPin 3
int pos = 90;
int delayPeriod = 50;  // increasing this slows down the servo movement

 
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  myservo.write(pos); // center the servo
  pinMode(leftPin, HIGH);   // turn on pullup resistors
  pinMode(rightPin, HIGH);
} 
 
 
void loop() 
{ 
  if(digitalRead(leftPin) == LOW)  
  {                              
   // in steps of 1 degree 
   if( pos > 0)
      --pos;
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(delayPeriod);                      
  } 
  if(digitalRead(rightPin) == LOW)  
  {                              
   if( pos < 180)
       ++pos;
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(delayPeriod);        
  }
}

If anyone can help me change the code from controlling one servo to six and is there any possibility of controlling the delay period for each servo independantly that would be amazing.

The next code is sending infrared code using the IR LED (Forum Link - http://garagelab.com/profiles/blogs/tutorial-arduino-ir-sender-and-receiver)

/*
irSend sketch
this code needs an IR LED connected to pin 3
and 5 switches connected to pins 4 - 8
*/
#include <IRremote.h>
// IR remote control library
const int numberOfKeys = 1;
const int firstKey = 4;
// the first pin of the 5 sequential pins connected to buttons
boolean buttonState[numberOfKeys];
boolean lastButtonState[numberOfKeys];
long irKeyCodes[numberOfKeys] = {
    0x18E758A7, //0 key
};

IRsend irsend;
void setup()
{
    for (int i = 0; i < numberOfKeys; i++){
        buttonState[i]=true;
        lastButtonState[i]=true;
        int physicalPin=i + firstKey;
        pinMode(physicalPin, INPUT);
        digitalWrite(physicalPin, HIGH); // turn on pull-ups
    }
    Serial.begin(9600);
}
void loop() {
    for (int keyNumber=0; keyNumber<numberOfKeys; keyNumber++)
    {
        int physicalPinToRead=keyNumber+4;
        buttonState[keyNumber] = digitalRead(physicalPinToRead);
        if (buttonState[keyNumber] != lastButtonState[keyNumber])
        {
            if (buttonState[keyNumber] == LOW)
            {
                irsend.sendSony(irKeyCodes[keyNumber], 32);
                Serial.println("Sending");
            }
            lastButtonState[keyNumber] = buttonState[keyNumber];
        }
    }
}

If anyone could help me change the code so that 12 buttons would send out an individual code that would be also amazing.

The final code is the IR receiver code from the same forum as above

/*

IR_remote_detector sketch
An IR remote receiver is connected to pin 2.
The LED on pin 13 toggles each time a button on the remote is pressed.
*/
#include <IRremote.h> //adds the library code to the sketch
const int irReceiverPin = 2; //pin the receiver is connected to
const int ledPin = 13; 
IRrecv irrecv(irReceiverPin); //create an IRrecv object
decode_results decodedSignal; //stores results from IR detector

void setup()
{
    pinMode(ledPin, OUTPUT);
    irrecv.enableIRIn();
}
boolean lightState = false;
unsigned long last = millis();

// Start the receiver object
//keep track of whether the LED is on
//remember when we last received an IR
void loop()
{
    if (irrecv.decode(&decodedSignal) == true) //this is true if a message has been received
    {
        if (millis() - last > 250) {
            //has it been 1/4 sec since last message
            lightState = !lightState;
            //toggle the LED
            digitalWrite(ledPin, lightState);
        }
        last = millis();
        irrecv.resume();
        // watch out for another message
    }
}

If anyone could help me to change the code so that instead of toggling the LED when receiving and IR signal that it will send a signal to the respective pin when is receives a certain IR code which the other arduino Sent. once again if anyone could do this you are a genius
If all this could be done the final request is to merge the servo code with the IR code so that the servos can be controlled when the respective button is pressed.
Thank you to anyone that can help and I apologize for this massive request.

If anyone can help me change the code from controlling one servo to six

What have you tried? Looking at that code, the only difference would be 5 more blocks of code reading 10 more switches moving 5 more servos.

and is there any possibility of controlling the delay period for each servo independantly

The delay there is to allow the servo to move one degree. Do your servos actually take different amounts of time to move one degree?

Actually, scrapping that code, reading and embracing the blink without delay example, and starting over without any delay()s would be a better idea.

If anyone could help me change the code so that 12 buttons would send out an individual code that would be also amazing.

Apparently, you don't understand that code. Each switch already results in sending a unique value. While numberOfKeys is currently 1, changing that to 12, adding 11 more entries to irKeyCodes would be trivial.

Slightly more complicated, but not by much, is removing the assumption that the switches are connected to consecutive pins. Adding an array to contain the actual pin numbers, and having the two necessary lines of code refer to that array instead of actual pin numbers is easy.

If anyone could help me to change the code so that instead of toggling the LED when receiving and IR signal that it will send a signal to the respective pin when is receives a certain IR code which the other arduino Sent.

Trivial. The library being used has a results member in the irrecv struct that is set based on which value the sender sent. Using a switch statement, with 12 cases and an array with 12 pin numbers, would take about 10 minutes to add.

next semester I am required to do a project

Unless you are business major, in which case the project choice is bizarre, you probably need to learn to do the project yourself. Demonstrating that you can beg, borrow, steal, wheedle, and get someone else to do the work for you is probably not going to prove that you have met the course objectives.

I apologize for this massive request.

As well you should.

Thank you very much for your reply and I apologize for this late reply as I was caught up in life.

I read all your comments and have used it to create everything that I had requested.
It took me a while but now it is done and without your advice, I would probably have gone round in circles for a long time.
So below are all the codes I have modified, if there are any mistakes or crap, I don't need in it please feel free to mention it so I can make it better.
Once again thank you for your reply.

Servo being controlled using buttons without delay

#include <Servo.h> // Adds the library code to the sketch
 
Servo myservo; // Create servo object to control the servo

int leftPin = 2;  // The pins which the infrared receiver will toggle
int rightPin = 3; // to control the servo

int pos = 90; // The position the servo will start up on

long previousMillis_0 = 0; // Stores the last time the servo was updated
long previousMillis_1 = 0; 

long interval = 10; // Changes servo speed - increasing the number slows
                    // down the servo speed
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  myservo.write(pos); // Turn the servo to the starting position 
  pinMode(leftPin, INPUT_PULLUP);   // turn on pullup resistors
  pinMode(rightPin, INPUT_PULLUP);
} 
 
 
void loop() 
  // Check to see if it's time to move the servo; If the difference
  // between the current time and last time the servo was moved is
  // bigger than the interval then move the servo.
{ 
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis_0 > interval)
  if(digitalRead(leftPin) == LOW)  
  { 
   previousMillis_0 = currentMillis;    
   if( pos > 0)
      --pos;
    myservo.write(pos);              // tell servo to go to position in variable 'pos'                   
  }
  if(currentMillis - previousMillis_1 > interval) 
  if(digitalRead(rightPin) == LOW)  
  {               
   previousMillis_1 = currentMillis;      
   if( pos < 180)
       ++pos;
    myservo.write(pos);              // tell servo to go to position in variable 'pos'        
  }
}

Servo_Button.ino (1.54 KB)

6 Servos being controlled using buttons without delay
Unable to post code as it exceeds maximum characters so download the attached file.
If you can't see the file please login or create an account to download the file.

Multiple_Servo_Button.ino (12.6 KB)

12 Button Infrared Sender
Unable to post code as it exceeds maximum characters so download the attached file.
If you can't see the file please login or create an account to download the file.

Multiple_Servo_IR_Sender.ino (3.02 KB)

6 Servo infrared controller without delay
Unable to post code as it exceeds maximum characters so download the attached file.
If you can't see the file please login or create an account to download the file.

Multiple_Servo_IR_Receiver.ino (11.4 KB)

I downloaded the final sketches given here to send and receive IR control codes. Uploaded to my mini pro 328 16mHz 5v. The pin assignments in the sketches don't give any movement of the servo I wired to RX arduino pin A0. buttons on tx arduino 4,5. Servo does centre ok. Sensor pcb Data lights working, TX/RX, including a check with a TV remote.

I won't start a new topic on this unless really necessary, because it got some really helpful replies 1st time around and everything seemed to be sorted out.

Question to OP really. Did your sketches work as given, or are some of the annotations/pin assignments changed?

Cheers,