//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
}
}
}
Comments