Publishing Data for Stepper Motor not Working

Hi all, I’m making a project that involves two Photons, a stepper motor, and a motor driver. Photon1 reads data from the Y axis of a ADXL362 accelerometer then publishes it once every second. Photon2 is supposed to subscribe to it and make the stepper turn accordingly, but that isn’t the case. I know Photon2 is getting the data because in the code I have it printing on the Serial monitor and I see it. I also know the motor driver works because the stepper is just barely making a step. So I have no clue why the motor shouldn’t be turning…

Photon1’s code:

> #include "adxl362/adxl362.h"
> ADXL362 xl;

> int16_t temp;
> int16_t XValue, YValue, ZValue, Temperature;
> int speed;
> int angle = 0;
> int potVal;
> void setup() {
>     Serial.begin(9600);
> xl.begin(SS);                   // Setup SPI protocol, issue device soft reset
>   xl.beginMeasure();              // Switch ADXL362 to measure mode  

> }

> void loop() {
> xl.readXYZTData(XValue, YValue, ZValue, Temperature);  

>
> Serial.print(YValue);

> int potSal = map(YValue, -1163, 1163, 0, 1023);
> Particle.publish("speed", String(potSal), 60, PRIVATE);
> delay(1000);
> }

Photon B’s code:

#include “Stepper/Stepper.h”
#include “math.h”

#define STEPS 200
#define ADCwidth 12
#define ADCsteps pow(2, ADCwidth)
#define maxSpeed 255//RPM (for test, using max LED value 255)

Stepper stepper1(STEPS, D0, D1, D2, D3);
int potSal;
String potSalAsAString;

int ledPin = D5;
int potVal = 0;

uint8_t speed = 0;

void myHandler(const char *event, const char *data){

speed = floor( data/(ADCsteps/maxSpeed));
analogWrite(ledPin, speed);
stepper1.setSpeed(1);
stepper1.step(1);
delay(1000);

 Serial.print(event);

Serial.print(", data: ");
if (data)
Serial.println(data);

else
Serial.println(“NULL”);
}

void setup() {
Serial.begin(9600);
Particle.subscribe(“speed”, myHandler, MY_DEVICES);
pinMode(potVal, INPUT);
pinMode(ledPin, OUTPUT); //LED brightness correlates to motor speed
stepper1.setSpeed(0);
}

void loop() {

}

I did the same thing with a servo where you tilt the accelerometer and the servo turns accordingly, but for some reason, I can’t get the stepper to work…

-Jack

How does this line even compile? You’re trying to divide a C string by an int.

Also, you say, “the stepper is just barely making a step”. Does that mean you’re seeing the stepper motor move one step? Isn’t that what you’re telling it to do with, stepper1.step(1);?

1 Like

@Ric

That’s part of the problem, and as for the motor, it’s just barely twitching. I don’t think it’s turning correctly but I can’t figure out why.

What’s part of the problem? Dividing a string by an int? If that’s really in your code, then it shouldn’t even compile, so I don’t understand how can you see the motor do anything.

If the motor is a 200 step motor, then 1 step is only 1.8 degrees. Is it doing that, or is it really just a twitch? If you do stepper1.step(200) does it make one revolution?

1 Like

It would be much easier to control stepper motors with something like this: https://www.adafruit.com/product/2653

@Ric

Would you have any tips on how to fix that line? (I’m very bad with strings and arrays :joy:)

You just need to add a line to convert the string to an int using atoi.

void myHandler(const char *event, const char *data){
    int intData = atoi(data);
    speed = floor( intData/(ADCsteps/maxSpeed));
    // etc.
}
1 Like

@Ric @Kurticus

Thanks for the help, it works good now :grinning: ! It’s ironic you suggested the atoi because that’s what I had first but when it wasn’t working, I figured it wasn’t right…

1 Like

@JackD12
Sorry I didn’t offer any help with your code. Was kind of a jerk comment on my behalf. This sounds like an interesting project, I’d like to see your project in action when you are done working on it! :smile:

@Kurticus

Hey no problem! If the code Rex suggested didn’t work I probably would’ve gotten that shield. The reason I’m doing this is because I’m making a car with my Photons, so when you tilt the one with the accelerometer forwards and to the side, the car drives forwards and/or turns. I’m not very good with the code yet (I’m 12).

1 Like