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