// Pin definitions for Motor 1
#define STEP_PIN_1 4
#define DIR_PIN_1 3
#define ENABLE_PIN_1 5
// Pin definitions for Motor 2
#define STEP_PIN_2 8
#define DIR_PIN_2 7
#define ENABLE_PIN_2 9
// Pin definitions for FAN
#define FAN_PIN 10
// Pin definitions for LED
#define LED_PIN 6
// Variables
unsigned long previousMicros1 = 0; // Store the time of the last step update for Motor 1
unsigned long previousMicros2 = 0; // Store the time of the last step update for Motor 2
int speed1 = 4000; // Time interval between steps for Motor 1 (in microseconds)
int speed2 = 4000; // Time interval between steps for Motor 2 (in microseconds)
bool stepState1 = false; // Step pin state for Motor 1 (HIGH or LOW)
bool stepState2 = false; // Step pin state for Motor 2 (HIGH or LOW)
bool enable1 = true;
bool enable2 = true;
int command = 0;
int fanMode = 1;
int fan = 0;
int ledMode = 1;
int led = 0;
void setup() {
Serial.begin(9600);
// Set up the pins for Motor 1
pinMode(STEP_PIN_1, OUTPUT);
pinMode(DIR_PIN_1, OUTPUT);
pinMode(ENABLE_PIN_1, OUTPUT);
digitalWrite(ENABLE_PIN_1, HIGH); // Enable the driver for Motor 1
// Set up the pins for Motor 2
pinMode(STEP_PIN_2, OUTPUT);
pinMode(DIR_PIN_2, OUTPUT);
pinMode(ENABLE_PIN_2, OUTPUT);
digitalWrite(ENABLE_PIN_2, HIGH); // Enable the driver for Motor 2
// Set the initial direction for both motors
digitalWrite(DIR_PIN_1, HIGH);
digitalWrite(DIR_PIN_2, HIGH);
// Set up for FAN
pinMode(FAN_PIN, OUTPUT);
digitalWrite(FAN_PIN, LOW);
// Set up for LED
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
}
void loop() {
unsigned long currentMicros = micros();
// Controls Motor 1
if (currentMicros - previousMicros1 >= speed1) {
previousMicros1 = currentMicros;
// Toggles the step pin state
stepState1 = !stepState1;
digitalWrite(STEP_PIN_1, stepState1);
}
// Controls Motor 2
if (currentMicros - previousMicros2 >= speed2) {
previousMicros2 = currentMicros;
// Toggles the step pin state
stepState2 = !stepState2;
digitalWrite(STEP_PIN_2, stepState2);
}
// Reed Serial commands
if (Serial.available() > 0) {
//char command = Serial.read();
// Save on variables Serial commands
command = Serial.parseInt();
speed1 = Serial.parseInt();
speed2 = Serial.parseInt();
fan = Serial.parseInt();
led = Serial.parseInt();
//ledMode = Serial.parseInt();
//int x = Serial.parseInt();
Serial.println(command);
Serial.println(speed1);
Serial.println(speed2);
Serial.println(fanMode);
Serial.println(ledMode);
// Call functions
Motors(command);
fanfuncion(fan);
ledfuncion(led);
}
}
void Motors(int command) {
switch (command) {
case 7:
// Move Motor 1 backwards
digitalWrite(DIR_PIN_1, LOW);
//Serial.println("Q");
break;
case 2:
// Move Motor 1 forwards
digitalWrite(DIR_PIN_1, HIGH);
Serial.println("W");
break;
case 3:
// Move Motor 2 backwards
digitalWrite(DIR_PIN_2, LOW);
Serial.println("A");
break;
case 4:
// Move Motor 2 forwards
digitalWrite(DIR_PIN_2, HIGH);
Serial.println("S");
break;
case 6:
// Toggle Motor 2 RUN/STOP
enable2=!enable2;
digitalWrite(ENABLE_PIN_2, enable2);
Serial.print("MOTOR 2 =");
Serial.println(enable1);
break;
case 5:
// Toggle Motor 1 RUN/STOP
enable1=!enable1;
digitalWrite(ENABLE_PIN_1, enable1);
Serial.print("MOTOR 1 =");
Serial.println(enable1);
break;
// Case 8 is for not changing things.
case 8:
break;
default:
break;
}
}
// Toggle FAN
void fanfuncion(int fan){
if(fan == 1){
digitalWrite(FAN_PIN, fanMode);
fanMode=!fanMode;
}
}
// Toggle LED
void ledfuncion(int led){
if(led == 1){
digitalWrite(LED_PIN, ledMode);
ledMode=!ledMode;
}
}