Connection and code error

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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. There will be no energy on the motor when the stop button is pressed.
  6. Potentiometer will be used to adjust the motor speed.
  7. 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();
}

Welcome to the forum

You started a topic in the Uncategorised category of the forum when its description explicitly tells you not to

Your topic has been moved to a relevant category. Please be careful in future when deciding where to start new topics

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

You should show your wiring diagram. This will show how you interpreted the wiring diagram you used.

the code isawkward

  • it doesn't really wait for the start button to be pressed for 1 second, it simply waits one second for
  • motorMove() doesn't monitor the limit switches, instead relies on the stop button being pressed
  • would be better if the arg to motorMove() indicated the direction instead of being true/false as well as the aregument name indicating which direction (e.g. forward)
  • waitForStopOrSwitch() doesn't appear to the stepping the motor, seems to be driving a DC motor

confusing

  • not clear what type of motor is being used
  • nested and conditional checks for start/stop buttons or the reed switch make the code difficult to follow and debug if the input don't occur in the expected sequence

the code would be simpler if it were state driven that monitored events.

start button should enable a state that could repeatedly step a motor or without using states, simply enables a DC motor

either limit or the stop switch should immediately stop the motor by exiting a stepping state or disable a DC motor

if the start switch needs to be pressed for one second, a timestamp can be captured when initially seen LOW and the motor turned on after a timer expires. The timer should be reset if the switch is released before the timer expires

look this over

const byte PinMotorFor = 13;
const byte PinMotorRev = 12;
const byte PinStart    = A1;
const byte PinStopLmt  = A2;    // stop and 2 limit switches wire parallel

enum { Off = HIGH, On = LOW };

enum { Stop, Run };
int state = Stop;

bool dirFor = true;

// -----------------------------------------------------------------------------
// do whatever is needed to stop the motor
void motorStop ()
{
    digitalWrite (PinMotorFor, Off);
    digitalWrite (PinMotorRev, Off);
}

// -------------------------------------
// repeatedly called to run motor, possibly toggle step
void motorRun ()
{
    if (dirFor)
        digitalWrite (PinMotorFor, On);
    else
        digitalWrite (PinMotorRev, On);

    // or step motor
}

// -----------------------------------------------------------------------------
const unsigned long OneSec = 1000;
      unsigned long msecBut;

bool tmr;

// return true if start butter pressed for 1 second
bool
startPressed ()
{
    unsigned long msec = millis ();

    if (tmr && msec - msecBut >= OneSec)  {
        tmr = false;
        return true;
    }

    // capture/reset but timestamp
    byte but = digitalRead (PinStart);
    if (! tmr && LOW == but)  {
        tmr     = true;
        msecBut = msec;
        Serial.println ("startPressed: msecBut set");
    }
    else if (tmr && HIGH == but)  {
        tmr = false;
        Serial.println ("startPressed: msecBut reset");
    }

    return false;
}

// -----------------------------------------------------------------------------
void
loop (void)
{
    switch (state) {
    case Stop:
        // Serial.println ("  Stop");
        if (startPressed())  {
            state = Run;
            Serial.println ("  state = Run");
        }
        break;

    case Run:
        // Serial.println ("  Run");
        if (LOW == digitalRead (PinStopLmt))  {
            motorStop ();
            state  = Stop;
            dirFor = ! dirFor;                  // toggle direction
            Serial.println ("  state = Stop");
        }
        else
            motorRun ();
        break;
    }
}

// -----------------------------------------------------------------------------
void
setup (void)
{
    Serial.begin (9600);

    pinMode (PinMotorFor, OUTPUT);
    pinMode (PinMotorRev, OUTPUT);
    motorStop ();

    pinMode (PinStart,   INPUT_PULLUP);
    pinMode (PinStopLmt, INPUT_PULLUP);
}

Hello,
I wrote the schema and codes in the link below. Can you check it?
Currently there are 12 volt and 5 volt dc currents available.

Hello,
I wrote the schema and codes in the link below. Can you check it?
Currently there are 12 volt and 5 volt dc currents available.

Nothing works in that. You need to draw a wiring diagram of your project.

Please until you are naturally typing formatted code, let the IDE help. I will make your code easier to read, and make certain errors much more obvious.

Just apply the IDE Autoformat tool.

Please also post your code in tags. Take the advice of @UKHeliBob in post #2 above.

You will greatly increase the amount of attention your code gets.

a7

Hi @alto777
Thank you for your interest. I don't know how to do what you say. can you help

  • you didn't wire in the start and stop buttons
  • the stepper motor connections weren't correct

look over the following

@gcjr hi,
Thank you for your interest, m. Something went wrong, I burned it to the Arduino. The items I mentioned above need to be coded for me and it will have 3 buttons. Right turn, stop and left turn.

i only see start/stop buttons, reed switch and left/right limit switches. when did left/right come up.

your code suggested that the motor alternates direction after each time it is stopped

if the direction is know, is there a need for separate CW/CCW limit switches, why can't they both be connected to the same pin?

Please replace kodu buraya yazın veya yapıştırın by your code; it could not be much easier :wink:

@sterretje hi,

// 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();
}

@gcjr,
I have explained above how it will be done. There are 3 buttons in the diagram. There will be 3 buttons in the project I will do. 1 button for left turn, 1 button for stop, 1 button for right turn.

your code suggests otherwise

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.