John R McAlpine V Mac
Published

A Totally Useless IoT Machine

A simple to implement totally useless IoT machine with a Rube Goldberg complex.

BeginnerFull instructions provided1 hour924
A Totally Useless IoT Machine

Things used in this project

Hardware components

Argon
Particle Argon
×2

Story

Read more

Code

Useless IOT App

Arduino
Copy/paste into the build.particle.io IDE
// A Totally Useless IOT Machine
// Mac McAlpine MEGR3171 Introduction to Instrumentation @ UNC CHARLOTTE
// 
//This machine detects a switch state.  If it detects an "on" state, it then sends its buddy argon a message to move a servo in an effort to turn itself back off.
//This machine is recursive in nature.  Any number of argons with switches and servos can be configured and all run the same code.
//This code does not differentiate what argon sent the message.  All servos on every argon will try to turn itself off.

int switchstate = 0; //a variable that will hold the current switch state
int lastswitchstate = 0; // a variable that will hold the previous switch state
Servo myservo;//servo position  180 is center.  sometimes servos won't move if you are close to 0 or 180

int switchpin = D1; //input switch is connected to D1 and the other terminal to ground.
int groundpin = D0; // aux ground out.
int servopin = A0; //the servo control pin is connected here.  Power from the servo can be had from the VUSB pin
int ledpin = D7;  // We utilize the onboard LED

void setup() 
{
Particle.subscribe("useless-on", uselessfunc, MY_DEVICES); //when useless-on occurs in the cloud (viewable by console.particle.io) run the uselessfunc
//MY_DEVICES limits the subscription such that only devices on your particle account will be listened to.
myservo.attach(servopin); //attach servo to A0
pinMode(switchpin, INPUT_PULLUP);  //connects this pin with a real resistor to 3.3V by software *configurable hardware*  Switch inputs will be detected by sensing a low here.
myservo.write(135);
pinMode(ledpin, OUTPUT);
pinMode(groundpin, OUTPUT);
digitalWrite(groundpin,LOW); //blink the led for visual feedback 


}



void loop() {  //normally this loop operates without delay.  It will loop at a very high frequency.
lastswitchstate = switchstate;  //copy the previous switch state to the last switch state
switchstate = digitalRead(D1);  //read a new switch state.
//begin switch input edge detection.  We want to trigger when the input goes from high to low.
if (switchstate == 0 && (lastswitchstate ==1)){ //if both are true then...  This is a bit more advanced than just detecting a input state. It detects an edge of a signal to prevent overloading the cloud.
    //"==" compares the two values.  If equal it takes a value of "true" or 1.
    Particle.publish("useless-on", PRIVATE);  //send the event to the particle cloud
    //delay(100);
    }

}

int uselessfunc(const char *topic, const char *data){  //this moves the servo to turn off the other switch
    myservo.write(25); //move servo to turn switch back off
    digitalWrite(ledpin,HIGH); //blink the led for visual feedback 
    delay(500); //give the servo a chance to move  Pause for 500ms
    myservo.write(135); //reset servo
    digitalWrite(ledpin,LOW); //turn the led off
    delay(500);
}

Credits

John R McAlpine V Mac
17 projects • 87 followers
www.MACSBOOST.com Assistant Teaching Professor at UNC Charlotte MEGR3171 Instrumentation, Motorsports Research
Contact

Comments

Please log in or sign up to comment.