Arduino程序
主函数
#include
#include
#include "WiFi.h"
#include "PubSubClient.h"
#include "Ticker.h"
#include "DHT.h"
AITA_DHT dht11;
uint8_t temperature, humidity;
const char *ssid = "八仙过海少了俩"; //wifi名
const char *password = "jisuanji124#"; //wifi密码
const char *mqtt_server = "183.230.40.39"; //onenet 的 IP地址
#define mqtt_devid "734129701" //设备ID
#define mqtt_pubid "439837" //产品ID
#define mqtt_password "esp32smart" //鉴权信息
#define CONL0 32 //GPIO32控制灯0
#define CONL1 27 //GPIO27控制灯1
// 定义电机控制用常量
// 电机内部输出轴旋转一周步数
const int STEPS_PER_ROTOR_REV = 32;
// 减速比
const int GEAR_REDUCTION = 64;
/*
* 转子旋转一周需要走32步。转子每旋转一周,电机输出轴只旋转1/64周。
* (电机内部配有多个减速齿轮,这些齿轮会的作用是让转子每旋转一周,
* 输出轴只旋转1/64周。)
* 因此电机输出轴旋转一周则需要转子走32X64=2048步,即以下常量定义。
*/
// 电机外部输出轴旋转一周步数 (2048)
const float STEPS_PER_OUT_REV = STEPS_PER_ROTOR_REV * GEAR_REDUCTION;
// 定义电机控制用变量
// 电机旋转步数
int StepsRequired;
// 建立步进电机对象
// 定义电机控制引脚以及电机基本信息。
// 电机控制引脚为 16,17,18,19 (16,18,17,19)
// 以上引脚依次连接在ULN2003 驱动板 In1, In2, In3, In4
Stepper steppermotor(STEPS_PER_ROTOR_REV, 16,18,17, 19);
#define AD5 34 //定义光敏模块模拟口
#define LED 22 //定义数字口
#define CONL0 32 //GPIO32控制灯0
#define CONL1 27 //GPIO27控制灯1
int Intensity = 0;//光照度数值
int a=0;
int b=0;
int c=0;
int level=0;//级数差
WiFiClient espClient; //创建一个WIFI连接客户端
PubSubClient client(espClient); // 创建一个PubSub客户端, 传入创建的WIFI客户端
char msg_buf[200]; //发送信息缓冲区
char dataTemplate[] = "{\"temp\":%d,\"humi\":%d,\"intensity\":%d}"; //信息模板
char msgJson[75]; //要发送的json格式的数据
unsigned short json_len = 0; //json长度
Ticker tim1; //定时器,用来循环上传数据
//连接WIFI相关函数
void setupWifi()
{
delay(10);
Serial.println("连接WIFI");
WiFi.begin(ssid, password);
while (!WiFi.isConnected())
{
Serial.print(".");
delay(500);
}
Serial.println("OK");
Serial.println("Wifi连接成功");
}
// 快速逆时针旋转:往下(小幅度)
void blindsdownsmall(){
StepsRequired = - STEPS_PER_OUT_REV;
steppermotor.setSpeed(800);
steppermotor.step(StepsRequired);
delay(2000);
}
// 快速顺时针旋转:往上(小幅度)
void blindsupsmall(){
StepsRequired = STEPS_PER_OUT_REV;
steppermotor.setSpeed(800);
steppermotor.step(StepsRequired);
delay(2000);
}
// 快速逆时针旋转:往下(大幅度)
void blindsdownlarge(){
StepsRequired = - STEPS_PER_OUT_REV*3;
steppermotor.setSpeed(800);
steppermotor.step(StepsRequired);
delay(2000);
}
// 快速顺时针旋转:往上(大幅度)
void blindsuplarge(){
StepsRequired = STEPS_PER_OUT_REV*3;
steppermotor.setSpeed(800);
steppermotor.step(StepsRequired);
delay(2000);
}
// 快速逆时针旋转:向下旋转角度
void blindsrotatedown(){
StepsRequired = - STEPS_PER_OUT_REV*0.2;
steppermotor.setSpeed(800);
steppermotor.step(StepsRequired);
delay(2000);
}
// 顺时针旋转:向上旋转角度
void blindsrotateup(){
StepsRequired = STEPS_PER_OUT_REV*0.2;
steppermotor.setSpeed(800);
steppermotor.step(StepsRequired);
delay(2000);
}
//自动初始化
void blindautoinit(){
StepsRequired = STEPS_PER_OUT_REV;
steppermotor.setSpeed(500);
steppermotor.step(StepsRequired);
delay(2000);
}
//收到主题下发的回调, 这个回调要实现三个形参 1:topic 主题, 2: payload: 传递过来的信息 3: length: 长度
void callback(char *topic, byte *payload, unsigned int length)
{
Serial.println("message rev:");
Serial.println(topic);
for (size_t i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
}
Serial.println();
if (payload[0] == 0x30) //如果第一个接收字符是0
{
if (payload[1] == 0x30) //第二个接收字符是0,则接收“00”,熄灯L0,百叶窗小幅度下降
{
digitalWrite(CONL0, HIGH);
blindsdownsmall();
}
else if(payload[1] == 0x31) //第二个接收字符是1,则接收“01”,点亮L0,百叶窗大幅度下降
{
digitalWrite(CONL0, LOW);
blindsdownlarge();
}
else if(payload[1] == 0x32) //第三个接收字符是2,则接收“02”,百叶窗向下旋转
{
blindsrotatedown();
}
else if(payload[1] == 0x33) //第四个接收字符是3,则接收“03”,百叶窗自动调节角度
{
blindautoinit();
c=1;
}
}
else if (payload[0] == 0x31) //如果第一个接收字符是1
{
if (payload[1] == 0x30) //第二个接收字符是0,则接收“10”,熄灯L1,百叶窗小幅度上升
{
digitalWrite(CONL1, HIGH);
blindsupsmall();
}
else if(payload[1] == 0x31) //第二个接收字符是1,则接收“11”,点亮L1,百叶窗大幅度上升
{
digitalWrite(CONL1, LOW);
blindsuplarge();
}
else if(payload[1] == 0x32) //第三个接收字符是2,则接收“12”,百叶窗向上旋转
{
blindsrotateup();
}
else if(payload[1] == 0x33) //第四个接收字符是3,则接收“13”,百叶窗手动调节
{
c=0;
}
}
}
void sendDHT(){
Serial.print("Read sensor: ");
Serial.print("Humidity (%): ");
Serial.println(humidity);
Serial.print("Temperature (oC): ");
Serial.println(temperature);
}
void sendIntensity(){
Serial.print("Intensity = "); //串口输出"Intensity = "
Serial.println(Intensity); //串口输出Intensity变量的值,并换行
}
//向主题发送模拟的数据
void sendDate()
{
if (client.connected())
{
dht11.dht11Read(&temperature, &humidity); //得到温湿度
Intensity = analogRead(AD5); //读取模拟口AD5的值,存入Intensity变量
snprintf(msgJson, 40, dataTemplate, temperature, humidity, Intensity); //将模拟温湿度数据套入dataTemplate模板中, 生成的字符串传给msgJson
json_len = strlen(msgJson); //msgJson的长度
msg_buf[0] = char(0x03); //要发送的数据必须按照ONENET的要求发送, 根据要求,数据第一位是3
msg_buf[1] = char(json_len >> 8); //数据第二位是要发送的数据长度的高八位
msg_buf[2] = char(json_len & 0xff); //数据第三位是要发送数据的长度的低八位
memcpy(msg_buf + 3, msgJson, strlen(msgJson)); //从msg_buf的第四位开始,放入要传的数据msgJson
msg_buf[3 + strlen(msgJson)] = 0; //添加一个0作为最后一位, 这样要发送的msg_buf准备好了
Serial.print("public message:");
Serial.println(msgJson);
client.publish("$dp", (uint8_t *)msg_buf, 3 + strlen(msgJson)); //发送数据到主题$dp
}
}
//重连函数, 如果客户端断线,可以通过此函数重连
void clientReconnect()
{
while (!client.connected()) //再重连客户端
{
Serial.println("reconnect MQTT...");
if (client.connect(mqtt_devid, mqtt_pubid, mqtt_password))
{
Serial.println("connected");
}
else
{
Serial.println("failed");
Serial.println(client.state());
Serial.println("try again in 5 sec");
delay(5000);
}
}
}
void setup()
{
pinMode(CONL0, OUTPUT);
pinMode(CONL1, OUTPUT);
digitalWrite(CONL0, HIGH);
digitalWrite(CONL1, HIGH);
pinMode(LED,OUTPUT);//设置LED为输出模式
pinMode(AD5,INPUT);//设置AD5为输入模式
Serial.begin(115200); //初始化串口
delay(3000); //这个延时是为了让我打开串口助手
setupWifi(); //调用函数连接WIFI
client.setServer(mqtt_server, 6002); //设置客户端连接的服务器,连接Onenet服务器, 使用6002端口
client.connect(mqtt_devid, mqtt_pubid, mqtt_password); //客户端连接到指定的产品的指定设备.同时输入鉴权信息
client.setCallback(callback); //设置好客户端收到信息是的回调
tim1.attach(20, sendDate); //定时每20秒调用一次发送数据函数sendDate
}
void loop()//程序主体循环
{
if (!WiFi.isConnected()) //先看WIFI是否还在连接
{
setupWifi();
}
if (!client.connected()) //如果客户端没连接ONENET, 重新连接
{
clientReconnect();
}
client.loop(); //客户端循环检测
Intensity = analogRead(AD5); //读取模拟口AD5的值,存入Intensity变量
sendIntensity();
delay(1000); //延时1000ms
dht11.dht11Read(&temperature, &humidity);
sendDHT();
delay(1000); //延时1000ms
//判断是否是自动调整模式,如果是,根据光照强度数值进行窗叶调整角度
if(c==1)
{
a= Intensity/200+1;//在此,将光照强度分为10个等级
if(a>10)
a=10;
if(a>b)
{
level=a-b;
b=a;
StepsRequired = - STEPS_PER_OUT_REV*0.05*level; //反转,向下
steppermotor.setSpeed(800);
steppermotor.step(StepsRequired);
delay(1000);
}
if(a {
level=b-a;
b=a;
StepsRequired = STEPS_PER_OUT_REV*0.05*level; //正转,向上
steppermotor.setSpeed(800);
steppermotor.step(StepsRequired);
delay(1000);
}
}
}
DHT.cpp
/* Includes ------------------------------------------------------- */
#include "DHT.h"
/* Private functions ---------------------------------------------- */
//DHT11初始化函数
uint8_t dht11_init(void) {
uint8_t errcode = SUCC;
uint8_t i = 0;
IO_OUTMODE //设置IO输出
IO_SET //IO输出1
DELAY_MS(2); //延时2ms,为下降沿做准备
IO_CLR //IO输出0,产生下降沿
DELAY_MS(20); //延时20ms,主机下降沿并保持,18ms以上的低电平启动DHT11
IO_SET //再次拉高IO,等待DHT11拉低以作应答
IO_INMODE //设置IO输入
DELAY_US(40); //延时40us,等待DHT11输入的低电平应答信号
//等待DHT11拉低IO,最高持续MAXCOUNT个us
while(READ_IO==1 && i i++;
DELAY_US(1);
}
if(i>=MAXCOUNT) {
errcode = INITTOUT;
return errcode;
} else i = 0;
//DHT11拉低IO持续80us,后会在拉高IO持续80us作为完整的应答
while (READ_IO==0 && i i++;
DELAY_US(1);
}
if(i>=MAXCOUNT) {
errcode = INITTOUT;
return errcode;
}
return errcode;
}
//读取一个bit函数
uint8_t dht11_readBit(void) {
uint8_t i = 0;
//等待IO变低
while(READ_IO==1 && i i++;
DELAY_US(1);
}
i = 0;
//等待IO变高——先低再高表示开始传输一个bit
while (READ_IO==0 && i i++;
DELAY_US(1);
}
DELAY_US(40); //等待40us(0 bit)
if(READ_IO==1) return 1;
else return 0;
}
//读取一个Byte函数
uint8_t dht11_readByte(void) {
uint8_t i = 0;
uint8_t data = 0;
for(i=0; i<8; i++) {
data <<= 1;
data |= dht11_readBit();
}
return data;
}
/* Class functions ------------------------------------------------ */
//读取温湿度值
uint8_t AITA_DHT::dht11Read(uint8_t *temp, uint8_t *humi) {
uint8_t buf[5];
uint8_t i;
if(!dht11_init()) {
for(i=0; i<5; i++) {
buf[i] = dht11_readByte();
}
humidity_integer = buf[0];
humidity_decimal = buf[1];
temperature_integer = buf[2];
temperature_decimal = buf[3];
checksum = buf[0]+buf[1]+buf[2]+buf[3];
if((buf[0]+buf[1]+buf[2]+buf[3]) == buf[4]) {
*humi = buf[0];
*temp = buf[2];
} else {
return CHECKSUM;
}
} else {
return INITTOUT;
}
return SUCC;
}
DHT.h
#ifndef __DHT_H_
#define __DHT_H_
/* Includes ------------------------------------------------------- */
#include
/* Exported macro ------------------------------------------------- */
#define SUCC 0 //成功编码
#define INITTOUT 1 //初始化超时错误编码
#define CHECKSUM 2 //校验和错误编码
#define MAXCOUNT 100 //溢出次数,超过此值则超时
#define DHT_PIN 0 //控制DHT11的IO编号,此处为:GPIO0
#define IO_OUTMODE pinMode(DHT_PIN, OUTPUT);//初始化GPIO0为输出
#define IO_INMODE pinMode(DHT_PIN, INPUT); //设置IO位输入
#define IO_SET digitalWrite(DHT_PIN, HIGH);//IO输出高
#define IO_CLR digitalWrite(DHT_PIN, LOW);//IO输出低
#define READ_IO digitalRead(DHT_PIN) //读取IO输入
#define DELAY_MS delay //ms级延时函数
#define DELAY_US delayMicroseconds //us级延时函数
/* Exported class declaration ------------------------------------- */
class AITA_DHT {
public:
uint8_t dht11Read(uint8_t *temp, uint8_t *humi);
uint8_t humidity_integer;
uint8_t temperature_integer;
uint8_t humidity_decimal;
uint8_t temperature_decimal;
uint8_t checksum;
};
#endif
Stepper.cpp
/*
* Stepper.cpp - Stepper library for Wiring/Arduino - Version 1.1.0
*
* Original library (0.1) by Tom Igoe.
* Two-wire modifications (0.2) by Sebastian Gassner
* Combination version (0.3) by Tom Igoe and David Mellis
* Bug fix for four-wire (0.4) by Tom Igoe, bug fix from Noah Shibley
* High-speed stepping mod by Eugene Kozlenko
* Timer rollover fix by Eugene Kozlenko
* Five phase five wire (1.1.0) by Ryan Orendorff
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*
* Drives a unipolar, bipolar, or five phase stepper motor.
*
* When wiring multiple stepper motors to a microcontroller, you quickly run
* out of output pins, with each motor requiring 4 connections.
*
* By making use of the fact that at any time two of the four motor coils are
* the inverse of the other two, the number of control connections can be
* reduced from 4 to 2 for the unipolar and bipolar motors.
*
* A slightly modified circuit around a Darlington transistor array or an
* L293 H-bridge connects to only 2 microcontroler pins, inverts the signals
* received, and delivers the 4 (2 plus 2 inverted ones) output signals
* required for driving a stepper motor. Similarly the Arduino motor shields
* 2 direction pins may be used.
*
* The sequence of control signals for 5 phase, 5 control wires is as follows:
*
* Step C0 C1 C2 C3 C4
* 1 0 1 1 0 1
* 2 0 1 0 0 1
* 3 0 1 0 1 1
* 4 0 1 0 1 0
* 5 1 1 0 1 0
* 6 1 0 0 1 0
* 7 1 0 1 1 0
* 8 1 0 1 0 0
* 9 1 0 1 0 1
* 10 0 0 1 0 1
*
* The sequence of control signals for 4 control wires is as follows:
*
* Step C0 C1 C2 C3
* 1 1 0 1 0
* 2 0 1 1 0
* 3 0 1 0 1
* 4 1 0 0 1
*
* The sequence of controls signals for 2 control wires is as follows
* (columns C1 and C2 from above):
*
* Step C0 C1
* 1 0 1
* 2 1 1
* 3 1 0
* 4 0 0
*
* The circuits can be found at
*
* http://www.arduino.cc/en/Tutorial/Stepper
*/
#include "Arduino.h"
#include "Stepper.h"
/*
* two-wire constructor.
* Sets which wires should control the motor.
*/
Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2)
{
this->step_number = 0; // which step the motor is on
this->direction = 0; // motor direction
this->last_step_time = 0; // time stamp in us of the last step taken
this->number_of_steps = number_of_steps; // total number of steps for this motor
// Arduino pins for the motor control connection:
this->motor_pin_1 = motor_pin_1;
this->motor_pin_2 = motor_pin_2;
// setup the pins on the microcontroller:
pinMode(this->motor_pin_1, OUTPUT);
pinMode(this->motor_pin_2, OUTPUT);
// When there are only 2 pins, set the others to 0:
this->motor_pin_3 = 0;
this->motor_pin_4 = 0;
this->motor_pin_5 = 0;
// pin_count is used by the stepMotor() method:
this->pin_count = 2;
}
/*
* constructor for four-pin version
* Sets which wires should control the motor.
*/
Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2,
int motor_pin_3, int motor_pin_4)
{
this->step_number = 0; // which step the motor is on
this->direction = 0; // motor direction
this->last_step_time = 0; // time stamp in us of the last step taken
this->number_of_steps = number_of_steps; // total number of steps for this motor
// Arduino pins for the motor control connection:
this->motor_pin_1 = motor_pin_1;
this->motor_pin_2 = motor_pin_2;
this->motor_pin_3 = motor_pin_3;
this->motor_pin_4 = motor_pin_4;
// setup the pins on the microcontroller:
pinMode(this->motor_pin_1, OUTPUT);
pinMode(this->motor_pin_2, OUTPUT);
pinMode(this->motor_pin_3, OUTPUT);
pinMode(this->motor_pin_4, OUTPUT);
// When there are 4 pins, set the others to 0:
this->motor_pin_5 = 0;
// pin_count is used by the stepMotor() method:
this->pin_count = 4;
}
/*
* constructor for five phase motor with five wires
* Sets which wires should control the motor.
*/
Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2,
int motor_pin_3, int motor_pin_4,
int motor_pin_5)
{
this->step_number = 0; // which step the motor is on
this->direction = 0; // motor direction
this->last_step_time = 0; // time stamp in us of the last step taken
this->number_of_steps = number_of_steps; // total number of steps for this motor
// Arduino pins for the motor control connection:
this->motor_pin_1 = motor_pin_1;
this->motor_pin_2 = motor_pin_2;
this->motor_pin_3 = motor_pin_3;
this->motor_pin_4 = motor_pin_4;
this->motor_pin_5 = motor_pin_5;
// setup the pins on the microcontroller:
pinMode(this->motor_pin_1, OUTPUT);
pinMode(this->motor_pin_2, OUTPUT);
pinMode(this->motor_pin_3, OUTPUT);
pinMode(this->motor_pin_4, OUTPUT);
pinMode(this->motor_pin_5, OUTPUT);
// pin_count is used by the stepMotor() method:
this->pin_count = 5;
}
/*
* Sets the speed in revs per minute
*/
void Stepper::setSpeed(long whatSpeed)
{
this->step_delay = 60L * 1000L * 1000L / this->number_of_steps / whatSpeed;
}
/*
* Moves the motor steps_to_move steps. If the number is negative,
* the motor moves in the reverse direction.
*/
void Stepper::step(int steps_to_move)
{
int steps_left = abs(steps_to_move); // how many steps to take
// determine direction based on whether steps_to_mode is + or -:
if (steps_to_move > 0) { this->direction = 1; }
if (steps_to_move < 0) { this->direction = 0; }
// decrement the number of steps, moving one step each time:
while (steps_left > 0)
{
unsigned long now = micros();
// move only if the appropriate delay has passed:
if (now - this->last_step_time >= this->step_delay)
{
// get the timeStamp of when you stepped:
this->last_step_time = now;
// increment or decrement the step number,
// depending on direction:
if (this->direction == 1)
{
this->step_number++;
if (this->step_number == this->number_of_steps) {
this->step_number = 0;
}
}
else
{
if (this->step_number == 0) {
this->step_number = this->number_of_steps;
}
this->step_number--;
}
// decrement the steps left:
steps_left--;
// step the motor to step number 0, 1, ..., {3 or 10}
if (this->pin_count == 5)
stepMotor(this->step_number % 10);
else
stepMotor(this->step_number % 4);
}
}
}
/*
* Moves the motor forward or backwards.
*/
void Stepper::stepMotor(int thisStep)
{
if (this->pin_count == 2) {
switch (thisStep) {
case 0: // 01
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, HIGH);
break;
case 1: // 11
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, HIGH);
break;
case 2: // 10
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, LOW);
break;
case 3: // 00
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, LOW);
break;
}
}
if (this->pin_count == 4) {
switch (thisStep) {
case 0: // 1010
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, LOW);
digitalWrite(motor_pin_3, HIGH);
digitalWrite(motor_pin_4, LOW);
break;
case 1: // 0110
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, HIGH);
digitalWrite(motor_pin_3, HIGH);
digitalWrite(motor_pin_4, LOW);
break;
case 2: //0101
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, HIGH);
digitalWrite(motor_pin_3, LOW);
digitalWrite(motor_pin_4, HIGH);
break;
case 3: //1001
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, LOW);
digitalWrite(motor_pin_3, LOW);
digitalWrite(motor_pin_4, HIGH);
break;
}
}
if (this->pin_count == 5) {
switch (thisStep) {
case 0: // 01101
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, HIGH);
digitalWrite(motor_pin_3, HIGH);
digitalWrite(motor_pin_4, LOW);
digitalWrite(motor_pin_5, HIGH);
break;
case 1: // 01001
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, HIGH);
digitalWrite(motor_pin_3, LOW);
digitalWrite(motor_pin_4, LOW);
digitalWrite(motor_pin_5, HIGH);
break;
case 2: // 01011
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, HIGH);
digitalWrite(motor_pin_3, LOW);
digitalWrite(motor_pin_4, HIGH);
digitalWrite(motor_pin_5, HIGH);
break;
case 3: // 01010
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, HIGH);
digitalWrite(motor_pin_3, LOW);
digitalWrite(motor_pin_4, HIGH);
digitalWrite(motor_pin_5, LOW);
break;
case 4: // 11010
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, HIGH);
digitalWrite(motor_pin_3, LOW);
digitalWrite(motor_pin_4, HIGH);
digitalWrite(motor_pin_5, LOW);
break;
case 5: // 10010
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, LOW);
digitalWrite(motor_pin_3, LOW);
digitalWrite(motor_pin_4, HIGH);
digitalWrite(motor_pin_5, LOW);
break;
case 6: // 10110
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, LOW);
digitalWrite(motor_pin_3, HIGH);
digitalWrite(motor_pin_4, HIGH);
digitalWrite(motor_pin_5, LOW);
break;
case 7: // 10100
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, LOW);
digitalWrite(motor_pin_3, HIGH);
digitalWrite(motor_pin_4, LOW);
digitalWrite(motor_pin_5, LOW);
break;
case 8: // 10101
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, LOW);
digitalWrite(motor_pin_3, HIGH);
digitalWrite(motor_pin_4, LOW);
digitalWrite(motor_pin_5, HIGH);
break;
case 9: // 00101
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, LOW);
digitalWrite(motor_pin_3, HIGH);
digitalWrite(motor_pin_4, LOW);
digitalWrite(motor_pin_5, HIGH);
break;
}
}
}
/*
version() returns the version of the library:
*/
int Stepper::version(void)
{
return 5;
}
Stepper.h
/*
* Stepper.h - Stepper library for Wiring/Arduino - Version 1.1.0
*
* Original library (0.1) by Tom Igoe.
* Two-wire modifications (0.2) by Sebastian Gassner
* Combination version (0.3) by Tom Igoe and David Mellis
* Bug fix for four-wire (0.4) by Tom Igoe, bug fix from Noah Shibley
* High-speed stepping mod by Eugene Kozlenko
* Timer rollover fix by Eugene Kozlenko
* Five phase five wire (1.1.0) by Ryan Orendorff
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*
* Drives a unipolar, bipolar, or five phase stepper motor.
*
* When wiring multiple stepper motors to a microcontroller, you quickly run
* out of output pins, with each motor requiring 4 connections.
*
* By making use of the fact that at any time two of the four motor coils are
* the inverse of the other two, the number of control connections can be
* reduced from 4 to 2 for the unipolar and bipolar motors.
*
* A slightly modified circuit around a Darlington transistor array or an
* L293 H-bridge connects to only 2 microcontroler pins, inverts the signals
* received, and delivers the 4 (2 plus 2 inverted ones) output signals
* required for driving a stepper motor. Similarly the Arduino motor shields
* 2 direction pins may be used.
*
* The sequence of control signals for 5 phase, 5 control wires is as follows:
*
* Step C0 C1 C2 C3 C4
* 1 0 1 1 0 1
* 2 0 1 0 0 1
* 3 0 1 0 1 1
* 4 0 1 0 1 0
* 5 1 1 0 1 0
* 6 1 0 0 1 0
* 7 1 0 1 1 0
* 8 1 0 1 0 0
* 9 1 0 1 0 1
* 10 0 0 1 0 1
*
* The sequence of control signals for 4 control wires is as follows:
*
* Step C0 C1 C2 C3
* 1 1 0 1 0
* 2 0 1 1 0
* 3 0 1 0 1
* 4 1 0 0 1
*
* The sequence of controls signals for 2 control wires is as follows
* (columns C1 and C2 from above):
*
* Step C0 C1
* 1 0 1
* 2 1 1
* 3 1 0
* 4 0 0
*
* The circuits can be found at
*
* http://www.arduino.cc/en/Tutorial/Stepper
*/
// ensure this library description is only included once
#ifndef Stepper_h
#define Stepper_h
// library interface description
class Stepper {
public:
// constructors:
Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2);
Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2,
int motor_pin_3, int motor_pin_4);
Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2,
int motor_pin_3, int motor_pin_4,
int motor_pin_5);
// speed setter method:
void setSpeed(long whatSpeed);
// mover method:
void step(int number_of_steps);
int version(void);
private:
void stepMotor(int this_step);
int direction; // Direction of rotation
unsigned long step_delay; // delay between steps, in ms, based on speed
int number_of_steps; // total number of steps this motor can take
int pin_count; // how many pins are in use.
int step_number; // which step the motor is on
// motor pin numbers:
int motor_pin_1;
int motor_pin_2;
int motor_pin_3;
int motor_pin_4;
int motor_pin_5; // Only 5 phase motor
unsigned long last_step_time; // time stamp in us of when the last step was taken
};
#endif