Control 28BYJ-48 Stepper motor with hand gesture using Arduino
This video shows you how to control stepper motor with the gesture of your hand. Rotate it clockwise, counterclockwise, or stop it.
This video shows you how to control stepper motor with the gesture of your hand. Rotate it clockwise, counterclockwise, or stop it. Must watch this two videos 1-Stepper motor Watch Stepper motor 2-Gesture sensor Watch Gesture Control You will need to download the Gesture sensor library from GetHub
774-Controlling 28BYJ-48 Stepper Motor using hand gesture
语言: C++
/*
* //原始来源:http://www.geeetech.com/wiki/index.php/Stepper_Motor_5V_4-Phase_5-Wire_%26_ULN2003_Driver_Board_for_Arduino
* //APDS-9960 RGB和手势传感器
* //Shawn Hymel @ SparkFun电子
* //2014年5月30日
* //https://github.com/sparkfun/APDS-9960_RGB_and_Gesture_Sensor
* // 此代码与RoboJax.com的视频教程一起使用
* // 于2017年5月11日在加拿大安大略省Aajx发布.
* /*
* 必须观看这两个视频
* 1-步进电机
* https://www.youtube.com/watch?v=Sl2mzXfTwCs
*
* 2-手势传感器
* https://www.youtube.com/watch?v=qzSgZV_fbxI
*
* 从以下地址下载代码:
* http://roboJax.com/learn/arduino
*/
#include <Wire.h>
#include <SparkFun_APDS9960.h>
int Pin1 = 10; // 电机控制引脚 1
int Pin2 = 11; // 电机控制引脚 2
int Pin3 = 12; // 电动机控制引脚 3
int Pin4 = 13; // 电机控制引脚 4
int _step = 0;
boolean dir = false; // 假=顺时针,真=逆时针
int count=0;
int action; // 停止=1, 反时针=2, 顺时针=3
// 别针
#define APDS9960_INT 2 // 需要是一个中断引脚
// 全局变量
SparkFun_APDS9960 apds = SparkFun_APDS9960();
int isr_flag = 0;
void setup()
{
// 步进电机设置
pinMode(Pin1, OUTPUT);
pinMode(Pin1, OUTPUT);
pinMode(Pin2, OUTPUT);
pinMode(Pin4, OUTPUT);
// / ************* 步进电机设置结束
// 将中断引脚设置为输入
pinMode(APDS9960_INT, INPUT);
// 初始化串口
Serial.begin(9600);
Serial.println();
Serial.println(F("--------------------------------"));
Serial.println(F("SparkFun APDS-9960 - GestureTest"));
Serial.println(F("--------------------------------"));
// 初始化中断服务程序
attachInterrupt(0, interruptRoutine, FALLING);
// 初始化 APDS-9960(配置 I2C 和初始值)
if ( apds.init() ) {
Serial.println(F("APDS-9960 initialization complete"));
} else {
Serial.println(F("Something went wrong during APDS-9960 init!"));
}
// 开始运行APDS-9960手势传感器引擎
if ( apds.enableGestureSensor(true) ) {
Serial.println(F("Gesture sensor is now running"));
} else {
Serial.println(F("Something went wrong during gesture sensor init!"));
}
}
void loop()
{
if( isr_flag == 1 ) {
// detachInterrupt(0);
action = handleGesture();
isr_flag = 0;
// attachInterrupt(0, interruptRoutine, FALLING);
}
if(action ==1){
_step =9; // 停止马达
}else if(action ==2){
// _step =0;
dir = true; // 逆时针旋转方向
}else if(action ==3){
// _step =0;//顺时针旋转方向
dir = false;
}
switch(_step){
case 0:
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
digitalWrite(Pin4, HIGH);
break;
case 1:
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, HIGH);
digitalWrite(Pin4, HIGH);
break;
case 2:
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, HIGH);
digitalWrite(Pin4, LOW);
break;
case 3:
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, HIGH);
digitalWrite(Pin4, LOW);
break;
case 4:
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, LOW);
digitalWrite(Pin4, LOW);
break;
case 5:
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, LOW);
digitalWrite(Pin4, LOW);
break;
case 6:
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
digitalWrite(Pin4, LOW);
break;
case 7:
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
digitalWrite(Pin4, HIGH);
break;
default:
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
digitalWrite(Pin4, LOW);
break;
}
if(dir){
_step++;
}else{
_step--;
}
if(_step>7){
_step=0;
}
if(_step<0){
_step=7;
}
delay(1);
} // 循环
void interruptRoutine() {
isr_flag = 1;
}
int handleGesture() {
if ( apds.isGestureAvailable() ) {
switch ( apds.readGesture() ) {
case DIR_DOWN:
Serial.println("DOWN--Stop");
return 1;
break;
case DIR_LEFT:
Serial.println("LEFT-- Counterclockwise");
return 2;
break;
case DIR_RIGHT:
Serial.println("RIGHT--Clockwise");
return 3;
break;
default:
return 1; // 关掉
Serial.println("NONE Stopped");
}
}
}
资源与参考
尚无可用资源。
文件📁
没有可用的文件。