mahmoudsabra
Published

NAMLA

Autonomous omni-mobile robot.

AdvancedWork in progress7,218
NAMLA

Things used in this project

Story

Read more

Schematics

I2C INTERFERANCE

Master and Slave

Code

Master

Arduino
//This sketch created by Dimitris Kagioulis.You can modify it at your need.
//Library belongs to Kristian Sloth Lauszus

#include <PS3BT.h>
#include <Wire.h>


USB Usb;
BTD Btd(&Usb);
PS3BT PS3(&Btd);

int sensor1 = A0;
int sensor2 = A1;
int sensor3 = A2;

void setup() {

  Serial.begin(9600);
  Wire.begin();  // 0 adress of Master
  
  if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while (1); //halt
  }

 pinMode(sensor1, INPUT);
 pinMode(sensor2, INPUT);
 pinMode(sensor3, INPUT);
 
}

void loop() 
{

  long safety1 = digitalRead(sensor1);
  long safety2 = digitalRead(sensor2);
  long safety3 = digitalRead(sensor3);
  
  Usb.Task();

    char c = Serial.read();
  
  


  if (PS3.PS3Connected || PS3.PS3NavigationConnected) {

    if (PS3.getButtonPress(UP)) {
      if(safety1 == LOW){
    Wire.beginTransmission(1); // transmit to device #1
    Wire.write( 'A' );            
    Wire.endTransmission();    // stop transmitting

    Wire.beginTransmission(2); // transmit to device #2
    Wire.write( 'A' );            
    Wire.endTransmission();    // stop transmitting  
      }
        else if (safety1 == HIGH){
    Wire.beginTransmission(1); // transmit to device #1
    Wire.write( 'C' );            
    Wire.endTransmission();    // stop transmitting

    Wire.beginTransmission(2); // transmit to device #2
    Wire.write( 'C' );            
    Wire.endTransmission();    // stop transmitting
  }
    }
    else  if  (PS3.getButtonPress(DOWN)) {
    Wire.beginTransmission(1); // transmit to device #1
    Wire.write( 'B' );            
    Wire.endTransmission();    // stop transmitting

    Wire.beginTransmission(2); // transmit to device #2
    Wire.write( 'B' );            
    Wire.endTransmission();    // stop transmitting    
 
    }

       else  if  (PS3.getButtonPress(RIGHT)) {
        if(safety2 == LOW){
    Wire.beginTransmission(1); // transmit to device #1
    Wire.write( 'D' );            
    Wire.endTransmission();    // stop transmitting

    Wire.beginTransmission(2); // transmit to device #2
    Wire.write( 'D' );            
    Wire.endTransmission();    // stop transmitting    
        }
        else if (safety2 == HIGH){
    Wire.beginTransmission(1); // transmit to device #1
    Wire.write( 'C' );            
    Wire.endTransmission();    // stop transmitting

    Wire.beginTransmission(2); // transmit to device #2
    Wire.write( 'C' );            
    Wire.endTransmission();    // stop transmitting
        }
    }
        else  if  (PS3.getButtonPress(LEFT)) {
          if(safety3 == LOW){
    Wire.beginTransmission(1); // transmit to device #1
    Wire.write( 'E' );            
    Wire.endTransmission();    // stop transmitting

    Wire.beginTransmission(2); // transmit to device #2
    Wire.write( 'E' );            
    Wire.endTransmission();    // stop transmitting    
          }
        else if (safety3 == HIGH){
    Wire.beginTransmission(1); // transmit to device #1
    Wire.write( 'C' );            
    Wire.endTransmission();    // stop transmitting

    Wire.beginTransmission(2); // transmit to device #2
    Wire.write( 'C' );            
    Wire.endTransmission();    // stop transmitting   
        }
    }
     else  if  (PS3.getButtonPress(CIRCLE)) {
      
    Wire.beginTransmission(1); // transmit to device #1
    Wire.write( 'F' );            
    Wire.endTransmission();    // stop transmitting

    Wire.beginTransmission(2); // transmit to device #2
    Wire.write( 'F' );            
    Wire.endTransmission();    // stop transmitting 
      
     }
        else  if  (PS3.getButtonPress(SQUARE)) {
          
    Wire.beginTransmission(1); // transmit to device #1
    Wire.write( 'G' );            
    Wire.endTransmission();    // stop transmitting

    Wire.beginTransmission(2); // transmit to device #2
    Wire.write( 'G' );            
    Wire.endTransmission();    // stop transmitting 
     }
  else
  {
    Wire.beginTransmission(1); // transmit to device #1
    Wire.write( 'C' );            
    Wire.endTransmission();    // stop transmitting

    Wire.beginTransmission(2); // transmit to device #2
    Wire.write( 'C' );            
    Wire.endTransmission();    // stop transmitting
  }
 

   if (PS3.PS3Connected) {

      if (PS3.getButtonClick(PS)) {
        PS3.disconnect();
      }
    }
  
  }
  
  }

  

Slave 1

Arduino
// Start the I2C Bus as Slave 1

#include <Wire.h>
void setup() 
{
  Wire.begin(1); // Adress 1
  Wire.onReceive(reciveEvent);

    //Setup Channel A
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin

  //Setup Channel B
  pinMode(13, OUTPUT); //Initiates Motor Channel A pin
  pinMode(8, OUTPUT);  //Initiates Brake Channel A pin
}

void loop() 
{
}

void reciveEvent ( int howMany)
{
  char c = Wire.read();
  if ( c == 'A')
  {
     
  //Motor A forward @ full speed
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 200);   //Spins the motor on Channel A at full speed

  //Motor B forward @ full speed
  digitalWrite(13, HIGH); //Establishes forward direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 200);   //Spins the motor on Channel B at full speed

  }
  else if ( c == 'B')
  {
  //Motor A backword @ full speed
  digitalWrite(12, LOW); //Establishes backword direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 200);   //Spins the motor on Channel A at full speed

  //Motor B backword @ full speed
  digitalWrite(13, LOW); //Establishes backword direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 200);   //Spins the motor on Channel B at full speed
  }

   else if ( c == 'D')
  {
  //Motor A backword @ full speed
  digitalWrite(12, LOW); //Establishes backword direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 200);   //Spins the motor on Channel A at full speed

  //Motor B backword @ full speed
  digitalWrite(13, HIGH); //Establishes backword direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 200);   //Spins the motor on Channel B at full speed
  }

  else if ( c == 'E')
  {
  //Motor A backword @ full speed
  digitalWrite(12, HIGH); //Establishes backword direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 200);   //Spins the motor on Channel A at full speed

  //Motor B backword @ full speed
  digitalWrite(13, LOW); //Establishes backword direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 200);   //Spins the motor on Channel B at full speed
  }
    else if ( c == 'F')
  {
  //Motor A backword @ full speed
  digitalWrite(12, LOW); //Establishes backword direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 200);   //Spins the motor on Channel A at full speed

  //Motor B backword @ full speed
  digitalWrite(13, HIGH); //Establishes backword direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 200);   //Spins the motor on Channel B at full speed
  }
  else if ( c == 'G')
  {
  //Motor A backword @ full speed
  digitalWrite(12, HIGH); //Establishes backword direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 200);   //Spins the motor on Channel A at full speed

  //Motor B backword @ full speed
  digitalWrite(13, LOW); //Establishes backword direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 200);   //Spins the motor on Channel B at full speed
  }
 else if ( c == 'C')
  {
     
      digitalWrite(8, HIGH);  //Engage the Brake for Channel B
      digitalWrite(9, HIGH);  //Engage the Brake for Channel A

  }

}

Slave 2

Arduino
// Start the I2C Bus as Slave 2

#include <Wire.h>
void setup() 
{
  Wire.begin(2); // Adress 2
  Wire.onReceive(reciveEvent);

    //Setup Channel A
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin

  //Setup Channel B
  pinMode(13, OUTPUT); //Initiates Motor Channel A pin
  pinMode(8, OUTPUT);  //Initiates Brake Channel A pin
}

void loop() 
{
}

void reciveEvent ( int howMany)
{
  char c = Wire.read();
  if ( c == 'A')
  {
     
  //Motor A forward @ full speed
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 200);   //Spins the motor on Channel A at full speed

  //Motor B forward @ full speed
  digitalWrite(13, HIGH); //Establishes forward direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 200);   //Spins the motor on Channel B at full speed

  }
  else if ( c == 'B')
  {
  //Motor A backword @ full speed
  digitalWrite(12, LOW); //Establishes backword direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 200);   //Spins the motor on Channel A at full speed

  //Motor B backword @ full speed
  digitalWrite(13, LOW); //Establishes backword direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 200);   //Spins the motor on Channel B at full speed
  }

   else if ( c == 'D')
  {
  //Motor A backword @ full speed
  digitalWrite(12, LOW); //Establishes backword direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 200);   //Spins the motor on Channel A at full speed

  //Motor B backword @ full speed
  digitalWrite(13, HIGH); //Establishes backword direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 200);   //Spins the motor on Channel B at full speed
  }
    else if ( c == 'E')
  {
  //Motor A backword @ full speed
  digitalWrite(12, HIGH); //Establishes backword direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 200);   //Spins the motor on Channel A at full speed

  //Motor B backword @ full speed
  digitalWrite(13, LOW); //Establishes backword direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 200);   //Spins the motor on Channel B at full speed
  }
    else if ( c == 'F')
  {
  //Motor A backword @ full speed
  digitalWrite(12, HIGH); //Establishes backword direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 200);   //Spins the motor on Channel A at full speed

  //Motor B backword @ full speed
  digitalWrite(13, LOW); //Establishes backword direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 200);   //Spins the motor on Channel B at full speed
  }
    else if ( c == 'G')
  {
  //Motor A backword @ full speed
  digitalWrite(12, LOW); //Establishes backword direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 200);   //Spins the motor on Channel A at full speed

  //Motor B backword @ full speed
  digitalWrite(13, HIGH); //Establishes backword direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 200);   //Spins the motor on Channel B at full speed
  }
 else if ( c == 'C')
  {
     
      digitalWrite(8, HIGH);  //Engage the Brake for Channel B
      digitalWrite(9, HIGH);  //Engage the Brake for Channel A

  }

}

Credits

mahmoudsabra

mahmoudsabra

1 project • 13 followers

Comments