Hello everyone
The code I wrote is not working. I wonder where the problem is. I am explaining the working logic of the codes below. I would be happy if you show it by drawing the diagram at the end. I made a mistake somewhere and couldn't solve it, I burned the uno.
- When the button is pressed for 1 second, the motor will start to turn left. When the stop button is pressed, the motor will stop and its energy will be cut off and it will wait until it receives a signal from the reed switch. If it receives a signal from the reed switch, it will start to turn in the opposite direction. When the stop button is pressed, the motor will stop and its energy will be cut off.
- When the button is pressed for 1 second, the motor will start to turn left. When the stop button is pressed, the motor will stop and its energy will be cut off. However, if it continues to receive a signal from the reed relay, it will start to turn in the opposite direction after 10 seconds. When the stop button is pressed, the motor will stop and its energy will be cut off.
- When the button is pressed for 1 second, the motor will start to turn left. When the stop button is pressed, the motor will stop and its energy will be cut off. However, when the motor receives a signal from the reed relay, if it encounters any strain while turning and stops, it will turn back again. When the stop button is pressed, the motor will stop and its energy will be cut off. It will wait 10 seconds and start to turn in the opposite direction. This process will continue until the stop button is pressed.
- Buzzer will be used in the first three items. Long beep when the button is pressed, intermittent beep when it starts to rotate when it receives a signal from the reed switch.
- There will be no energy on the motor when the stop button is pressed.
- Potentiometer will be used to adjust the motor speed.
- Green LED will be on when turning right, red LED will be on when turning left.
A1 pin for left rotation
A2 pin for stop
A3 pin for right rotation
A0 and 5v pin for potentiometer
Direction of stepper motor driver 8 pin
Step of stepper motor driver 9 pin
Enabled of stepper motor driver 10 pin
11 pin for green LED
12 pin for red LED
kodu buraya yazın veya yapıştırın
// Pin Definitions
#define DIR_PIN 8
#define STEP_PIN 9
#define ENABLE_PIN 10
#define POT_PIN A0
#define BUZZER_PIN 4
#define START_BUTTON_PIN 2
#define STOP_BUTTON_PIN 3
#define LIMIT_SWITCH_LEFT A1
#define LIMIT_SWITCH_RIGHT A3
#define REED_SWITCH A2
#define RED_LED 11
#define GREEN_LED 12
// Variables
int motorSpeed = 500; // Connected to potentiometer
bool isMoving = false;
void setup() {
// Pin modları
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(ENABLE_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(START_BUTTON_PIN, INPUT_PULLUP);
pinMode(STOP_BUTTON_PIN, INPUT_PULLUP);
pinMode(LIMIT_SWITCH_LEFT, INPUT_PULLUP);
pinMode(LIMIT_SWITCH_RIGHT, INPUT_PULLUP);
pinMode(REED_SWITCH, INPUT_PULLUP);
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
digitalWrite(ENABLE_PIN, HIGH); // Engine disabled
}
void loop() {
// Start button control
if (digitalRead(START_BUTTON_PIN) == LOW) {
delay(1000); // 1 second press time
motorMove(true); // Left turn
digitalWrite(RED_LED, HIGH); // The red LED lights up
waitForStopOrSwitch(LIMIT_SWITCH_LEFT); //Limit switch or stop button is expected
digitalWrite(RED_LED, LOW); // LED turns off
if (digitalRead(REED_SWITCH) == LOW) {
delay(10); // If reed switch signal is detected
motorMove(false); // Right turn
digitalWrite(GREEN_LED, HIGH); // Green LED lights up
waitForStopOrSwitch(LIMIT_SWITCH_RIGHT); // Limit switch or stop button is expected
digitalWrite(GREEN_LED, LOW); //LED turns off
}
}
}
// Motor motion function
void motorMove(bool direction) {
digitalWrite(DIR_PIN, direction);
digitalWrite(ENABLE_PIN, LOW); // Engine active
tone(BUZZER_PIN, 1000); // Long beep
while (true) {
//Steps are taken
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(motorSpeed);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(motorSpeed);
// Stop button control
if (digitalRead(STOP_BUTTON_PIN) == LOW) {
stopMotor();
return;
}
}
}
// Stop engine function
void stopMotor() {
digitalWrite(ENABLE_PIN, HIGH); // Disable engine
noTone(BUZZER_PIN); // Stop the buzzer
}
// Limit switch or stop control function
void waitForStopOrSwitch(int switchPin) {
while (digitalRead(switchPin) == HIGH) {
// Stop butonu kontrolü
if (digitalRead(STOP_BUTTON_PIN) == LOW) {
stopMotor();
return;
}
//Potentiometer reading
motorSpeed = map(analogRead(POT_PIN), 0, 1023, 200, 1000);
}
stopMotor();
}