# homerun
[架構](https://www.instructables.com/MESOMIX-Automated-Paint-Mixing-Machine/)
[採買清單](https://docs.google.com/spreadsheets/d/1W92vVjgYpxAHwhfL3RxYXUUigrk4m-8GouR7BfJi8IY/edit?usp=sharing)
## 顏料抽取方式
http://www.taichi-maker.com/homepage/reference-index/motor-reference-index/arduino-cnc-shield/
https://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html
[elapsedMillis下載](https://www.arduinolibraries.info/libraries/elapsed-millis)
## 兩個arduino通訊
1.Arduino通訊協議教學 – DevicePlus
https://micro.rohm.com/tw/deviceplus/how-tos/arduino-guide/arduino-communication-protocols-tutorial/
2.Arduino | 序列練習(1):兩個Arduino通訊---使用TX0、RX0 https://hugheschung.blogspot.com/2018/05/arduino-1arduino-tx0rx0.html
3.藍芽
https://swf.com.tw/?p=750
## 顏色感測器
https://www.youtube.com/watch?v=csTQbmTgGIs
shopee:https://shopee.tw/WS2812B-8%E4%BD%8D%E7%9B%B4%E6%A2%9D-RGB%E6%A8%A1%E7%B5%84-%E6%95%B8%E4%BD%8D%E8%A8%8A%E8%99%9F-%E9%9B%BB%E7%AB%B6%E9%A2%A8%E6%94%B9%E8%A3%9D-%E6%99%BA%E6%85%A7%E7%87%88%E5%85%89%E8%A8%AD%E8%A8%88-256%E8%89%B2-WS2812-LED%E7%87%88%E6%A2%9D-i.656213378.14450489913?sp_atk=4b3e9225-8fc5-4571-bf0e-4bfa558d7237&xptdk=4b3e9225-8fc5-4571-bf0e-4bfa558d7237
### 光敏電阻
1. 連結
[[Arduino範例] 光敏電阻的使用](https://blog.jmaker.com.tw/arduino-photoresistor/)
2. 程式碼
```cpp=
void setup() {
Serial.begin(9600);
pinMode(3,OUTPUT);
}
void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
if(sensorValue < 900){ //當光敏電阻給的值小於基準值。這個基準值依個別情況修改
digitalWrite(3,HIGH); //燈亮
}else{
digitalWrite(3,LOW); //燈滅
}
delay(10);
}
```
## 蝦皮商城
[iCShop](https://shopee.tw/icshop_store)
[宇倉電子](https://shopee.tw/allen_6833)
[link text](https://shopee.tw/goodsforyou2015)
## 購物清單
| 項目 | 數量 | 單價 | 總價 |
| ------------- | ---- | ---- | ---- |
| 細管子 | 1 | 0 | 0 |
| CNC Shield V3 | 1 | 49 | 49 |
| A4988 | 4 | 35 | 140 |
| 馬達 | 4 | 375 | 1500 |
| 電源供應器 | 1 | 349 | 349 |
| 插頭 | 1 | 30 | 30 |
| m3x14螺絲 | 2 | 24 | 48 |
| m4x20螺絲 | 1 | 35 | 35 |
| m4x25螺絲 | 1 | 37 | 37 |
| m3螺帽 | 40 | 1 | 40 |
| m4螺帽 | 44 | 1 | 44 |
| 624軸承 | 24 | 15 | 360 |
| 總價 | | | 2632 |
1. 細管子
2. 針筒
3. 馬達*4
4. a4988*4
5. 螺桿(螺絲)、螺帽
6. 馬達聯軸器
7. CNC Shield V3
```cpp=
#include <AccelStepper.h> //本示例程序使用AccelStepper库
#include <elapsedMillis.h>
// 定义电机控制用常量
const int enablePin = 8; // 使能控制引脚
// A4988连接Arduino引脚号
const int xdirPin = 5; // 方向控制引脚
const int xstepPin = 2; // 步进控制引脚
const int ydirPin = 6; // y方向控制引脚
const int ystepPin = 3; // y步进控制引脚
const int zdirPin = 7; // z方向控制引脚
const int zstepPin = 4; // z步进控制引脚
const int adirPin = 13; // a方向控制引脚
const int astepPin = 12; // a步进控制引脚
AccelStepper stepper1(1, xstepPin, xdirPin); //建立步进电机对象
AccelStepper stepper2(1, ystepPin, ydirPin); //建立步进电机对象
AccelStepper stepper3(1, zstepPin, zdirPin); //建立步进电机对象
AccelStepper stepper4(1, astepPin, adirPin); //建立步进电机对象
elapsedMillis ptime = 0;
void setup() {
Serial.begin(115200);
pinMode(enablePin, OUTPUT); // Arduino控制A4988使能引脚为输出模式
pinMode(xstepPin, OUTPUT); // Arduino控制A4988步进引脚为输出模式
pinMode(xdirPin, OUTPUT); // Arduino控制A4988方向引脚为输出模式
pinMode(ystepPin, OUTPUT); // Arduino控制A4988步进引脚为输出模式
pinMode(ydirPin, OUTPUT); // Arduino控制A4988方向引脚为输出模式
pinMode(zstepPin, OUTPUT); // Arduino控制A4988步进引脚为输出模式
pinMode(zdirPin, OUTPUT); // Arduino控制A4988方向引脚为输出模式
pinMode(astepPin, OUTPUT); // Arduino控制A4988步进引脚为输出模式
pinMode(adirPin, OUTPUT); // Arduino控制A4988方向引脚为输出模式
digitalWrite(enablePin, LOW); // 将使能控制引脚设置为低电平从而让
digitalWrite(xdirPin, LOW); // 将使能控制引脚设置为低电平从而让
// 电机驱动板进入工作状态
stepper1.setMaxSpeed(300.0); // 设置电机最大速度300
stepper1.setSpeed(100.0);
stepper2.setMaxSpeed(300.0); // 设置电机最大速度300
stepper2.setSpeed(0.0);
stepper3.setMaxSpeed(300.0); // 设置电机最大速度300
stepper3.setSpeed(0.0);
stepper4.setMaxSpeed(300.0); // 设置电机最大速度300
stepper4.setSpeed(0.0);
// stepper1.setAcceleration(20.0); 设置电机加速度20.0
}
int step1 = 0;
bool dir1 = 0;
void loop() {
// 控制步进电机往复运动
if (ptime >= 1000) {
Serial.print(stepper1.currentPosition());
Serial.println(' ');
if(dir1==0){
digitalWrite(xdirPin, HIGH);
dir1=1;
}
else{
digitalWrite(xdirPin, LOW);
dir1=0;
}
ptime = 0;
}
stepper1.runSpeed(); // 1号电机运行
stepper2.runSpeed(); // 1号电机运行
stepper3.runSpeed(); // 1号电机运行
stepper4.runSpeed(); // 1号电机运行
}
```
```cpp=
#include <AccelStepper.h> //本示例程序使用AccelStepper库
#include <elapsedMillis.h>
// 定义电机控制用常量
const int enablePin = 8; // 使能控制引脚
// A4988连接Arduino引脚号
const int xdirPin = 5; // 方向控制引脚
const int xstepPin = 2; // 步进控制引脚
const int ydirPin = 6; // y方向控制引脚
const int ystepPin = 3; // y步进控制引脚
const int zdirPin = 7; // z方向控制引脚
const int zstepPin = 4; // z步进控制引脚
const int adirPin = 13; // a方向控制引脚
const int astepPin = 12; // a步进控制引脚
AccelStepper stepper1(1, xstepPin, xdirPin); //建立步进电机对象
AccelStepper stepper2(1, ystepPin, ydirPin); //建立步进电机对象
AccelStepper stepper3(1, zstepPin, zdirPin); //建立步进电机对象
AccelStepper stepper4(1, astepPin, adirPin); //建立步进电机对象
elapsedMillis ptime = 0;
void setup() {
Serial.begin(115200);
pinMode(enablePin, OUTPUT); // Arduino控制A4988使能引脚为输出模式
pinMode(xstepPin, OUTPUT); // Arduino控制A4988步进引脚为输出模式
pinMode(xdirPin, OUTPUT); // Arduino控制A4988方向引脚为输出模式
pinMode(ystepPin, OUTPUT); // Arduino控制A4988步进引脚为输出模式
pinMode(ydirPin, OUTPUT); // Arduino控制A4988方向引脚为输出模式
pinMode(zstepPin, OUTPUT); // Arduino控制A4988步进引脚为输出模式
pinMode(zdirPin, OUTPUT); // Arduino控制A4988方向引脚为输出模式
pinMode(astepPin, OUTPUT); // Arduino控制A4988步进引脚为输出模式
pinMode(adirPin, OUTPUT); // Arduino控制A4988方向引脚为输出模式
digitalWrite(enablePin, LOW); // 将使能控制引脚设置为低电平从而让
// 电机驱动板进入工作状态
stepper1.setMaxSpeed(300.0); // 设置电机最大速度300
stepper1.setSpeed(150.0);
stepper2.setMaxSpeed(300.0); // 设置电机最大速度300
stepper2.setSpeed(0.0);
stepper3.setMaxSpeed(300.0); // 设置电机最大速度300
stepper3.setSpeed(0.0);
stepper4.setMaxSpeed(300.0); // 设置电机最大速度300
stepper4.setSpeed(0.0);
// stepper1.setAcceleration(20.0); 设置电机加速度20.0
}
int step1 = 0;
void loop() {
// 控制步进电机往复运动
// if (ptime >= 1000){
// Serial.print(stepper1.currentPosition());
// Serial.print(' ');
// Serial.print(stepper2.currentPosition());
// Serial.print(' ');
// Serial.println(stepper3.currentPosition());
// ptime=0;
// }
if(stepper1.currentPosition()>=200){
stepper1.setSpeed(0.0);
stepper1.setCurrentPosition(0);
stepper2.setSpeed(150.0);
}
if(stepper2.currentPosition()>=200){
stepper2.setSpeed(0.0);
stepper2.setCurrentPosition(0);
stepper3.setSpeed(150.0);
}
if(stepper3.currentPosition()>=200){
stepper3.setSpeed(0.0);
stepper3.setCurrentPosition(0);
stepper4.setSpeed(150.0);
}
if(stepper4.currentPosition()>=200){
stepper4.setSpeed(0.0);
stepper4.setCurrentPosition(0);
stepper1.setSpeed(150.0);
}
stepper1.runSpeed(); // 1号电机运行
stepper2.runSpeed(); // 1号电机运行
stepper3.runSpeed(); // 1号电机运行
stepper4.runSpeed(); // 1号电机运行
}
```
```cpp=
#include <AccelStepper.h> //本示例程序使用AccelStepper库
// 定义电机控制用常量
const int enablePin = 8; // 使能控制引脚
// A4988连接Arduino引脚号
const int xdirPin = 5; // 方向控制引脚
const int xstepPin = 2; // 步进控制引脚
const int ydirPin = 6; // y方向控制引脚
const int ystepPin = 3; // y步进控制引脚
const int zdirPin = 7; // z方向控制引脚
const int zstepPin = 4; // z步进控制引脚
const int adirPin = 13; // a方向控制引脚
const int astepPin = 12; // a步进控制引脚
AccelStepper stepper1(1, xstepPin, xdirPin); //建立步进电机对象
AccelStepper stepper2(1, ystepPin, ydirPin); //建立步进电机对象
AccelStepper stepper3(1, zstepPin, zdirPin); //建立步进电机对象
AccelStepper stepper4(1, astepPin, adirPin); //建立步进电机对象
//elapsedMillis ptime = 0;
void setup() {
Serial.begin(115200);
pinMode(enablePin, OUTPUT); // Arduino控制A4988使能引脚为输出模式
pinMode(xstepPin, OUTPUT); // Arduino控制A4988步进引脚为输出模式
pinMode(xdirPin, OUTPUT); // Arduino控制A4988方向引脚为输出模式
pinMode(ystepPin, OUTPUT); // Arduino控制A4988步进引脚为输出模式
pinMode(ydirPin, OUTPUT); // Arduino控制A4988方向引脚为输出模式
pinMode(zstepPin, OUTPUT); // Arduino控制A4988步进引脚为输出模式
pinMode(zdirPin, OUTPUT); // Arduino控制A4988方向引脚为输出模式
pinMode(astepPin, OUTPUT); // Arduino控制A4988步进引脚为输出模式
pinMode(adirPin, OUTPUT); // Arduino控制A4988方向引脚为输出模式
digitalWrite(enablePin, LOW); // 将使能控制引脚设置为低电平从而让
digitalWrite(xdirPin, LOW); // 将使能控制引脚设置为低电平从而让
// 电机驱动板进入工作状态
stepper1.setMaxSpeed(300.0); // 设置电机最大速度300
stepper1.setSpeed(100.0);
stepper2.setMaxSpeed(300.0); // 设置电机最大速度300
stepper2.setSpeed(0.0);
stepper3.setMaxSpeed(300.0); // 设置电机最大速度300
stepper3.setSpeed(0.0);
stepper4.setMaxSpeed(300.0); // 设置电机最大速度300
stepper4.setSpeed(0.0);
// stepper1.setAcceleration(20.0); 设置电机加速度20.0
}
int step1 = 0;
bool dir1 = 0;
void loop() {
int r,g,b;
char rc,gc,bc;
while(!Serial.available()){};
if(Serial.available()){
rc=Serial.read();
r=Serial.parseInt();
gc=Serial.read();
g=Serial.parseInt();
bc=Serial.read();
b=Serial.parseInt();
float R=r/255,G=g/255,B=b/255;
float k=1-max(max(R,G),B),c=(1-R-k)/(1-k),m=(1-G-k)/(1-k),y=(1-B-k)/(1-k);
Serial.print(rc);
Serial.println(r);
Serial.print('c');
Serial.println(c);
Serial.print(gc);
Serial.println(g);
Serial.print('m');
Serial.println(m);
Serial.print(bc);
Serial.println(b);
Serial.print('y');
Serial.println(y);
Serial.print('k');
Serial.println(k);
}
// 控制步进电机往复运动
// if (ptime >= 1000) {
// Serial.print(stepper1.currentPosition());
// Serial.println(' ');
// if(dir1==0){
// digitalWrite(xdirPin, HIGH);
// dir1=1;
// }
// else{
// digitalWrite(xdirPin, LOW);
// dir1=0;
// }
// ptime = 0;
// }
// stepper1.runSpeed(); // 1号电机运行
// stepper2.runSpeed(); // 1号电机运行
// stepper3.runSpeed(); // 1号电机运行
// stepper4.runSpeed(); // 1号电机运行
}
```
## 馬達端主程式
```cpp=
#include <AccelStepper.h>
// 馬達腳位常數
const int enablePin = 8; // 使能控制引腳
const int xdirPin = 5; // 方向控制引腳
const int xstepPin = 2; // 步進控制引腳
const int ydirPin = 6; // y方向控制引腳
const int ystepPin = 3; // y步進控制引腳
const int zdirPin = 7; // z方向控制引腳
const int zstepPin = 4; // z步進控制引腳
const int adirPin = 13; // a方向控制引腳
const int astepPin = 12; // a步進控制引腳
// 步進馬達變數
AccelStepper stepper1(1, xstepPin, xdirPin);
AccelStepper stepper2(1, ystepPin, ydirPin);
AccelStepper stepper3(1, zstepPin, zdirPin);
AccelStepper stepper4(1, astepPin, adirPin);
void setup() {
Serial.begin(115200);
pinMode(enablePin, OUTPUT);
pinMode(xstepPin, OUTPUT);
pinMode(xdirPin, OUTPUT);
pinMode(ystepPin, OUTPUT);
pinMode(ydirPin, OUTPUT);
pinMode(zstepPin, OUTPUT);
pinMode(zdirPin, OUTPUT);
pinMode(astepPin, OUTPUT);
pinMode(adirPin, OUTPUT);
digitalWrite(enablePin, LOW); //將控制引腳設為低電位
// 設置馬達最大速度300、初始速度0
stepper1.setMaxSpeed(300.0);
stepper1.setSpeed(0.0);
stepper2.setMaxSpeed(300.0);
stepper2.setSpeed(0.0);
stepper3.setMaxSpeed(300.0);
stepper3.setSpeed(0.0);
stepper4.setMaxSpeed(300.0);
stepper4.setSpeed(0.0);
}
double motorspeed = -150.0;
bool getsi = 0;
int circ, cirm, ciry, cirk;
void loop() {
int r, g, b, s;
char rc, gc, bc, pc, sc;
// 接收主端數值
while (!Serial.available()) {};
if (Serial.available()) {
rc = Serial.read();
r = Serial.parseInt();
gc = Serial.read();
g = Serial.parseInt();
bc = Serial.read();
b = Serial.parseInt();
sc = Serial.read();
s = Serial.parseInt();
if (s == 1)
getsi = 1;
// rgb轉cmyk
double R = r * 1.0 / 255.0, G = g * 1.0 / 255.0, B = b * 1.0 / 255.0;
double M = max(max(R, G), B);
double k = 1.0 - M, c = 1.0 - R * 1.0 / M, m = 1.0 - G * 1.0 / M, y = 1.0 - B * 1.0 / M;
c *= 100.0;
m *= 100.0;
y *= 100.0;
k *= 100.0;
// 輸出數值
Serial.println("rgb code:");
Serial.print("r: ");
Serial.print(rc);
Serial.println(r);
Serial.print("g: ");
Serial.print(gc);
Serial.println(g);
Serial.print("b: ");
Serial.print(bc);
Serial.println(b);
Serial.println("cmyk code:");
Serial.print("c: ");
Serial.println(c);
Serial.print("m: ");
Serial.println(m);
Serial.print("y: ");
Serial.println(y);
Serial.print("k: ");
Serial.println(k);
Serial.println();
// 馬達運轉
if (getsi == 1) {
double sum = c + y + m + k;
circ = c * 20.0 / sum;
cirm = m * 20.0 / sum;
ciry = y * 20.0 / sum;
cirk = k * 20.0 / sum;
stepper1.setCurrentPosition(0);
while (stepper1.currentPosition() >= -200 * circ) {
stepper1.setSpeed(motorspeed);
stepper1.runSpeed();
}
stepper1.setSpeed(0.0);
stepper2.setCurrentPosition(0);
while (stepper2.currentPosition() >= -200 * cirm) {
stepper2.setSpeed(motorspeed);
stepper2.runSpeed();
}
stepper2.setSpeed(0.0);
stepper3.setCurrentPosition(0);
while (stepper3.currentPosition() >= -200 * ciry) {
stepper3.setSpeed(motorspeed);
stepper3.runSpeed();
}
stepper3.setSpeed(0.0);
stepper4.setCurrentPosition(0);
while (stepper4.currentPosition() >= -200 * cirk) {
stepper4.setSpeed(motorspeed);
stepper4.runSpeed();
}
stepper4.setSpeed(0.0);
}
getsi = 0;
}
}
```
## 感應器端主程式
```cpp=
#include <FastLED.h>
#define NUM_LEDS 1 //燈條數量 6顆燈
#define DATA_PIN 7 //燈條腳位
CRGB leds[NUM_LEDS];
CRGB ledshow[NUM_LEDS];
//存入顏色的陣列
int Red[1];
int Green[1];
int Blue[1];
int lightTime = 100; //點亮LED的時間
//設定6顆燈RGB的黑白基準值
int whiteR[1];
int whiteG[1];
int whiteB[1];
int blackR[1];
int blackG[1];
int blackB[1];
void setup() {
Serial.begin(115200);
LEDS.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
LEDS.addLeds<WS2812B, 8, RGB>(ledshow, NUM_LEDS);
FastLED.setBrightness(255);
pinMode(2, INPUT_PULLUP); //黑色基準值的按鈕
pinMode(3, INPUT_PULLUP); //白色基準值的按鈕
pinMode(4, INPUT_PULLUP); //顏色館子校正
pinMode(5, INPUT_PULLUP); //顏色印出
}
void loop() {
//如果白色基準值的按鈕壓下去
if (digitalRead(3) == LOW) {
//亮燈 將讀取到的數值存入白色基準值
ledG();
delay(lightTime);
for (int i = 0; i < NUM_LEDS; i++)
whiteG[i] = analogRead(A0);
ledR();
delay(lightTime);
for (int i = 0; i < NUM_LEDS; i++)
whiteR[i] = analogRead(A0);
ledB();
delay(lightTime);
for (int i = 0; i < NUM_LEDS; i++)
whiteB[i] = analogRead(A0);
delay(lightTime);
}
//如果黑色基準值的按鈕壓下去
if (digitalRead(2) == LOW) {
//亮燈 將讀取到的數值存入黑色基準值
ledG();
delay(lightTime);
for (int i = 0; i < NUM_LEDS; i++)
blackG[i] = analogRead(A0);
ledR();
delay(lightTime);
for (int i = 0; i < NUM_LEDS; i++)
blackR[i] = analogRead(A0);
ledB();
delay(lightTime);
for (int i = 0; i < NUM_LEDS; i++)
blackB[i] = analogRead(A0);
delay(lightTime);
}
ledR();
delay(lightTime);
for (int i = 0; i < NUM_LEDS; i++)
Red[i] = constrain( map(analogRead(A0), whiteR[i], blackR[i], 255, 0), 0, 255);
ledG();
delay(lightTime);
for (int i = 0; i < NUM_LEDS; i++)
Green[i] = constrain( map(analogRead(A0), whiteG[i], blackG[i] , 255, 0), 0, 255);
ledB();
delay(lightTime);
for (int i = 0; i < NUM_LEDS; i++)
Blue[i] = constrain( map(analogRead(A0) , whiteB[i], blackB[i], 255, 0), 0, 255);
ledOFF(); //關燈
delay(500);
Serial.print('r');
Serial.print(Red[0]);
Serial.print('g');
Serial.print(Blue[0]);
Serial.print('b');
Serial.print(Green[0]);
Serial.print('s');
if (digitalRead(5) == LOW){
Serial.print(1);
}
else
Serial.print(0);
delay(500);
// float r=Red[0]/255,g=Green[0]/255,b=Blue[0]/255;
//float k=1-max(max(r,g),b),c=(1-r-k)/(1-k),m=(1-g-k)/(1-k),y=(1-b-k)/(1-k);
}
//顯示綠色燈
void ledG() {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(255, 0, 0);
}
FastLED.show();
}
//顯示紅色燈
void ledR() {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(0, 255, 0);
}
FastLED.show();
}
//顯示藍色燈
void ledB() {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(0, 0, 255);
}
FastLED.show();
}
//關燈
void ledOFF() {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(0, 0, 0);
}
FastLED.show();
}
```
## 馬達調整
```cpp=
#include <AccelStepper.h> //本示例程序使用AccelStepper库
#include <elapsedMillis.h>
// 定义电机控制用常量
int circ, cirm, ciry, cirk;
const int enablePin = 8; // 使能控制引脚
const int xdirPin = 5; // 方向控制引脚
const int xstepPin = 2; // 步进控制引脚
const int ydirPin = 6; // y方向控制引脚
const int ystepPin = 3; // y步进控制引脚
const int zdirPin = 7; // z方向控制引脚
const int zstepPin = 4; // z步进控制引脚
const int adirPin = 13; // a方向控制引脚
const int astepPin = 12; // a步进控制引脚
AccelStepper stepper1(1, xstepPin, xdirPin); //建立步进电机对象
AccelStepper stepper2(1, ystepPin, ydirPin); //建立步进电机对象
AccelStepper stepper3(1, zstepPin, zdirPin); //建立步进电机对象
AccelStepper stepper4(1, astepPin, adirPin); //建立步进电机对象
elapsedMillis ptime = 0;
void setup() {
Serial.begin(115200);
pinMode(enablePin, OUTPUT); // Arduino控制A4988使能引脚为输出模式
pinMode(xstepPin, OUTPUT); // Arduino控制A4988步进引脚为输出模式
pinMode(xdirPin, OUTPUT); // Arduino控制A4988方向引脚为输出模式
pinMode(ystepPin, OUTPUT); // Arduino控制A4988步进引脚为输出模式
pinMode(ydirPin, OUTPUT); // Arduino控制A4988方向引脚为输出模式
pinMode(zstepPin, OUTPUT); // Arduino控制A4988步进引脚为输出模式
pinMode(zdirPin, OUTPUT); // Arduino控制A4988方向引脚为输出模式
pinMode(astepPin, OUTPUT); // Arduino控制A4988步进引脚为输出模式
pinMode(adirPin, OUTPUT); // Arduino控制A4988方向引脚为输出模式
digitalWrite(enablePin, LOW); // 将使能控制引脚设置为低电平从而让
// 电机驱动板进入工作状态
stepper1.setMaxSpeed(300.0); // 设置电机最大速度300
stepper1.setSpeed(0.0);
stepper2.setMaxSpeed(300.0); // 设置电机最大速度300
stepper2.setSpeed(0.0);
stepper3.setMaxSpeed(300.0); // 设置电机最大速度300
stepper3.setSpeed(0.0);
stepper4.setMaxSpeed(300.0); // 设置电机最大速度300
stepper4.setSpeed(0.0);
// stepper1.setAcceleration(20.0); 设置电机加速度20.0
}
int step1 = 0;
bool getsi = 0;
void loop() {
int cir=Serial.parseInt();
stepper1.setCurrentPosition(0);
if(cir>0){
while(stepper1.currentPosition() <= 200*cir){
stepper1.setSpeed(150.0);
stepper1.runSpeed();
}
}
else{
while(stepper1.currentPosition() >=200*cir){
stepper1.setSpeed(-150.0);
stepper1.runSpeed();
}
}
stepper1.setSpeed(0.0);
}
```