/**
ESP32 + DHT22 Example for Wokwi
https://wokwi.com/arduino/projects/322410731508073042
*/
/*
#include "DHTesp.h"
const int DHT_PIN = 15;
DHTesp dhtSensor;
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
delay(1000);
}
*/
// Not sure whether you need this or not, but uncomment if required:
// #include <Arduino.h>
// Defines to make code more readable
#define ENABLE_PIN 18
#define STEP_PIN 5
#define DIR_PIN 4
#define Frequency 1000 // Frequency in Hertz
#define Period (int)(1. / ((float)Frequency) * 1000000.) // Wait Time in µs. Float conversion because integer div doesn't work that way
// Force one-time evaluation of the period to guarantee only one float operation
const int PERIOD = Period;
// Setup is run once at the start of the program
void setup()
{
pinMode(ENABLE_PIN, OUTPUT); //Enable
pinMode(STEP_PIN, OUTPUT); //Puls
pinMode(DIR_PIN, OUTPUT); //Direction
// Enable motor driver (Enable is negated)
digitalWrite(ENABLE_PIN, LOW);
}
// Runs repeatedly
void loop()
{
// Select direction as forwards
digitalWrite(DIR_PIN, HIGH);
// Perform 5000 steps
perform_steps(5000, PERIOD);
delay(10);
// Invert Direction
digitalWrite(DIR_PIN, LOW);
// Perform 5000 steps
perform_steps(5000, PERIOD);
delay(10);
}
// type uint32_t means 32 bit unsigned integer (max number is 2^32, which is around 4 billion)
void perform_steps(uint32_t steps, uint32_t delay_micros) {
for(uint32_t i = 0; i < steps; i++)
{
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(delay_micros / 2); // the delay here can be very low generally, as this is basically a pulse
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(delay_micros / 2); // dividing delay by 2.
// Note: this will lose accuracy for sufficiently small delay_micros, because
// 3 / 2 == 1, 5 / 2 == 2, ...
// Integer division always rounds down.
}
}