Created
November 24, 2015 15:17
-
-
Save plopp/a199101ea1bad0d44315 to your computer and use it in GitHub Desktop.
Thread Particle.io - Controlling a stepper motor with a wheel on it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This #include statement was automatically added by the Particle IDE. | |
#include "SparkJson/SparkJson.h" | |
//Includes | |
#include "MQTT/MQTT.h" | |
#include "math.h" | |
//Defines | |
#define STEPS 200 | |
#define ADCwidth 12 | |
#define ADCsteps pow(2, ADCwidth) | |
#define maxSpeed 256 //RPM (for test, using max LED value 255) | |
#define motor_pin_1 D0 | |
#define motor_pin_2 D1 | |
#define motor_pin_3 D2 | |
#define motor_pin_4 D3 | |
#define MAX_ACT 2000 | |
uint8_t speed = 0; | |
//MQTT-init | |
void callback(char* topic, uint8_t* payload, unsigned int length); | |
byte server[] = { 192,168,1,234 }; //Server IP-address for MQTT | |
MQTT client(server, 1883, callback); | |
#define MQTT_POWER_TOPIC "/thread" | |
unsigned long del = 2; | |
unsigned long step = 0; | |
int forward = 0; //0=forward 1=backward 2=stand still | |
int req = 0; | |
int act = 0; | |
void callback(char* topic, byte* payload, unsigned int length) { | |
char p[length + 1]; | |
memcpy(p, payload, length); | |
p[length] = '\0'; | |
StaticJsonBuffer<200> jsonBuffer; | |
JsonObject& root = jsonBuffer.parseObject(p); | |
const char* svalue = root["height"]; | |
String value(svalue); | |
Spark.publish("height",value); | |
req = value.toInt(); | |
if(req > MAX_ACT){ | |
req = MAX_ACT; | |
} | |
else if(req < 0){ | |
req = 0; | |
} | |
} | |
void setup() { | |
pinMode(motor_pin_1, OUTPUT); | |
pinMode(motor_pin_2, OUTPUT); | |
pinMode(motor_pin_3, OUTPUT); | |
pinMode(motor_pin_4, OUTPUT); | |
client.connect("sparkclient"); // connect to the MQTT server | |
if (client.isConnected()) { | |
Spark.publish("Subscribear","nu"); | |
client.subscribe(MQTT_POWER_TOPIC); | |
} | |
} | |
void loop() { | |
//potVal = analogRead(potPin); | |
//speed = floor(potVal/(ADCsteps/maxSpeed)); | |
if(!client.isConnected()){ | |
Spark.publish("CONNECT","NO"); | |
client.connect("sparkclient"); // connect to the MQTT server | |
client.subscribe(MQTT_POWER_TOPIC); | |
} | |
else{ | |
//Spark.publish("CONNECT","YES"); | |
client.loop(); | |
//Spark.publish("MQTT","LOOPED"); | |
} | |
switch(forward){ | |
case 0: | |
digitalWrite(motor_pin_1, HIGH); | |
digitalWrite(motor_pin_2, LOW); | |
digitalWrite(motor_pin_3, LOW); | |
digitalWrite(motor_pin_4, HIGH); | |
delay(del); | |
digitalWrite(motor_pin_1, HIGH); | |
digitalWrite(motor_pin_2, HIGH); | |
digitalWrite(motor_pin_3, LOW); | |
digitalWrite(motor_pin_4, LOW); | |
delay(del); | |
digitalWrite(motor_pin_1, LOW); | |
digitalWrite(motor_pin_2, HIGH); | |
digitalWrite(motor_pin_3, HIGH); | |
digitalWrite(motor_pin_4, LOW); | |
delay(del); | |
digitalWrite(motor_pin_1, LOW); | |
digitalWrite(motor_pin_2, LOW); | |
digitalWrite(motor_pin_3, HIGH); | |
digitalWrite(motor_pin_4, HIGH); | |
delay(del); | |
step++; | |
break; | |
case 1: | |
digitalWrite(motor_pin_1, LOW); | |
digitalWrite(motor_pin_2, LOW); | |
digitalWrite(motor_pin_3, HIGH); | |
digitalWrite(motor_pin_4, HIGH); | |
delay(del); | |
digitalWrite(motor_pin_1, LOW); | |
digitalWrite(motor_pin_2, HIGH); | |
digitalWrite(motor_pin_3, HIGH); | |
digitalWrite(motor_pin_4, LOW); | |
delay(del); | |
digitalWrite(motor_pin_1, HIGH); | |
digitalWrite(motor_pin_2, HIGH); | |
digitalWrite(motor_pin_3, LOW); | |
digitalWrite(motor_pin_4, LOW); | |
delay(del); | |
digitalWrite(motor_pin_1, HIGH); | |
digitalWrite(motor_pin_2, LOW); | |
digitalWrite(motor_pin_3, LOW); | |
digitalWrite(motor_pin_4, HIGH); | |
delay(del); | |
step--; | |
break; | |
case 2: | |
digitalWrite(motor_pin_1, LOW); | |
digitalWrite(motor_pin_2, LOW); | |
digitalWrite(motor_pin_3, LOW); | |
digitalWrite(motor_pin_4, LOW); | |
delay(10); | |
break; | |
} | |
act = step; | |
if((req-act) > 0){ | |
forward = 0; | |
} | |
else if((req-act) < 0){ | |
forward = 1; | |
} | |
else{ | |
forward = 2; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment