Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************************************************
- * GROUND code
- * looks for substancial change from sensors
- *
- * Publishes: ping
- * Subscribes: NA
- *
- * Setters:
- * - setBrightness(String val)
- * - setSpeed(String val)
- * Convention for setters, return 1 if successful, -1 otherwise
- *
- * Published Variables:
- * - int lastPing // i think its better deleting? check possible functionality
- *
- * Extension:
- * - wifi status
- * - runtime and battery status
- * Built for Photon, no dependecies
- *
- *******************************************************************************/
- /*
- * Every refresh the arduino looks for substancial
- * change in any pin since last
- * Should investigate sleep function to save photon battery
- * https://github.com/spark/firmware/issues/655
- *
- * You get also a battery status report and last time since ping
- * http://community.particle.io/t/name-resolving-of-tcpclient-when-connecting-to-other-photon-running-mdns/24843/2
- * http://community.particle.io/t/sample-code-for-connecting-2-photons-wifi-direct/21386/7
- * https://community.particle.io/t/real-time-communication-between-two-photons-on-internet/15053/5
- */
- #define LED_PIN D7 // onboard LED pin
- #define WAIT 1000
- #define WAIT2 500
- int pins[] = {A0, A1, A2, A3};
- int numInput = sizeof(pins) - 1;// sizeof(myPins) / sizeof(myPins[0]); // check this
- int lastSum = -1;
- int thresh = 1000; // amount of difference to tolerate;
- bool ignore = false; // ignore one cycle
- SYSTEM_MODE(AUTOMATIC);
- STARTUP(WiFi.selectAntenna(ANT_AUTO)); // continually switches at high speed between antennas
- void setup() {
- /* Initialize analog ins */
- for (int i = 0; i < numInput; i++) {
- pinMode(pins[i], INPUT);
- }
- /* Initialize led pin */
- pinMode(LED_PIN, OUTPUT);
- pinResetFast(LED_PIN);
- /* Turn off on board LED */
- RGB.control(true);
- RGB.color(0, 0, 0);
- Particle.variable("LDRsum", &lastSum, INT);
- // https://api.particle.io/v1/devices/1b0022001947353236343033/LDRsum?access_token=e9ff4e5ec3b85e66f7e50e1c0e2fa606d0722c02
- Particle.function("setThresh", newThresh); // sets a new threshold
- }
- void loop() {
- int sum = 0;
- if (!ignore) {
- for (int i = 0; i < 6; i++){
- int myVal = analogRead(pins[i]);
- sum += myVal;
- }
- int diff = abs(lastSum - sum);
- if (diff > thresh) {
- char publishString[4];
- sprintf(publishString,"%u", diff);
- // how do i make it private
- Particle.publish( "ping", publishString, 60, PRIVATE ); // event name, data, TTL, status
- ignore = true;
- }
- lastSum = sum;
- delay(WAIT2);
- }
- ignore = false;
- delay(WAIT);
- }
- int newThresh(String command) {
- /* Particle.functions always take a string as an argument and return an integer. */
- // sets new threshold value
- thresh = command.toInt();
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement