Back to Parent

#include "Particle.h"
#include "neopixel.h"

#define TOUCH_SENSOR_1_PIN D3
#define TOUCH_SENSOR_2_PIN D2
#define PIXEL_PIN SPI
#define PIXEL_COUNT 8
#define PIXEL_TYPE WS2812

bool wasTouched1 = false;
bool wasTouched2 = false;

Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

void setup() {
    pinMode(TOUCH_SENSOR_1_PIN, INPUT);
    pinMode(TOUCH_SENSOR_2_PIN, INPUT);
    Serial.begin(9600);
    Particle.subscribe("toggle_led_1", eventHandler);
    Particle.subscribe("toggle_led_3", eventHandler);
    
    strip.begin();
    strip.show();
}

void eventHandler(const char *event, const char *data) {
    Serial.printlnf("Received event: %s, data: %s", event, data);

    if (strcmp(data, "on") == 0) {
        if (strcmp(event, "toggle_led_1") == 0) {
            setStripColor(255, 165, 0); // warm orange yellow for toggle_led_1
        } else if (strcmp(event, "toggle_led_3") == 0) {
            setStripColor(0, 0, 204); // deep Blue for toggle_led_3
        }
    }
}

void setStripColor(int r, int g, int b) {
    for(int i=0; i<PIXEL_COUNT; i++) {
        strip.setPixelColor(i, strip.Color(r, g, b));
    }
    strip.show();
    delay(1000);
    strip.clear();
    strip.show();
}

void loop() {
    int sensorValue1 = digitalRead(TOUCH_SENSOR_1_PIN);
    int sensorValue2 = digitalRead(TOUCH_SENSOR_2_PIN);

    if (sensorValue1 == HIGH && !wasTouched1) {
        wasTouched1 = true;
        Serial.println("Touch 1 detected! Publishing event.");
        Particle.publish("toggle_led_2", "on", PRIVATE);
    } else if (sensorValue1 == LOW && wasTouched1) {
        wasTouched1 = false;
        Serial.println("Touch 1 released.");
    }

    if (sensorValue2 == HIGH && !wasTouched2) {
        wasTouched2 = true;
        Serial.println("Touch 2 detected! Publishing event.");
        Particle.publish("toggle_led_4", "on", PRIVATE);
    } else if (sensorValue2 == LOW && wasTouched2) {
        wasTouched2 = false;
        Serial.println("Touch 2 released.");
    }

    delay(50);
}
Click to Expand

Content Rating

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

0