These days, I want to make a small car with stepper motors and use my smart phone to control it. Searching on the internet for many days, I find a perfect app called 'GoBLE'. Its interface is similar as a game controller. Arduino Bluno is also what I want. It's a development board that integrates a TI CC2540 Bluetooth 4.0 chip.So, I can control the speed and direction of the car with these two stuff. I also need a stepper motor driver. (Maybe I can use LattePanda to do this project! It's a Win10 development board with Arduino processor. (Still working on it)
And here's my car model. A little simple. Anyway, it can run.
First, let's try to run the motor. I need to buy a stepper motor driver. I find a nice driver from DFRobot that is compatible with the Arduino Bluno. In the Wiki, there's a sample code for the stepper motor. Upload to the board. You can see the motor is running.
You can change the direction and speed by setting the time delay and direction pin.
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!
#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); //上升沿步进
}
}
Plans for the futureI 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!
Comments