Back to Parent

#define innerLED D7
#define VibAPIN D2
#define VibBPIN D3
#define VibCPIN D4
#define TouchControlPIN D6

const String prefixEventName = "DIOT-2018-DARA-TEAM-HOME-WRECKERS";
const String nameMiniGroup = "MOBILE";
const String eventMobileA = prefixEventName + "-" + nameMiniGroup + "-MOBILEA";
const String eventMobileB = prefixEventName + "-" + nameMiniGroup + "-MOBILEB";
const String eventMobileC = prefixEventName + "-" + nameMiniGroup + "-MOBILEC";
const String eventReset =prefixEventName+"-RESET";
const String eventPlay = prefixEventName + "-PLAY";

int EventInterval = 5; //unit second;
int EventBlock = 2;

unsigned long timeLastEventAny = 0;

unsigned long timeLastEventA = 0;
unsigned long timeLastEventB = 0;
unsigned long timeLastEventC = 0;

int amountMobileA =0;
int amountMobileB =0;
int amountMobileC =0;

int resetTouchCap(String cmd);

void eventHandlerReset(const char *event, const char *data)
{
    String sdata = String(data);
 amountMobileA =0;
 amountMobileB =0;
 amountMobileC =0;
}

int PushEventA() {
    if (timeLastEventA < 0) {
        Particle.publish("LOG_TIMELASTNOTEXPECTED","");
        return -1;    //time stamp not initialized or unkown error
        
    }
    
    int currentTime = Time.now();
    
    if (abs(currentTime - timeLastEventAny) < EventBlock) {
        return 0;
    }
    
    if (abs(currentTime - timeLastEventA) > EventInterval) {
        amountMobileA += 1;
        Particle.publish(eventMobileA,String(amountMobileA));
        Particle.publish(eventPlay,"MA");
        timeLastEventA = currentTime;
        timeLastEventAny = currentTime;
        return 1;   // published
    } else {
        timeLastEventAny = currentTime;
        Particle.publish("LOG_TOOFREQUENT","");
        return 0; 
    }
    
    return -2; // unknown error
}

int PushEventB() {
    if (timeLastEventB < 0) {
        return -1;    //time stamp not initialized or unkown error
    }
    
    int currentTime = Time.now();
    
    if (abs(currentTime - timeLastEventAny) < EventBlock) {
        return 0;
    }
    
    if (abs(currentTime - timeLastEventB) > EventInterval) {
        amountMobileB += 1;
        Particle.publish(eventMobileB,String(amountMobileB));
        Particle.publish(eventPlay,"MB");
        timeLastEventAny = currentTime;
        return 1;   // published
    } else {
        timeLastEventAny = currentTime;
        return 0; 
    }
    
    return -2; // unknown error
}

int PushEventC() {
    if (timeLastEventC < 0) {
        return -1;    //time stamp not initialized or unkown error
    }
    
    int currentTime = Time.now();
    
    if (abs(currentTime - timeLastEventAny) < EventBlock) {
        return 0;
    }
    
    if (abs(currentTime - timeLastEventC) > EventInterval) {
        amountMobileC += 1;
        Particle.publish(eventMobileC,String(amountMobileC));
        Particle.publish(eventPlay,"MC");
        timeLastEventAny = currentTime;
        return 1;   // published
    } else {
        timeLastEventAny = currentTime;
        return 0; 
    }
    
    return -2; // unknown error
}


int resetTouchCap(String cmd){


digitalWrite(TouchControlPIN, LOW);
if (cmd.toInt() > 0) {
    delay(cmd.toInt());
} else {
    delay(500);
}
digitalWrite(TouchControlPIN, HIGH);
return 1;
    
}
void setup() {

pinMode(VibAPIN, INPUT);
pinMode(VibBPIN, INPUT);
pinMode(VibCPIN, INPUT);

pinMode(TouchControlPIN, OUTPUT);

pinMode(innerLED,OUTPUT);

Particle.subscribe(eventReset, eventHandlerReset);

digitalWrite(TouchControlPIN, HIGH);  /////

Particle.function("resetTouch",resetTouchCap);

}

bool ReadVib( int PIN){
    if (digitalRead(PIN) == HIGH){
//        Particle.publish("LOG_VIBSENSOR",String(PIN));
        digitalWrite(innerLED,HIGH);
        return TRUE;
    } else {
        digitalWrite(innerLED,LOW);
        return FALSE;
    }
}

void loop() {

    if (ReadVib(VibAPIN)) {
        PushEventA();
    }
    
  ///////////////////////////disable moblie B and C for debuging
   if (ReadVib(VibBPIN)) {
        PushEventB();
    }
    
    if (ReadVib(VibCPIN)) {
        PushEventC();
    }

    
    delay(100);

}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0