String msg_name = "heavyWithTheHeat";
long lastPublishedAt = 0;
int publishAfter = 10000;
int LED_0 = D0;
int LED_1 = D1;
int LED_2 = D2;
int LED_3 = D3;
void setup() {
Particle.subscribe( msg_name, LEDController);
pinMode(LED_0, OUTPUT);
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(LED_3, OUTPUT);
}
void loop()
{
Particle.publish("bad", "loop start");
String new_msg_name = msg_name+System.deviceID();
if( triCheck(0) ){
Particle.publish(new_msg_name, "A");
}
if( triCheck(1) ){
Particle.publish(new_msg_name, "B");
}
if( triCheck(2)){
Particle.publish(new_msg_name, "C");
}
if( triCheck(3) ){
Particle.publish(new_msg_name, "D");
}
delay(1000);
}
bool triCheck( int pin )
{
int try1 = analogRead(pin);
delay(50);
int try2 = analogRead(pin);
delay(50);
int try3 = analogRead(pin);
int goalValue = 4094;
if( isBetween( try1, goalValue, 10) and isBetween( try1, goalValue, 10) and isBetween( try1, goalValue, 10))
{
return true;
}
else
{
return false;
}
}
bool isBetween(int num, int approx, int error)
{
if ( num < approx - error || num > approx + error ){
return false;
}
else{
return true;
}
}
void LEDController(const char *event, const char *data)
{
String eventName = String(event);
String deviceID = System.deviceID();
if( eventName.indexOf( deviceID ) != -1 ){
Particle.publish("same device?", "yes");
return;
}
if( strcmp( data, "A")==0)
{
Particle.publish("tester", "A");
digitalWrite(LED_0, HIGH);
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, LOW);
digitalWrite(LED_3, LOW);
}
else if( strcmp( data, "B")==0)
{
Particle.publish("tester", "B");
digitalWrite(LED_0, HIGH);
digitalWrite(LED_1, HIGH);
digitalWrite(LED_2, LOW);
digitalWrite(LED_3, LOW);
}
else if( strcmp( data, "C")==0)
{
Particle.publish("tester", "C");
digitalWrite(LED_0, HIGH);
digitalWrite(LED_1, HIGH);
digitalWrite(LED_2, HIGH);
digitalWrite(LED_3, LOW);
}
else if( strcmp( data, "D")==0)
{
Particle.publish("tester", "D");
digitalWrite(LED_0, HIGH);
digitalWrite(LED_1, HIGH);
digitalWrite(LED_2, HIGH);
digitalWrite(LED_3, HIGH);
}
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .