#define STEP_PIN 3 // Pin connected to A4988 STEP
#define DIR_PIN 9 // Pin connected to A4988 DIR
#define ENABLE_PIN 8 // Pin connected to A4988 ENABLE
#define VMOT_PIN 5 // Pin connected to A4988 VMOT (simulated in Wokwi)
#define STEP_PIN 3 // Pin connected to A4988 STEP
#define DIR_PIN 9 // Pin connected to A4988 DIR
#define ENABLE_PIN 8 // Pin connected to A4988 ENABLE
#define VMOT_PIN 5 // Pin connected to A4988 VMOT (simulated in Wokwi)
void setup() {
Serial.begin(9600); // Start serial communication
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(ENABLE_PIN, OUTPUT);
digitalWrite(ENABLE_PIN, LOW); // Enable the A4988 (active low)
digitalWrite(VMOT_PIN, HIGH); // Simulate powering the motor (This is a Wokwi-specific simulation)
Serial.begin(9600); // Start serial communication
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(ENABLE_PIN, OUTPUT);
digitalWrite(ENABLE_PIN, LOW); // Enable the A4988 (active low)
digitalWrite(VMOT_PIN, HIGH); // Simulate powering the motor (This is a Wokwi-specific simulation)
}
void loop() {
if (Serial.available() > 0) {
float temperature = Serial.parseFloat(); // Read the temperature value from Arduino 1
// Map the temperature to motor speed (0 to 255)
int motorSpeed = map(temperature, 0, 40, 0, 255); // Adjust temperature range as needed
// Set the motor speed and direction based on temperature
int stepDelay = map(motorSpeed, 0, 255, 1000, 100); // Speed control
// Control motor direction (you can adjust the threshold for temperature)
if (temperature > 25) {
digitalWrite(DIR_PIN, HIGH); // Forward direction if temperature is high
} else {
digitalWrite(DIR_PIN, LOW); // Reverse direction if temperature is low
}
// Generate step pulses to control motor speed
for (int i = 0; i < motorSpeed; i++) {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(stepDelay);
}
delay(1000); // Delay to avoid excessive updates
}
if (Serial.available() > 0) {
float temperature = Serial.parseFloat(); // Read the temperature value from Arduino 1
// Map the temperature to motor speed (0 to 255)
int motorSpeed = map(temperature, 0, 40, 0, 255); // Adjust temperature range as needed
// Set the motor speed and direction based on temperature
int stepDelay = map(motorSpeed, 0, 255, 1000, 100); // Speed control
// Control motor direction (you can adjust the threshold for temperature)
if (temperature > 25) {
digitalWrite(DIR_PIN, HIGH); // Forward direction if temperature is high
} else {
digitalWrite(DIR_PIN, LOW); // Reverse direction if temperature is low
}
// Generate step pulses to control motor speed
for (int i = 0; i < motorSpeed; i++) {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(stepDelay);
}
delay(1000); // Delay to avoid excessive updates
}
}