Code for Testing Fan without Webhook Data
int fanPin = D2;
void setup() {
// Register the fan control function with Particle
Particle.function("fanControl", fanControl);
// Initialize the fan pin
pinMode(fanPin, OUTPUT);
digitalWrite(fanPin, LOW);
}
void loop() {
// The loop remains empty as the fan control is handled by the Particle function
}
// Fan control function with time and cycle count
int fanControl(String command) {
// Example command format: "10,3" for 10 seconds and 3 cycles
int commaIndex = command.indexOf(',');
if (commaIndex == -1) {
return -1; // Error in command format
}
// Extract time and cycle count from command
int time = command.substring(0, commaIndex).toInt();
int cycles = command.substring(commaIndex + 1).toInt();
if (time > 0 && cycles > 0) {
for (int i = 0; i < cycles; i++) {
// Turn the fan on
analogWrite(fanPin, 255);
// Keep the fan on for the specified time
delay(time * 1000);
// Turn the fan off
analogWrite(fanPin, 0);
// Wait before the next cycle
delay((6 - time)*1000);
}
} else {
return -1; // Invalid time or cycle count
}
return 1; // Return a success status
}
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. .