Goody599
Published © GPL3+

$30 Swarm Nodes

The ability to mass produce and create a large scale swarm with interchangeable pieces for a variety of purposes and algorithms.

AdvancedWork in progressOver 2 days277
$30 Swarm Nodes

Things used in this project

Hardware components

Photon
Particle Photon
×2
Argon
Particle Argon
×1
DC Motor, 12 V
DC Motor, 12 V
×6
rubber wheel
×6
support wheel/trolley wheel
×3
Infra red obstacle sensor
×12
2 WD Motor Chassis
×3
White cardboard
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×3
4xAA battery holder
4xAA battery holder
×3
L298N Dual Motor Driver
×3
Jumper wires (generic)
Jumper wires (generic)
×72

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE

Hand tools and fabrication machines

Extraction Tool, 6 Piece Screw Extractor & Screwdriver Set
Extraction Tool, 6 Piece Screw Extractor & Screwdriver Set
Scissors, Free Fall
Scissors, Free Fall
Drill / Driver, Cordless
Drill / Driver, Cordless
Tape, Double Sided
Tape, Double Sided

Story

Read more

Schematics

Project Artefact

Overview of the entire project

Code

Leader Code

C/C++
C++ Particle.io code, run this on the leader
// This #include statement was automatically added by the Particle IDE.
#include <google-maps-device-locator.h>
#include <stdio.h>
#include <stdlib.h>


GoogleMapsDeviceLocator locator;

SerialLogHandler logHandler;
//----------------

int NodeLeftS1;
int NodeLeftS2;
int NodeLeftS3;
int NodeLeftS4;


int NodeRightS1;
int NodeRightS2;
int NodeRightS3;
int NodeRightS4;





//Motor A
int IN1 = D7;
int IN2 = D6;
int SPEEDPINA = D1;


//Motor B
int IN3 = D5;
int IN4 = D4;
int SPEEDPINB = D0;



//IR Sensors Declaration
int IRSensor1 = A0;

int IRSensor2 = A1;

int IRSensor3 = A2;

int IRSensor4 = A3;

//IR Sensor Read (Count)

int Sensor1;
int Sensor2;
int Sensor3;
int Sensor4;

void setup()
{
    //MOTORS
    pinMode(IN1, OUTPUT);
    pinMode(IN2, OUTPUT);
    pinMode(IN3, OUTPUT);
    pinMode(IN4, OUTPUT);
    pinMode(SPEEDPINA, OUTPUT);
    pinMode(SPEEDPINB, OUTPUT);
    
    
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, HIGH);
    digitalWrite(IN3, HIGH);
    digitalWrite(IN4, HIGH);

    //IR Sensors
    pinMode(IRSensor1, INPUT);
    pinMode(IRSensor2, INPUT);
    pinMode(IRSensor3, INPUT);
    pinMode(IRSensor4, INPUT);
    
    
    Particle.subscribe("LS1NoObject", loop2);
    
    //Particle.subscribe("LS1NoObject", loop2);
    
    Particle.subscribe("LS2NoObject", loop2);
    
    
    //Google Maps
    
    
    Serial.begin(9600);
    
    //Particle.publish("test2", "This is a test");
    
    

    //locator.withLocatePeriodic(30);
    
    
}

//Try adding both within the loop here
void loop()
{
    //Try analog Read Tommorow if this still does not work
    Sensor1 = digitalRead(IRSensor1);
    Sensor2 = digitalRead(IRSensor2);
    Sensor3 = digitalRead(IRSensor3);
    Sensor4 = digitalRead(IRSensor4);
    
    //char Sensor1Str[30];
    //sprintf(Sensor1Str,"%d", Sensor1);
    //Particle.publish("LeaderSensor1", Sensor1Str, 21600, PUBLIC);
    
    if (Sensor1 == 0 && Sensor2 == 1 && Sensor3 == 0 && Sensor4 == 1)
        {
            Particle.publish("Should be going", PUBLIC);
            //Particle.publish("Swarm Mates Found", PUBLIC);
            
            digitalWrite(IN2, LOW);
            digitalWrite(IN1, HIGH);
        
            analogWrite(SPEEDPINA, 255);
        
            digitalWrite(IN4, LOW);
            digitalWrite(IN3, HIGH);
        
            analogWrite(SPEEDPINB, 255);
        }
    else
    {
        StopAll();
        
    }
    //Object Detected on sensor 1
    //if (Sensor1 == 0)
    //{
    //    Particle.publish("LeaderS1", "Object", 60, PRIVATE);
    //    delay(1);
    //}
    
    
    //if (Sensor2 == 0)
    //{
    //    Particle.publish("LeaderS2", "Object", 60, PRIVATE);
    //    delay(1);
    //}
    
    
    if (Sensor1 == 0)
    {
        Particle.publish("LeaderS1", "Object");
        delay(3000);
    }
    if (Sensor1 == 1)
    {
        Particle.publish("LeaderS1NoObject", "NoObject");
        delay(3000);
    }
    
    
    if (Sensor3 == 0)
    {
        Particle.publish("LeaderS3", "Object");
        delay(3000);
    }
    if (Sensor3 == 1)
    {
        Particle.publish("LeaderS3NoObject", "NoObject");
        delay(3000);
    }
    
    
    //if (Sensor4 == 0)
    //{
    //    Particle.publish("LeaderS4", "Object", 60, PRIVATE);
    //    delay(1);
    //}
    
    
  // get here if event publish did not work
}
    
    
    /*digitalWrite(IN2, LOW);
    digitalWrite(IN1, HIGH);
        
    analogWrite(SPEEDPINA, 255);
        
    digitalWrite(IN4, LOW);
    digitalWrite(IN3, HIGH);
        
    analogWrite(SPEEDPINB, 255);*/


void StopAll()
{
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, LOW);
    
    analogWrite(SPEEDPINA, 100);
    analogWrite(SPEEDPINB, 100);
    
    
}

void loop2(const char *event, const char *data)
{
    
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, LOW);
    /*if (Sensor1 == 0)
    {
        Particle.publish("LeaderS1", "Object");
        delay(3000);
    }
    if (Sensor1 == 1)
    {
        Particle.publish("LeaderS1NoObject", "NoObject");
        delay(3000);
    }
    
    
    if (Sensor3 == 0)
    {
        Particle.publish("LeaderS3", "Object");
        delay(3000);
    }
    if (Sensor3 == 1)
    {
        Particle.publish("LeaderS3NoObject", "NoObject");
        delay(3000);
    }*/
    
    StopAll();
}


/*void SensorTest()
{
    if (Sensor1 == 1 && Sensor2 == 1 && Sensor3 == 1 && Sensor4 == 1)
        {
            Particle.publish("No Object", PUBLIC);
            
            digitalWrite(IN1, HIGH);
            digitalWrite(IN2, LOW);
            analogWrite(SPEEDPINA, 150);
            delay(2000);
            digitalWrite(IN1, LOW);
            digitalWrite(IN2, LOW);
        }

    //<<<<<<<<<<<<<<<<<<<Object Detected on 4 Sensors>>>>>>>>>>>>>>>>>>>>>>>>>>>
    else if (Sensor1 == 0 && Sensor2 == 0 && Sensor3 == 0 && Sensor4 == 0)
    {
        Particle.publish("Object detected on All Sensors 1,2,3,4" , PUBLIC);
    }
    //<<<<<<<<<<<<<<<<<<<<Object Detected on Three Sensors >>>>>>>>>>>>>>>>>>>>>
    
    
    else if (Sensor1 == 0 && Sensor2 == 0 && Sensor3 == 0 && Sensor4 == 1)
    {
        Particle.publish("Object detected on Sensor 1,2,3" , PUBLIC);
    }
    
    else if (Sensor1 == 0 && Sensor2 == 0 && Sensor3 == 1 && Sensor4 == 4)
    {
        Particle.publish("Object detected on Sensor 1,2,4" , PUBLIC);
    }
    
    else if (Sensor1 == 0 && Sensor2 == 1 && Sensor3 == 0 && Sensor4 == 0)
    {
        Particle.publish("Object detected on Sensor 1,3,4" , PUBLIC);
    }
    
    else if (Sensor1 == 1 && Sensor2 == 0 && Sensor3 == 0 && Sensor4 == 0)
    {
        Particle.publish("Object detected on Sensor 2,3,4" , PUBLIC);
    }
    
    
    
    //<<<<<<<<<<<<<<<<<<<<<<Object Detected on two sensors>>>>>>>>>>>>>>>>>>>>>>>>
    
    
    //Sensor 1
    
    else if (Sensor1 == 0 && Sensor2 == 0 && Sensor3 == 1 && Sensor4 == 1)
    {
        Particle.publish("Object detected on Sensor 1 and 2" , PUBLIC);
    }
    
    else if (Sensor1 == 0 && Sensor2 == 1 && Sensor3 == 0 && Sensor4 == 1)
    {
        Particle.publish("Object detected on Sensor 1 and 3" , PUBLIC);
    }
    
    else if (Sensor1 == 0 && Sensor2 == 1 && Sensor3 == 1 && Sensor4 == 0)
    {
        Particle.publish("Object detected on Sensor 1 and 4" , PUBLIC);
    }
    
    
    //Sensor 2
    
    else if (Sensor1 == 1 && Sensor2 == 0 && Sensor3 == 0 && Sensor4 == 1)
    {
        Particle.publish("Object detected on Sensor 2 and 3" , PUBLIC);
    }
    
    else if (Sensor1 == 1 && Sensor2 == 0 && Sensor3 == 1 && Sensor4 == 0)
    {
        Particle.publish("Object detected on Sensor 2 and 4" , PUBLIC);
    }
    
    //Sensor 3
    
    else if (Sensor1 == 1 && Sensor2 == 1 && Sensor3 == 0 && Sensor4 == 0)
    {
        Particle.publish("Object detected on Sensor 3 and 4" , PUBLIC);
    }
    
    //<<<<<<<<<<<<<<<<<<<<<Object Detected on Single Sensor>>>>>>>>>>>>>>>>>>>>>>>
    else if (Sensor1 == 0 && Sensor2 == 1 && Sensor3 == 1 && Sensor4 == 1)
        {
            Particle.publish("Object detected on sensor 1", PUBLIC);
        }
        
    else if (Sensor1 == 1 && Sensor2 == 0 && Sensor3 == 1 && Sensor4 == 1)
        {
            Particle.publish("Object detected on sensor 2", PUBLIC);
        }
        
    else if (Sensor1 == 1 && Sensor2 == 1 && Sensor3 == 0 && Sensor4 == 1)
        {
            Particle.publish("Object detected on sensor 3", PUBLIC);
        }
        
    else if (Sensor1 == 1 && Sensor2 == 1 && Sensor3 == 1 && Sensor4 == 4)
        {
            Particle.publish("Object detected on sensor 4", PUBLIC);
        }
}


void SensorFinder()
{
    if (Sensor1 == 0)
    {
        digitalWrite(IN2, LOW);
        digitalWrite(IN1, HIGH);
        
        analogWrite(SPEEDPINA, 150);
        
        digitalWrite(IN4, LOW);
        digitalWrite(IN3, HIGH);
        
        analogWrite(SPEEDPINB, 150);
    }
    
    //Nothing dont go
    if (Sensor1 == 1)
    {
        digitalWrite(IN2, LOW);
        digitalWrite(IN1, LOW);
        
        digitalWrite(IN4, LOW);
        digitalWrite(IN3, LOW);
    }
    
    //Object go
    //Sensor 2
    if (Sensor2 == 0)
    {
        digitalWrite(IN2, LOW);
        digitalWrite(IN1, HIGH);
        
        analogWrite(SPEEDPINA, 150);
        
        digitalWrite(IN4, LOW);
        digitalWrite(IN3, HIGH);
        
        analogWrite(SPEEDPINB, 150);
    }
    
    //Nothing dont go
    if (Sensor2 == 1)
    {
        digitalWrite(IN2, LOW);
        digitalWrite(IN1, LOW);
        
        digitalWrite(IN4, LOW);
        digitalWrite(IN3, LOW);
    }
    
    
    //Sensor3
    if (Sensor3 == 0)
    {
        digitalWrite(IN2, LOW);
        digitalWrite(IN1, HIGH);
        
        analogWrite(SPEEDPINA, 150);
        
        digitalWrite(IN4, LOW);
        digitalWrite(IN3, HIGH);
        
        analogWrite(SPEEDPINB, 150);
    }
    
    //Nothing dont go
    if (Sensor3 == 1)
    {
        digitalWrite(IN2, LOW);
        digitalWrite(IN1, LOW);
        
        digitalWrite(IN4, LOW);
        digitalWrite(IN3, LOW);
    }
    
    //Sensor 4
    if (Sensor4 == 0)
    {
        digitalWrite(IN2, LOW);
        digitalWrite(IN1, HIGH);
        
        analogWrite(SPEEDPINA, 150);
        
        digitalWrite(IN4, LOW);
        digitalWrite(IN3, HIGH);
        
        analogWrite(SPEEDPINB, 150);
    }
    
    //Nothing dont go
    if (Sensor4 == 1)
    {
        digitalWrite(IN2, LOW);
        digitalWrite(IN1, LOW);
        
        digitalWrite(IN4, LOW);
        digitalWrite(IN3, LOW);
    }
}
*/

//Device ID - 270035000147373336323230
//Accesss Token - bee763562c709a8cec07705008beb1e4f8372e38

Left Node Code

C/C++
Particle.io Left Node Code, run it on the left node in the swarm
// This #include statement was automatically added by the Particle IDE.
#include <google-maps-device-locator.h>

GoogleMapsDeviceLocator locator;

SerialLogHandler logHandler;
//----------------

//Motor A




int IN1 = D7;
int IN2 = D6;
int SPEEDPINA = D1;


//Motor B
int IN3 = D5;
int IN4 = D4;
int SPEEDPINB = D0;


//IR Sensors Declaration
int IRSensor1 = A0;

int IRSensor2 = A1;

int IRSensor3 = A2;

int IRSensor4 = A3;

//IR Sensor Read (Count)

int Sensor1;
int Sensor2;
int Sensor3;
int Sensor4;




void setup()
{
    
    //MOTORS
    pinMode(IN1, OUTPUT);
    pinMode(IN2, OUTPUT);
    pinMode(IN3, OUTPUT);
    pinMode(IN4, OUTPUT);
    pinMode(SPEEDPINA, OUTPUT);
    pinMode(SPEEDPINB, OUTPUT);
    
    
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, HIGH);
    digitalWrite(IN3, HIGH);
    digitalWrite(IN4, HIGH);

    //IR Sensors
    pinMode(IRSensor1, INPUT);
    pinMode(IRSensor2, INPUT);
    pinMode(IRSensor3, INPUT);
    pinMode(IRSensor4, INPUT);
    
    
    
    //Google Maps
    
    Particle.subscribe("LeaderS3", loop);
    Particle.subscribe("LeaderS3NoObject", loop2);
    Serial.begin(9600);
    
    
    

    //locator.withLocatePeriodic(30);
    
    
}


//void loop(const char *event, const char *data)
void loop(const char *event, const char *data)
{
    //Try analog Read Tommorow if this still does not work
    Sensor1 = digitalRead(IRSensor1);
    Sensor2 = digitalRead(IRSensor2);
    Sensor3 = digitalRead(IRSensor3);
    Sensor4 = digitalRead(IRSensor4);
    
    //Particle.publish(event, "This is a test");
   // const char LS1 = LeaderS1(char *event,char *LS1);
    //const char LeaderSensor1 = LS1;
    
    //char Sensor1Str[30];
    //sprintf(Sensor1Str,"%d", Sensor1);
    //Particle.publish("LeaderSensor1", Sensor1Str, 21600, PUBLIC);
    
    if (Sensor1 == 0)
    {
        Particle.publish("LS1", "Obect");
        delay(3000);
    }
    if (Sensor1 == 1)
    {
        Particle.publish("LS1NoObject", "Obect");
        delay(3000);
    }
    
    if (Sensor1 == 0 && Sensor2 == 1 && Sensor3 == 1 && Sensor4 == 1)
        {
            //Particle.publish("Swarm Mates Found", PUBLIC);
            
            digitalWrite(IN2, LOW);
            digitalWrite(IN1, HIGH);
        
            analogWrite(SPEEDPINA, 255);
        
            digitalWrite(IN4, LOW);
            digitalWrite(IN3, HIGH);
        
            analogWrite(SPEEDPINB, 255);
        }
    else
    {
        StopAll();
        
    }
}

void loop2(const char *event, const char *data)
{
    /*if (Sensor1 == 0)
    {
        Particle.publish("LS1", "Obect");
        delay(3000);
    }
    if (Sensor1 == 1)
    {
        Particle.publish("LS1NoObject", "Obect");
        delay(3000);
    }*/
    
    StopAll();
}

void StopAll()
{
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, LOW);
    
    analogWrite(SPEEDPINA, 100);
    analogWrite(SPEEDPINB, 100);
    
    
}

/*void myHandler(const char *event, const char *data)
{
  int i = 0;
  i++;
  Serial.print(i);
  Serial.print(event);
  Serial.print(", data: ");
  if (data)
  {
    Serial.println(data);
    Particle.publish("Leader", "This is a test");
  }
}
*/

/*void SensorTest()
{
    if (Sensor1 == 1 && Sensor2 == 1 && Sensor3 == 1 && Sensor4 == 1)
        {
            Particle.publish("No Object", PUBLIC);
            
            digitalWrite(IN1, HIGH);
            digitalWrite(IN2, LOW);
            analogWrite(SPEEDPINA, 150);
            delay(2000);
            digitalWrite(IN1, LOW);
            digitalWrite(IN2, LOW);
        }

    //<<<<<<<<<<<<<<<<<<<Object Detected on 4 Sensors>>>>>>>>>>>>>>>>>>>>>>>>>>>
    else if (Sensor1 == 0 && Sensor2 == 0 && Sensor3 == 0 && Sensor4 == 0)
    {
        Particle.publish("Object detected on All Sensors 1,2,3,4" , PUBLIC);
    }
    //<<<<<<<<<<<<<<<<<<<<Object Detected on Three Sensors >>>>>>>>>>>>>>>>>>>>>
    
    
    else if (Sensor1 == 0 && Sensor2 == 0 && Sensor3 == 0 && Sensor4 == 1)
    {
        Particle.publish("Object detected on Sensor 1,2,3" , PUBLIC);
    }
    
    else if (Sensor1 == 0 && Sensor2 == 0 && Sensor3 == 1 && Sensor4 == 4)
    {
        Particle.publish("Object detected on Sensor 1,2,4" , PUBLIC);
    }
    
    else if (Sensor1 == 0 && Sensor2 == 1 && Sensor3 == 0 && Sensor4 == 0)
    {
        Particle.publish("Object detected on Sensor 1,3,4" , PUBLIC);
    }
    
    else if (Sensor1 == 1 && Sensor2 == 0 && Sensor3 == 0 && Sensor4 == 0)
    {
        Particle.publish("Object detected on Sensor 2,3,4" , PUBLIC);
    }
    
    
    
    //<<<<<<<<<<<<<<<<<<<<<<Object Detected on two sensors>>>>>>>>>>>>>>>>>>>>>>>>
    
    
    //Sensor 1
    
    else if (Sensor1 == 0 && Sensor2 == 0 && Sensor3 == 1 && Sensor4 == 1)
    {
        Particle.publish("Object detected on Sensor 1 and 2" , PUBLIC);
    }
    
    else if (Sensor1 == 0 && Sensor2 == 1 && Sensor3 == 0 && Sensor4 == 1)
    {
        Particle.publish("Object detected on Sensor 1 and 3" , PUBLIC);
    }
    
    else if (Sensor1 == 0 && Sensor2 == 1 && Sensor3 == 1 && Sensor4 == 0)
    {
        Particle.publish("Object detected on Sensor 1 and 4" , PUBLIC);
    }
    
    
    //Sensor 2
    
    else if (Sensor1 == 1 && Sensor2 == 0 && Sensor3 == 0 && Sensor4 == 1)
    {
        Particle.publish("Object detected on Sensor 2 and 3" , PUBLIC);
    }
    
    else if (Sensor1 == 1 && Sensor2 == 0 && Sensor3 == 1 && Sensor4 == 0)
    {
        Particle.publish("Object detected on Sensor 2 and 4" , PUBLIC);
    }
    
    //Sensor 3
    
    else if (Sensor1 == 1 && Sensor2 == 1 && Sensor3 == 0 && Sensor4 == 0)
    {
        Particle.publish("Object detected on Sensor 3 and 4" , PUBLIC);
    }
    
    //<<<<<<<<<<<<<<<<<<<<<Object Detected on Single Sensor>>>>>>>>>>>>>>>>>>>>>>>
    else if (Sensor1 == 0 && Sensor2 == 1 && Sensor3 == 1 && Sensor4 == 1)
        {
            Particle.publish("Object detected on sensor 1", PUBLIC);
        }
        
    else if (Sensor1 == 1 && Sensor2 == 0 && Sensor3 == 1 && Sensor4 == 1)
        {
            Particle.publish("Object detected on sensor 2", PUBLIC);
        }
        
    else if (Sensor1 == 1 && Sensor2 == 1 && Sensor3 == 0 && Sensor4 == 1)
        {
            Particle.publish("Object detected on sensor 3", PUBLIC);
        }
        
    else if (Sensor1 == 1 && Sensor2 == 1 && Sensor3 == 1 && Sensor4 == 4)
        {
            Particle.publish("Object detected on sensor 4", PUBLIC);
        }
}


void SensorFinder()
{
    if (Sensor1 == 0)
    {
        digitalWrite(IN2, LOW);
        digitalWrite(IN1, HIGH);
        
        analogWrite(SPEEDPINA, 150);
        
        digitalWrite(IN4, LOW);
        digitalWrite(IN3, HIGH);
        
        analogWrite(SPEEDPINB, 150);
    }
    
    //Nothing dont go
    if (Sensor1 == 1)
    {
        digitalWrite(IN2, LOW);
        digitalWrite(IN1, LOW);
        
        digitalWrite(IN4, LOW);
        digitalWrite(IN3, LOW);
    }
    
    //Object go
    //Sensor 2
    if (Sensor2 == 0)
    {
        digitalWrite(IN2, LOW);
        digitalWrite(IN1, HIGH);
        
        analogWrite(SPEEDPINA, 150);
        
        digitalWrite(IN4, LOW);
        digitalWrite(IN3, HIGH);
        
        analogWrite(SPEEDPINB, 150);
    }
    
    //Nothing dont go
    if (Sensor2 == 1)
    {
        digitalWrite(IN2, LOW);
        digitalWrite(IN1, LOW);
        
        digitalWrite(IN4, LOW);
        digitalWrite(IN3, LOW);
    }
    
    
    //Sensor3
    if (Sensor3 == 0)
    {
        digitalWrite(IN2, LOW);
        digitalWrite(IN1, HIGH);
        
        analogWrite(SPEEDPINA, 150);
        
        digitalWrite(IN4, LOW);
        digitalWrite(IN3, HIGH);
        
        analogWrite(SPEEDPINB, 150);
    }
    
    //Nothing dont go
    if (Sensor3 == 1)
    {
        digitalWrite(IN2, LOW);
        digitalWrite(IN1, LOW);
        
        digitalWrite(IN4, LOW);
        digitalWrite(IN3, LOW);
    }
    
    //Sensor 4
    if (Sensor4 == 0)
    {
        digitalWrite(IN2, LOW);
        digitalWrite(IN1, HIGH);
        
        analogWrite(SPEEDPINA, 150);
        
        digitalWrite(IN4, LOW);
        digitalWrite(IN3, HIGH);
        
        analogWrite(SPEEDPINB, 150);
    }
    
    //Nothing dont go
    if (Sensor4 == 1)
    {
        digitalWrite(IN2, LOW);
        digitalWrite(IN1, LOW);
        
        digitalWrite(IN4, LOW);
        digitalWrite(IN3, LOW);
    }
}
*/

//Device ID - 270035000147373336323230
//Accesss Token - bee763562c709a8cec07705008beb1e4f8372e38

Right Node Code

C/C++
Particle.io Code to run on the right node in the swarm
// This #include statement was automatically added by the Particle IDE.
#include <google-maps-device-locator.h>



GoogleMapsDeviceLocator locator;

SerialLogHandler logHandler;
//----------------

//Motor A
int IN1 = D7;
int IN2 = D6;
int SPEEDPINA = D1;


//Motor B
int IN3 = D5;
int IN4 = D4;
int SPEEDPINB = D0;



//IR Sensors Declaration
int IRSensor1 = A0;

int IRSensor2 = A1;

int IRSensor3 = A2;

int IRSensor4 = A3;

//IR Sensor Read (Count)

int Sensor1;
int Sensor2;
int Sensor3;
int Sensor4;

void setup()
{
    //MOTORS
    pinMode(IN1, OUTPUT);
    pinMode(IN2, OUTPUT);
    pinMode(IN3, OUTPUT);
    pinMode(IN4, OUTPUT);
    pinMode(SPEEDPINA, OUTPUT);
    pinMode(SPEEDPINB, OUTPUT);
    
    
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, HIGH);
    digitalWrite(IN3, HIGH);
    digitalWrite(IN4, HIGH);

    //IR Sensors
    pinMode(IRSensor1, INPUT);
    pinMode(IRSensor2, INPUT);
    pinMode(IRSensor3, INPUT);
    pinMode(IRSensor4, INPUT);
    
    
    //Google Maps
    Particle.subscribe("LeaderS1", loop);
    Particle.subscribe("LeaderS1NoObject", loop2);
    //Particle.subscribe("LeaderSensor1", myHandler, ALL_DEVICES);
    Serial.begin(9600);
    
    
    

    //locator.withLocatePeriodic(30);
    
    
}


void loop(const char *event, const char *data)
{
    //Try analog Read Tommorow if this still does not work
    Sensor1 = digitalRead(IRSensor1);
    Sensor2 = digitalRead(IRSensor2);
    Sensor3 = digitalRead(IRSensor3);
    Sensor4 = digitalRead(IRSensor4);
    
    if (Sensor3 == 1)
    {
        Particle.publish("RS3NoObject", "NoObject");
        delay(3000);
    }
    
    
    
    if (Sensor1 == 1 && Sensor2 == 1 && Sensor3 == 0 && Sensor4 == 1)
        {
            //Particle.publish("Swarm Mates Found", PUBLIC);
            
            digitalWrite(IN2, LOW);
            digitalWrite(IN1, HIGH);
        
            analogWrite(SPEEDPINA, 255);
        
            digitalWrite(IN4, LOW);
            digitalWrite(IN3, HIGH);
        
            analogWrite(SPEEDPINB, 255);
        }
    else
    {
        StopAll();
        
    }
}

void loop2(const char *event, const char *data)
{
    StopAll();
}

void StopAll()
{
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, LOW);
    
    analogWrite(SPEEDPINA, 100);
    analogWrite(SPEEDPINB, 100);
    
    
}


/*void SensorTest()
{
    if (Sensor1 == 1 && Sensor2 == 1 && Sensor3 == 1 && Sensor4 == 1)
        {
            Particle.publish("No Object", PUBLIC);
            
            digitalWrite(IN1, HIGH);
            digitalWrite(IN2, LOW);
            analogWrite(SPEEDPINA, 150);
            delay(2000);
            digitalWrite(IN1, LOW);
            digitalWrite(IN2, LOW);
        }

    //<<<<<<<<<<<<<<<<<<<Object Detected on 4 Sensors>>>>>>>>>>>>>>>>>>>>>>>>>>>
    else if (Sensor1 == 0 && Sensor2 == 0 && Sensor3 == 0 && Sensor4 == 0)
    {
        Particle.publish("Object detected on All Sensors 1,2,3,4" , PUBLIC);
    }
    //<<<<<<<<<<<<<<<<<<<<Object Detected on Three Sensors >>>>>>>>>>>>>>>>>>>>>
    
    
    else if (Sensor1 == 0 && Sensor2 == 0 && Sensor3 == 0 && Sensor4 == 1)
    {
        Particle.publish("Object detected on Sensor 1,2,3" , PUBLIC);
    }
    
    else if (Sensor1 == 0 && Sensor2 == 0 && Sensor3 == 1 && Sensor4 == 4)
    {
        Particle.publish("Object detected on Sensor 1,2,4" , PUBLIC);
    }
    
    else if (Sensor1 == 0 && Sensor2 == 1 && Sensor3 == 0 && Sensor4 == 0)
    {
        Particle.publish("Object detected on Sensor 1,3,4" , PUBLIC);
    }
    
    else if (Sensor1 == 1 && Sensor2 == 0 && Sensor3 == 0 && Sensor4 == 0)
    {
        Particle.publish("Object detected on Sensor 2,3,4" , PUBLIC);
    }
    
    
    
    //<<<<<<<<<<<<<<<<<<<<<<Object Detected on two sensors>>>>>>>>>>>>>>>>>>>>>>>>
    
    
    //Sensor 1
    
    else if (Sensor1 == 0 && Sensor2 == 0 && Sensor3 == 1 && Sensor4 == 1)
    {
        Particle.publish("Object detected on Sensor 1 and 2" , PUBLIC);
    }
    
    else if (Sensor1 == 0 && Sensor2 == 1 && Sensor3 == 0 && Sensor4 == 1)
    {
        Particle.publish("Object detected on Sensor 1 and 3" , PUBLIC);
    }
    
    else if (Sensor1 == 0 && Sensor2 == 1 && Sensor3 == 1 && Sensor4 == 0)
    {
        Particle.publish("Object detected on Sensor 1 and 4" , PUBLIC);
    }
    
    
    //Sensor 2
    
    else if (Sensor1 == 1 && Sensor2 == 0 && Sensor3 == 0 && Sensor4 == 1)
    {
        Particle.publish("Object detected on Sensor 2 and 3" , PUBLIC);
    }
    
    else if (Sensor1 == 1 && Sensor2 == 0 && Sensor3 == 1 && Sensor4 == 0)
    {
        Particle.publish("Object detected on Sensor 2 and 4" , PUBLIC);
    }
    
    //Sensor 3
    
    else if (Sensor1 == 1 && Sensor2 == 1 && Sensor3 == 0 && Sensor4 == 0)
    {
        Particle.publish("Object detected on Sensor 3 and 4" , PUBLIC);
    }
    
    //<<<<<<<<<<<<<<<<<<<<<Object Detected on Single Sensor>>>>>>>>>>>>>>>>>>>>>>>
    else if (Sensor1 == 0 && Sensor2 == 1 && Sensor3 == 1 && Sensor4 == 1)
        {
            Particle.publish("Object detected on sensor 1", PUBLIC);
        }
        
    else if (Sensor1 == 1 && Sensor2 == 0 && Sensor3 == 1 && Sensor4 == 1)
        {
            Particle.publish("Object detected on sensor 2", PUBLIC);
        }
        
    else if (Sensor1 == 1 && Sensor2 == 1 && Sensor3 == 0 && Sensor4 == 1)
        {
            Particle.publish("Object detected on sensor 3", PUBLIC);
        }
        
    else if (Sensor1 == 1 && Sensor2 == 1 && Sensor3 == 1 && Sensor4 == 4)
        {
            Particle.publish("Object detected on sensor 4", PUBLIC);
        }
}


void SensorFinder()
{
    if (Sensor1 == 0)
    {
        digitalWrite(IN2, LOW);
        digitalWrite(IN1, HIGH);
        
        analogWrite(SPEEDPINA, 150);
        
        digitalWrite(IN4, LOW);
        digitalWrite(IN3, HIGH);
        
        analogWrite(SPEEDPINB, 150);
    }
    
    //Nothing dont go
    if (Sensor1 == 1)
    {
        digitalWrite(IN2, LOW);
        digitalWrite(IN1, LOW);
        
        digitalWrite(IN4, LOW);
        digitalWrite(IN3, LOW);
    }
    
    //Object go
    //Sensor 2
    if (Sensor2 == 0)
    {
        digitalWrite(IN2, LOW);
        digitalWrite(IN1, HIGH);
        
        analogWrite(SPEEDPINA, 150);
        
        digitalWrite(IN4, LOW);
        digitalWrite(IN3, HIGH);
        
        analogWrite(SPEEDPINB, 150);
    }
    
    //Nothing dont go
    if (Sensor2 == 1)
    {
        digitalWrite(IN2, LOW);
        digitalWrite(IN1, LOW);
        
        digitalWrite(IN4, LOW);
        digitalWrite(IN3, LOW);
    }
    
    
    //Sensor3
    if (Sensor3 == 0)
    {
        digitalWrite(IN2, LOW);
        digitalWrite(IN1, HIGH);
        
        analogWrite(SPEEDPINA, 150);
        
        digitalWrite(IN4, LOW);
        digitalWrite(IN3, HIGH);
        
        analogWrite(SPEEDPINB, 150);
    }
    
    //Nothing dont go
    if (Sensor3 == 1)
    {
        digitalWrite(IN2, LOW);
        digitalWrite(IN1, LOW);
        
        digitalWrite(IN4, LOW);
        digitalWrite(IN3, LOW);
    }
    
    //Sensor 4
    if (Sensor4 == 0)
    {
        digitalWrite(IN2, LOW);
        digitalWrite(IN1, HIGH);
        
        analogWrite(SPEEDPINA, 150);
        
        digitalWrite(IN4, LOW);
        digitalWrite(IN3, HIGH);
        
        analogWrite(SPEEDPINB, 150);
    }
    
    //Nothing dont go
    if (Sensor4 == 1)
    {
        digitalWrite(IN2, LOW);
        digitalWrite(IN1, LOW);
        
        digitalWrite(IN4, LOW);
        digitalWrite(IN3, LOW);
    }
}
*/

//Device ID - 270035000147373336323230
//Accesss Token - bee763562c709a8cec07705008beb1e4f8372e38

Credits

Goody599

Goody599

2 projects • 1 follower

Comments

Add projectSign up / Login