starting point of the connected devices
Made by Ziru Wei and Leslie Liu
Made by Ziru Wei and Leslie Liu
make 2 devices response to each other
Created: December 9th, 2024
This is the start point of building connected paired device.
bool doBlink = false;
// We will be using D2 to control our LED
int ledPin = D2;
// Our button wired to D0
int buttonPin = D3;
void setup()
{
// For input, we define the
// pushbutton as an input-pullup
// this uses an internal pullup resistor
// to manage consistent reads from the device
pinMode( buttonPin , INPUT_PULLUP); // sets pin as input
// We also want to use the LED
pinMode( ledPin , OUTPUT ); // sets pin as output
// blink the LED when the setup is complete
blinkLED( 3, ledPin );
Particle.subscribe( "blinkLED", handleActivateLED );
}
void loop()
{
// find out if the button is pushed
// or not by reading from it.
int buttonState = digitalRead( buttonPin );
// remember that we have wired the pushbutton to
// ground and are using a pulldown resistor
// that means, when the button is pushed,
// we will get a LOW signal
// when the button is not pushed we'll get a HIGH
// let's use that to set our LED on or off
if( buttonState == LOW )
{
// turn the LED On
digitalWrite( ledPin, HIGH);
Particle.publish( "doPairedPublish" );
}else{
// otherwise
// turn the LED Off
digitalWrite( ledPin, LOW);
}
delay( 1000 );
if( doBlink == true ){
blinkLED( 6, ledPin );
doBlink = false;
}
}
void blinkLED( int times, int pin ){
for( int i = 0; i < times ; i++ ){
digitalWrite( pin, HIGH );
delay( 500 );
digitalWrite( pin, LOW );
delay( 500 );
}
}
void handleActivateLED( const char *event, const char *data)
{
doBlink = true;
}
Click to Expand
int sensorPin = D3;
int ledPin = D2;
void setup() {
pinMode(ledPin, OUTPUT);
Particle.subscribe("blinkLED", handleActivateLED);
blinkLED(3, ledPin);
}
unsigned long readQTR() {
unsigned long duration = 0;
pinMode(sensorPin, OUTPUT);
digitalWrite(sensorPin, HIGH);
delayMicroseconds(10);
pinMode(sensorPin, INPUT);
while(digitalRead(sensorPin) == HIGH) {
duration++;
if(duration > 3000) break;
}
return duration;
}
void loop() {
unsigned long sensorValue = readQTR();
if(sensorValue < 3000) {
digitalWrite(ledPin, HIGH);
Particle.publish("doPairedPublish");
} else {
digitalWrite(ledPin, LOW);
}
delay(100);
if(doBlink) {
blinkLED(6, ledPin);
doBlink = false;
}
}
void blinkLED( int times, int pin ){
for( int i = 0; i < times ; i++ ){
digitalWrite( pin, HIGH );
delay( 500 );
digitalWrite( pin, LOW );
delay( 500 );
}
}
void handleActivateLED( const char *event, const char *data)
{
doBlink = true;
}
Click to Expand
bool doBlink = false;
int ledPin = D2;
int sensorPin = D3;
int buttonPin = D4; // Button pin for manual retraction
bool isRetracting = false; // Track retraction state
// L298N control pins
int enPin = A2; // Enable pin for speed control
int in1 = D6; // Direction control 1
int in2 = D7; // Direction control 2
bool isBlinking = false;
int blinkCount = 0;
unsigned long lastBlinkTime = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(enPin, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(enPin, HIGH);
blinkLED(3, ledPin);
Particle.subscribe("blinkLED", handleActivateLED);
}
unsigned long readQTR() {
unsigned long duration = 0;
pinMode(sensorPin, OUTPUT);
digitalWrite(sensorPin, HIGH);
delayMicroseconds(10);
pinMode(sensorPin, INPUT);
while(digitalRead(sensorPin) == HIGH && duration < 3000) {
duration++;
}
return duration;
}
void actuatorExtend() {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
}
void actuatorRetract() {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
}
void actuatorStop() {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
analogWrite(enPin, 0);
}
void loop() {
unsigned long currentMillis = millis();
// Handle sensor and actuator control first
if (!isRetracting) {
unsigned long sensorValue = 0;
for(int i = 0; i < 5; i++) {
sensorValue += readQTR();
delay(1);
}
sensorValue /= 5;
if(sensorValue < 3000) {
actuatorExtend();
if (!isBlinking) digitalWrite(ledPin, HIGH);
Particle.publish("doPairedPublish");
// Remove delay here - it's interrupting actuator operation
} else {
if (!isBlinking) digitalWrite(ledPin, LOW);
actuatorStop();
}
}
// Handle button control
if (digitalRead(buttonPin) == LOW) {
isRetracting = true;
actuatorRetract();
if (!isBlinking) digitalWrite(ledPin, HIGH);
} else if (isRetracting) {
isRetracting = false;
actuatorStop();
if (!isBlinking) digitalWrite(ledPin, LOW);
}
// Handle LED blinking last
if (doBlink && !isBlinking) {
isBlinking = true;
blinkCount = 0;
lastBlinkTime = currentMillis;
}
if (isBlinking) {
if (currentMillis - lastBlinkTime >= 500) {
if (blinkCount >= 12) {
isBlinking = false;
doBlink = false;
digitalWrite(ledPin, LOW);
} else {
digitalWrite(ledPin, !digitalRead(ledPin));
lastBlinkTime = currentMillis;
blinkCount++;
}
}
}
delay(10);
}
void blinkLED(int times, int pin) {
for(int i = 0; i < times; i++) {
digitalWrite(pin, HIGH);
delay(500);
digitalWrite(pin, LOW);
delay(500);
}
}
void handleActivateLED(const char *event, const char *data) {
doBlink = true;
}
Click to Expand
make 2 devices response to each other