Control the stepper motor(2) —— APP Control Part
I'm trying to make a small car with stepper motors. You can see my last post here.
Now, let's try to use 'GoBLE' to control the speed of the motor. In the GoBLE Wiki, download the library first. The testing code named GoBLE_Test.ino can be found in the software package. It enables you to check the signal from your iPhone on PC. Have a try first!
In the picture we can see the data received from the app. Then we can try to control the speed of the motor!
Here's the code:
#include <Metro.h>
#include "GoBLE.h"
int joystickX;
int M1dirpin = 4;
int M1steppin = 5;
int M1en=12;
bool motorEnableFlag;
Metro blink1Metro = Metro(1000);
Metro blink2Metro = Metro(0);
void setup() {
// put your setup code here, to run once:
pinMode(M1dirpin,OUTPUT);
pinMode(M1steppin,OUTPUT);
pinMode(M1en,OUTPUT);
Goble.begin();
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(joystickX);
if(Goble.available())
{
joystickX = Goble.readJoystickX();
if (joystickX > 128)
{
digitalWrite(M1dirpin,LOW);
joystickX=255-joystickX;
}
else if (joystickX<128)
{
digitalWrite(M1dirpin,HIGH);
joystickX=joystickX-1;
}
if (joystickX==128)
motorEnableFlag=false;
else
motorEnableFlag=true;
}
Metro blink2Metro = Metro(joystickX/50);
if(blink1Metro.check()){
Serial.println(joystickX);
}
if(blink2Metro.check()&&motorEnableFlag){
digitalWrite(M1steppin,LOW);
digitalWrite(M1steppin,HIGH); //上升沿步进
}
}
I do this in a multi-threaded way. One for receiving app data, one for sending digital to motor.
OK, next time I will use 2 motors to make a car.
To be continued!