Hello i am making a project that will allow me to turn on and off a light and change the speed of a stepper motor via Bluetooth. I can turn the light on and off but i cannot operate the stepper.
The motor will have 3 fixed speeds sort of like a table fan.
The speeds will be like this
If i send 2 through my Bluetooth terminal the stepper will be off
If i send 3 the stepper will run slowly
If i send 4 it will run faster.
I am using 9V batteries to power the motor. I know they don't provide enough current but i tested it for those speeds and it works. Also i used the examples of the Accelestepper library to make sure the thing works and it does.
Alright, then put a meter on the 9V motor power and measure the voltage WHILE you are trying to run it.
Did you add the serial prints to your code ?
Post the new code with the serial prints on the line before the speed command.
tefras14:
I am using 9V batteries to power the motor. I know they don't provide enough current but i tested it for those speeds and it works. Also i used the examples of the Accelestepper library to make sure the thing works and it does.
No chance, you need proper supply for the motors, capable of much higher currents
than a 9V battery can provide. What is the motor and motor driver?
Sorry for the delay in replying. I have added the serial prints and i know that the arduino gets my input. Also i have done more testing with the examples that come with the library and i know that the motor runs. But when i implement the code of the examples in my own code the motor does not start but the lights on the driver turn on. Here is the new code.
#include <AccelStepper.h>
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
char INBYTE; //declare variable for bluetooth COM
const int ledPin = 13; // LED connected to pin 13
const int arrayPin = 7; // Array to pin 7
void setup()
{
Serial.begin(9600); // Bletooth baud rate
pinMode(ledPin, OUTPUT); // LED pin as output
pinMode(arrayPin, OUTPUT); // Array as output
stepper.setMaxSpeed(1000);
stepper.setSpeed(500);
stepper.runSpeed();
}
void loop()
{
digitalWrite(ledPin, HIGH);
while (!Serial.available()); // Wait for input
INBYTE = Serial.read(); // read next available byte
stepper.runSpeed();
if( INBYTE == '0' )
{
Serial.println(INBYTE);
digitalWrite(arrayPin, HIGH); // if it's a 0 (zero) tun LED off
}
if( INBYTE == '1' )
{
Serial.println(INBYTE);
digitalWrite(arrayPin, LOW); // if it's a 1 (one) turn LED on
}
if( INBYTE == '2' )
{
Serial.println(INBYTE);
stepper.setMaxSpeed(1000);
stepper.setSpeed(0);
stepper.runSpeed();
}
if( INBYTE == '3' )
{
Serial.println(INBYTE);
stepper.setMaxSpeed(1000);
stepper.setSpeed(200);
stepper.runSpeed();
}
if( INBYTE == '4' )
{
Serial.println(INBYTE);
stepper.setMaxSpeed(1000);
stepper.setSpeed(500);
stepper.runSpeed();
}
stepper.runSpeed();
delay(50);
}
raschemmel:
Where's the voltage measurements I asked for ?
Unfortunately i don't have a voltmeter. But i know that the motor works with the batteries since i tested it using the examples that come from the Accelstepper library. Also i only power the motor from the batteries so they are not overloaded
raschemmel:
Did you try commenting out all the bluetooth code (and possibly the serial prints if necessary ) and just running the steppers ?
What I mean is comment out the "IF " statements so the stepper code executes UNCONDITIONALLY .
Yes actually the rest of the code was built around the stepper code. The fact is that the stepper code is the constant speed example that comes with the library i use so i know it works. I just copy paste the code with different speeds in the IF statements because i want to have 3 distinct speeds.
Looking at the code in Reply #11 I suspect that loop() is getting another value for INBYTE before the stepper has had time to deal with the previous one. The code won't wait for stepper.run() to complete its stuff.
I think you need to split your code into two chunks - one which reads the input and updates a global variable whenever a different input is received and the other which moves the motor depending on what is in the global variable.
Something like this
int stepperSpeed = 0;
void setup() {
// all the necessary stuff
}
void loop() {
getStuffFromSerial();
moveMotor();
}
void getStuffFromSerial() {
// read serial input and store the appropriate value in stepperSpeed
}
void moveMotor() {
stepper.setSpeed(stepperSpeed);
stepper.runSpeed();
}
raschemmel:
Where's the voltage measurements I asked for ?
Unfortunately i don't have a voltmeter. But i know that the motor works with the batteries since i tested it using the examples that come from the Accelstepper library. Also i only power the motor from the batteries so they are not overloaded
The whole point is they are being overloaded since they are puny 9V batteries. You need the
right power supply to get reliable operation.