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