I am trying to get a fan to blow 3 seconds then stop for 27 minutes. My connections and code may be all wrong. Here is the schematic but I have substituted an AI Thinker ESP32 Cam. I have replaced the Uno in this image with my camera.
https://github.com/espressif/arduino-esp32
Please be sure to select the right ESP32 module
in the Tools -> Board menu!
Change WiFi ssid, pass, and Blynk auth token to run :)
Feel free to apply it to any other example. It's simple!
*************************************************************/
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
/* Fill-in your Template ID (only if using Blynk.Cloud) */
//Device 10
#define BLYNK_TEMPLATE_ID ""
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
const int RELAY_PIN = A5; // the Arduino pin, which connects to the IN pin of relay
int speedPin=5;
int dir1=4;
int dir2=3;
int mSpeed=255;
// the setup function runs once when you press reset or power the board
void setup()
{
// Debug console
// initialize digital pin A5 as an output.
pinMode(RELAY_PIN, OUTPUT);
pinMode (speedPin, OUTPUT);
pinMode (dir1, OUTPUT);
pinMode (dir2, OUTPUT);
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
}
// the loop function runs over and over again forever
void loop()
{
Blynk.run();
digitalWrite(RELAY_PIN, HIGH); // turn on fan 3 seconds
delay(3000);
digitalWrite (RELAY_PIN, LOW); // turn off fan 1797 seconds
delay(1797000);
digitalWrite (dir1, HIGH);
digitalWrite (dir2, LOW);
analogWrite (speedPin, mSpeed);```
Here is a drawing of the device, with explanation:
[Schematic_Bug_Cam_2022-05-21.pdf|attachment](upload://nTl8PkClp7vuNjzRiUBE9VhWov0.pdf) (32.9 KB)
Right now, the fan will not spin. I would like to use millis so that the fan can operate without disrupting other portions of the program.
Can you help a newbie reduce the learning curve?
If you want your program to be responsive, you need to get rid of those delay() calls and just track elapsed time since the start of an event. Look at the Blink Without Delay example in the IDE (File->examples->02.digital->Blink Without Delay).
You will also need to keep track of what state the program is in (fan_on/fan_off) so you know what to do when the desired time has elapsed.
int speedPin=5;
int dir1=4;
int dir2=3;
int mSpeed=255;
void setup() {
// put your setup code here, to run once:
pinMode (speedPin, OUTPUT);
pinMode (dir1, OUTPUT);
pinMode (dir2, OUTPUT);
Serial.begin (115200);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite (dir1, HIGH);
digitalWrite (dir2, LOW);
analogWrite (speedPin, mSpeed);
}```
In that case your schematic is not useful. Uno and ESP32 have different pins and voltages. We will not be able to spot any error you have made with that schematic.
Please post a schematic showing the components you are using, how you have wired them together, what power supplies you are using. Hand drawn schematic of your circuit is better than a professionally produced schematic of a different circuit.
Yes, the original fan code made the fan spin with the Uno. I did not want to use Uno unless I can get it to be a slave to my ESP32 cam or vice versa. I explored how to do that, but it felt I was going down a rabbit hole.
The schematic and the code for the fan alone were prepared for me by a friend of one of my former employees. I would have preferred that he use the ESP32 cam but he was doing me a favor.