Amgad Mohamed
Created March 22, 2016

desert storm mineweeper

it use 3 arduino uno to making a map for the mine and controlling tha motors and connecting the robot to the internet to get the map data

Intermediate15 hours308
desert storm mineweeper

Things used in this project

Story

Read more

Schematics

mane citt.

the mane CITT. of he drivers and the all connectiont to the arduinos

Code

final_Main_arduino13.ino

Arduino
//FINAL CODE (ULTRASONIC,SENSOR,MAPPING,with counts)
//Main Arduino
// Say YA RAB
/*
          
         
         
    
*/
//acess coil with 1 pin (as one coil).
//acess 3ultrasonic as one ultrasonic.
#include <Math.h>
char val;
//////
#define encoder0PinA  2
#define encoder0PinB  4
volatile unsigned int counts = 0;
////
int x = 1;
float y;
float a;
float b;
float c;
float count;
int CountTurn = 1;
/////////////////////////////////////////ULRASONIC&&&MINESENSOR RECIEVER///////////////////////////////
#include <SoftwareSerial.h>
#include<NewPing.h>
#define Max_distance 100
/////////////////////////////
#define trigger_pin1 14
#define echo_pin1 15
NewPing ultrasonic1(trigger_pin1, echo_pin1 , Max_distance);

//acess 3 ultrasonic to 1pin
const int ultrasonicPin = 3;
/////////////////////////////////
#define trigger_pin2 17
#define echo_pin2 16
NewPing ultrasonic2(trigger_pin2, echo_pin2 , Max_distance);
////////////////////////////////
#define trigger_pin3 19
#define echo_pin3 18
NewPing ultrasonic3(trigger_pin3, echo_pin3 , Max_distance);
///////MineSensor/////////////////////////////////////////
int metalpin = 5;
int metalvalue = 0 ;
///////////////////////////////////////////////////////////////Ethernet//////////////////////////////
#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 1, 66);
unsigned int localPort = 8888;      // local port to listen on
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
///////////////////////////////////////////////////////////////////////////////////////////
void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac, ip);
  Udp.begin(localPort);
  Serial.begin(9600);
  //encoder
  pinMode(encoder0PinA, INPUT);
  digitalWrite(encoder0PinA, HIGH);       // turn on pullup resistor
  pinMode(encoder0PinB, INPUT);
  digitalWrite(encoder0PinB, HIGH);       // turn on pullup resistor
  attachInterrupt(0, doEncoder, CHANGE);  // encoder pin on interrupt 0 - pin 2
  //metal pin
  pinMode(metalpin , INPUT);
  pinMode(ultrasonicPin, OUTPUT);
}
///////////////////////////////////////////////////////////////////////////////////
void loop() {
  a = y;
  //Mapping
  while (CountTurn % 2 == 1) //odd
  {
    x = CountTurn;
    if (counts <= 39)
    { y = 1;
      a = y;
    }
    else
    { y = counts / 39;
      a = y;
    }
     int packetSize = Udp.parsePacket();
    if (packetSize == 1)
    {

      // read the packet into packetBufffer
      Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);

      for (int i = 0; i < UDP_TX_PACKET_MAX_SIZE; i++) {

                if (packetBuffer[i] == 'S')
        {
          delay(10);
        }
      }
    } 
    ////ULTRASONIC &MINESENSOR RECIEVER///////////////////////////////////
    //Metal detector sensor1
    metalvalue = digitalRead(metalpin);
    if   (metalvalue == HIGH)
    {
      unsigned int distance1 = ultrasonic1.ping_cm();
      unsigned int distance2 = ultrasonic2.ping_cm();
      unsigned int distance3 = ultrasonic3.ping_cm();
      if (  ((1 <= distance1) && (distance1 <= 37)) || ((1 <= distance2) && (distance2 <= 37)) ||  ((1 <= distance3) && (distance3 <= 37))  )
      {
        digitalWrite(ultrasonicPin, HIGH);
        b = a + 0.5;
        c = counts + 20;
        Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
        Udp.print(x);
        Udp.print("," );
        Udp.print(ceil(b));
        Udp.print("," );
        Udp.print("S");
        Udp.print("," );
        Udp.print(c);
        Udp.endPacket();
       /* Serial.print(x);
        Serial.print("," );
        Serial.print(ceil(b));
        Serial.print("," );
        Serial.print("S");
        Serial.print("," );
        Serial.print(c);
        delay(500);*/
      }
      else if
      (  ((distance1 > 37) || (distance1 == 0))  && ((distance2 > 37) || (distance2 == 0)) && ((distance3 > 37) || (distance3 == 0))  )
      { digitalWrite(ultrasonicPin, LOW);
        b = a + 0.5;
        c = counts + 20;
        Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
        Udp.print(x);
        Udp.print("," );
        Udp.print(ceil(b));
        Udp.print("," );
        Udp.print("U");
        Udp.print("," );
        Udp.print(c);
        Udp.endPacket();
        /*Serial.print(x);
        Serial.print("," );
        Serial.print(ceil(b));
        Serial.print("," );
        Serial.print("U");
        Serial.print("," );
        Serial.print(c);
        delay(500);*/
      }
    }
    else if
    (metalvalue == LOW)
    {
      delay(100);
    }
    ///////////////////////
    delay(10);
    if (y == 19) {
      CountTurn++;
    }
  }
  ////////////////////////////////////////EVEN//////////////////
  while (CountTurn % 2 == 0) //even
  {
    x = CountTurn;
    y = 19 + (19 - (counts / 39));
    // ceil(x);
    //ceil(y);
     int packetSize = Udp.parsePacket();
    if (packetSize == 1)
    {

      // read the packet into packetBufffer
      Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);

      for (int i = 0; i < UDP_TX_PACKET_MAX_SIZE; i++) {

                if (packetBuffer[i] == 'S')
        {
          delay(10);
        }
      }
    }
    ////ULTRASONIC &MINESENSOR RECIEVER///////////////////////////////////
    //Metal detector sensor
    metalvalue = digitalRead(metalpin);

    if  (metalvalue == HIGH)
    {
      unsigned int distance1 = ultrasonic1.ping_cm();
      unsigned int distance2 = ultrasonic2.ping_cm();
      unsigned int distance3 = ultrasonic3.ping_cm();
      if
      ( ((1 <= distance1) && (distance1 <= 37)) || ((1 <= distance2) && (distance2 <= 37)) ||  ((1 <= distance3) && (distance3 <= 37))  )
      {
        digitalWrite(ultrasonicPin, HIGH);
        b = a + 0.5;
        c = counts + 20;
        
        Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
        Udp.print(x);
        Udp.print("," );
        Udp.print(ceil(b));
        Udp.print("," );
        Udp.print("S");
        Udp.print("," );
        Udp.print(c);
        Udp.endPacket();
        /*Serial.print(x);
        Serial.print("," );
        Serial.print(ceil(b));
        Serial.print("," );
        Serial.print("S");
        Serial.print("," );
        Serial.print(c);
        delay(500);*/
      }
      else if
      (  ((distance1 > 37) || (distance1 == 0))  && ((distance2 > 37) || (distance2 == 0)) && ((distance3 > 37) || (distance3 == 0))  )
      { digitalWrite(ultrasonicPin, LOW);
        b = a + 0.5;
        c = counts + 20;
       
        Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
        Udp.print(x);
        Udp.print("," );
        Udp.print(ceil(b));
        Udp.print("," );
        Udp.print("U");
        Udp.print("," );
        Udp.print(c);
        Udp.endPacket();
        /*Serial.print(x);
        Serial.print("," );
        Serial.print(ceil(b));
        Serial.print("," );
        Serial.print("U");
        Serial.print("," );
        Serial.print(c);
        delay(500);*/
      }
    }
    else if
    (metalvalue == LOW)
    {
      delay(100);
    }

    /////////////////////GO TO ODD WHILE//////////////////////////
    if (y == 1) {
      CountTurn++;
      counts = 0;
      attachInterrupt(0, doEncoder, CHANGE);
    }
  }
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//Encoder Functions
void doEncoder() {
  /* If pinA and pinB are both high or both low, it is spinning
   * forward. If they're different, it's going backward.
   */
  if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)) {
    counts++;
  } else {
    counts--;
  }
}
void doEncoder_Expanded() {
  if (digitalRead(encoder0PinA) == HIGH) {   // found a low-to-high on channel A
    if (digitalRead(encoder0PinB) == LOW) {  // check channel B to see which way
      // encoder is turning
      counts = counts - 1;         // CCW
    }
    else {
      counts = counts + 1;         // CW
    }
  }
  else                                        // found a high-to-low on channel A
  {
    if (digitalRead(encoder0PinB) == LOW) {   // check channel B to see which way
      // encoder is turning
      counts = counts + 1;          // CW 
    }
    else {
      counts = counts - 1;          // CCW
    }
  }
}

final_second_arduino.ino

Arduino
//Second arduino


// metal detector sensor1
const int metalpin1 = 5 ;    //pin 5 second arduino to pin 5 in Main arduino 
int metalvalue1 = 0 ;
// metal detector sensor2
const int metalpin2 = 10 ;   //pin 9 second arduino to pin 6 in Main arduino     
int metalvalue2 = 0 ;

//Lites
const int lite1 = 3;
const int lite2 = 4;
//sound
const int sound1 = 9;
const int sound2 = 2;
//Van
const int van = 8;
//////ultrasonic1            //pin 10 second arduino to pin 3 in Main arduino 
const int ultrasonic1 = 6; ///  
int ultrasonicchek1 = 0;
//////ultrasonic3
//const int ultrasonic3 = 11;      //
//int ultrasonicchek3 = 0;
//////////////////////////////////////
void setup()
{
  
  pinMode(metalpin1, INPUT);
  pinMode(metalpin2, INPUT);
  pinMode(7, OUTPUT);
  pinMode(lite1, OUTPUT);
  pinMode(lite2, OUTPUT);
  pinMode(sound1, OUTPUT);
  pinMode(sound2, OUTPUT);
  pinMode(van, OUTPUT);
  pinMode(ultrasonic1, INPUT);
}
void loop()
{
  digitalWrite(van, HIGH);
  metalvalue1 = digitalRead(metalpin1);
  metalvalue2 = digitalRead(metalpin2);
  if ((metalvalue1 == HIGH )||(metalvalue2== HIGH ))
  {   
    ultrasonicchek1 = digitalRead(ultrasonic1);
    if (ultrasonicchek1 == HIGH)
    { delay(1000);
      digitalWrite(7, HIGH);
      digitalWrite(lite1, HIGH);
      digitalWrite(sound1, HIGH);
     
    }
    if (ultrasonicchek1 == LOW)
    { delay(1000);
      digitalWrite(7, HIGH);
      digitalWrite(lite2, HIGH);
      digitalWrite(sound2, HIGH);
     
    }
  }
  else if ((metalvalue1 == LOW)&&(metalvalue2 == LOW))
  {
    
    digitalWrite(7, LOW);
    digitalWrite(lite2, LOW);
    digitalWrite(sound2, LOW);
    digitalWrite(lite1, LOW);
    digitalWrite(sound1, LOW);
    delay(10);
  }
  
 
}

Whiplash_Final_control_direction_and_servo_encoder__with_romot.ino

Arduino
#include <Servo.h>
#include <SoftwareSerial.h>
Servo myservo;
unsigned long duration1;
unsigned long duration2;
unsigned long duration3;
unsigned long duration4;
//////////////////////////////////////
const int R1R = 8;  //  
const int R2R = 6;    //    1
const int R3R = 7;    // 2
//////////arm//////////////
const int Bm1R = 11 ;
const int Bm1L = 12;
const int Be1 = 10 ;
/////////////// RELAE FOR ARM
const int RFA = 13;

void setup() {
  Serial.begin(9600);
  myservo.attach(9);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  //Motors Relay
  pinMode(R1R, OUTPUT);
  pinMode(R2R, OUTPUT);
  pinMode(R3R, OUTPUT);
  //arm
  pinMode(Bm1R, OUTPUT);
  pinMode(Bm1L, OUTPUT);
  pinMode(Be1, OUTPUT);
  ////RELE FOR ARM
  pinMode(RFA, OUTPUT);
}
void loop() {
  duration1 = pulseIn(2, HIGH);
  duration2 = pulseIn(3, HIGH);
  duration3 = pulseIn(4, HIGH);
  duration4 = pulseIn(5, HIGH);
  /* Serial.print("duration1 = ");
   Serial.print(duration1);
   Serial.print("   duration2 = ");
   Serial.print(duration2);
   Serial.print("   duration3 = ");
   Serial.print(duration3);
   Serial.print("  duration4 = ");
   Serial.println(duration4);
   delay(10);
  */
  if (duration2 > 1800)
  {
    //Forward
    digitalWrite(R1R, HIGH);
    //////
    digitalWrite(R2R, HIGH);
    digitalWrite(R3R, HIGH);
    delay(10);
  }
  else if (duration2 < 1150)
  {
    //Backward
    digitalWrite(R1R, HIGH);
    ///////
    digitalWrite(R2R, LOW);
    digitalWrite(R3R, LOW);
    delay(10);
  }
  else if (duration1 > 1710)
  {
    //Right
    digitalWrite(R1R, HIGH);
    ///////
    digitalWrite(R2R, HIGH);
    digitalWrite(R3R, LOW);
    delay(10);
  }
  else if (duration1 < 1100)
  {
    //Lift
    digitalWrite(R1R, HIGH);
    //////
    digitalWrite(R2R, LOW);
    digitalWrite(R3R, HIGH);
    delay(10);
  }
  else if ((duration1 = 0) && (duration2 = 0) && (duration3 = 0) && (duration4 = 0) )
  {
    //Stop
    digitalWrite(R1R, LOW);
    delay(100);
    digitalWrite(R2R, LOW);
    digitalWrite(R3R, LOW);
    delay(10);
  }
  else if (duration3 < 1000)
  {
    myservo.write(150);
  }
  else if (duration3 > 1700)
  {
    myservo.write(110);
  }

  else if (duration4 > 1730)
  {
    digitalWrite(RFA, HIGH);
    delay(200);
    analogWrite(Be1, 50);
    digitalWrite(Bm1R, HIGH);
    digitalWrite(Bm1L, LOW);
    delay(10);
  }
  else if (duration4 < 1120)
  {
    digitalWrite(RFA, HIGH);
    delay(200);
    analogWrite(Be1, 100);
    digitalWrite(Bm1R, HIGH);
    digitalWrite(Bm1L, LOW);
    delay(10);
  }
  else
  {
    //Stop
    digitalWrite(RFA, LOW);
    digitalWrite(R1R, LOW);
    delay(100);
    digitalWrite(R2R, LOW);
    digitalWrite(R3R, LOW);
    delay(10);
  }
}

Credits

Amgad Mohamed

Amgad Mohamed

1 project • 0 followers

Comments