How can I turn on a LED after full rotation on stepper motor

Hi All,

I need some help with my code. I have a Stepper motor (28BYJ-48+ ULN2003 DC5V Reduction Step Gear Stepper Motor)

This motor turns at 45 degrees on every command received from bluetooth.
So what I would like to achieve is when the stepper motor as done a full rotation or the command has been sent 8 time (full rotation) then I want an LED to light up. When the command has been sent again the LED needs to turn off.

At the moment I have a LED light up everytime a command has been received, the stepper motor turn 45 degrees and the LED turns off.

I just now need help on the second LEDE to light up once a full rotation has been completed.

Here is my code I have at the moment. Please guide me in the right direction to achieve this.

#include "BluetoothSerial.h"
#include "esp_bt_device.h"
#include <Stepper.h>
 
#define STEPS_PER_MOTOR_REVOLUTION 32   
#define STEPS_PER_OUTPUT_REVOLUTION 32 * 64  //2048  
Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, 25, 27, 26, 33);
int  Steps2Take;

// Check if Bluetooth configs are enabled
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

// Bluetooth Serial object
BluetoothSerial SerialBT;

String message = "";
char incomingChar;
int stepCount = 0;
const int ledPin =  32;
int SensorPin = 35;
int currentSensorState;
int lastSensorState;
unsigned long readTimer;

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  pinMode(SensorPin, INPUT);
  Serial.begin(115200);
  // Bluetooth device name
  SerialBT.begin("Bluetooth");
  Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {
  unsigned long currentMillis = millis();
  currentSensorState = digitalRead(SensorPin);
  Steps2Take  =  STEPS_PER_OUTPUT_REVOLUTION / 7.99;  
  // Read received messages (LED control command)
  if (SerialBT.available()){
    char incomingChar = SerialBT.read();
    if (incomingChar != '\n'){
      message += String(incomingChar);
    }
    else{
      message = "";
    }
    Serial.write(incomingChar);  
  }
  // Check received message and control output accordingly
  if (message =="a"){   
    digitalWrite(ledPin, LOW);      
    small_stepper.setSpeed(900);   
    small_stepper.step(Steps2Take);
    digitalWrite(ledPin, HIGH);
  }
}

Thanks

add a global variable and every time you rotate by 45° (every time you receive a command) you increment this variable. When that variable is 8 you turn on the led and reset the variable to 0 otherwise you turn the led off

may be something like this (cleaned up a bit your code)

#include "BluetoothSerial.h"
#include "esp_bt_device.h"
#include <Stepper.h>

#define STEPS_PER_MOTOR_REVOLUTION 32
#define STEPS_PER_OUTPUT_REVOLUTION 32 * 64  //2048  
const long Steps2Take =  STEPS_PER_OUTPUT_REVOLUTION / 7.99; // <===== why 7.99 ????

Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, 25, 27, 26, 33);

// Check if Bluetooth configs are enabled
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

// Bluetooth Serial object
BluetoothSerial SerialBT;

int stepCount = 0;
const byte ledPin =  32;

void setup() {
  pinMode(ledPin, OUTPUT);
  small_stepper.setSpeed(900);
  Serial.begin(115200);
  SerialBT.begin("Bluetooth");
  Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {
  switch (SerialBT.read()) {
    case 'a':
      if (++ stepCount >= 8) {
        digitalWrite(ledPin, HIGH);
        stepCount = 0;
      } else {
        digitalWrite(ledPin, LOW);
      }
      small_stepper.step(Steps2Take);
      break;

    default: delay(1); break;
  }
}

fully untested, typed here

what about

      if (! (++stepCount % 8))
        digitalWrite (ledPin, HIGH);
      else
        digitalWrite (ledPin, LOW);

turns on LED every 8th command

yeah modulo is more costly (code size, time) than the reset of the counter but that works too (probably need to make stepCount an unsigned integral type - a byte would be good enough) and the cost is probably negligible for the needs here... so whatever rocks OP's boat :slight_smile:

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