接上一篇 WiFi机械臂 自动运行版(原创 文饼)代码是下面
[mw_shl_code=arduino,true]
include <Servo.h>
Servo s1;
Servo s2;
Servo s3;
Servo s4;
Servo s5;
#define BLINKER_WIFI
int s1pos, s2pos, s3pos, s4pos, s5pos;//之前的位置
int32_t s1ppos, s2ppos, s3ppos, s4ppos, s5ppos;//后来的位置
int32_t servo01SP[50], servo02SP[50], servo03SP[50], servo04SP[50], servo05SP[50];; // 用来储存数据
int a = 0;
int speedDelay = 20;
#include <Blinker.h>
char auth[] = "521b80a5b79f";
char ssid[] = "Daniel handsome";
char pswd[] = "15975618512dwb";
#define led1 D0
#define led2 D1
#define led3 D2
BlinkerSlider Slider1("a");
BlinkerSlider Slider2("b");
BlinkerSlider Slider3("c");
BlinkerSlider Slider4("d");
BlinkerSlider Slider5("e");
BlinkerButton Button1("save");
BlinkerButton Button2("run");
BlinkerButton Button3("reset");
BlinkerText text1("step");
BlinkerText text2("step1");
void Button1_callback(const String & state1)
{
BLINKER_LOG("get button state: ", state1);
servo01SP[a] = s1ppos; // 将自己所希望的位置和步骤存储下来 是将当前所在位置存储下来
servo02SP[a] = s2ppos;
servo03SP[a] = s3ppos;
servo04SP[a] = s4ppos;
servo05SP[a] = s5ppos;
a++;
text1.print("step:", a);
}
//button 2 run
void Button2_callback(const String & state2)
{
BLINKER_LOG("get button state: ", state2);
runservo();
}
//button 3 reset
void Button3_callback(const String & state3)
{
BLINKER_LOG("get button state: ", state3);
memset(servo01SP, 0, sizeof(servo01SP)); // Clear the array data to 0
memset(servo02SP, 0, sizeof(servo02SP));
memset(servo03SP, 0, sizeof(servo03SP));
memset(servo04SP, 0, sizeof(servo04SP));
memset(servo05SP, 0, sizeof(servo05SP));
a = 0;
text1.print("reset is ok,step:", a);
digitalWrite(led3, HIGH);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//number 1
void slider1_callback(int32_t value1)
{
BLINKER_LOG("get slider value: ", value1);
s1ppos = value1;
if (s1ppos > s1pos)
{
for (int i1 = s1ppos; i1 >= s1pos; i1--) {
s1.write(i1);
delay(20);
}
}
if (s1ppos < s1pos)
{
for (int i1 = s1ppos; i1 <= s1pos; i1++) {
s1.write(i1);
delay(20);
}
}
s1pos = s1ppos;
}
//number 2
void slider2_callback(int32_t value2)
{
BLINKER_LOG("get slider value: ", value2);
s2ppos = value2;
if (s2ppos > s2pos)
{
for (int i2 = s2ppos; i2 >= s2pos; i2--) {
s2.write(i2);
delay(20);
}
}
if (s2ppos < s2pos)
{
for (int i2 = s2ppos; i2 <= s2pos; i2++) {
s2.write(i2);
delay(20);
}
}
s2pos = s2ppos;
}
void slider3_callback(int32_t value3)
{
BLINKER_LOG("get slider value: ", value3);
s3ppos = value3;
if (s3ppos > s3pos)
{
for (int i3 = s3ppos; i3 >= s3pos; i3--) {
s3.write(i3);
delay(20);
}
}
if (s3ppos < s3pos)
{
for (int i3 = s3ppos; i3 <= s3pos; i3++) {
s1.write(i3);
delay(20);
}
}
s3pos = s3ppos;
}
void slider4_callback(int32_t value4)
{
BLINKER_LOG("get slider value: ", value4);
s4ppos = value4;
if (s4ppos > s4pos)
{
for (int i4 = s4ppos; i4 >= s4pos; i4--) {
s4.write(i4);
delay(20);
}
}
if (s4ppos < s4pos)
{
for (int i4 = s4ppos; i4 <= s4pos; i4++) {
s4.write(i4);
delay(20);
}
}
s4pos = s4ppos;
}
void slider5_callback(int32_t value5)
{
BLINKER_LOG("get slider value: ", value5);
s5ppos = value5;
if (s5ppos > s5pos)
{
for (int i5 = s5ppos; i5 >= s5pos; i5--) {
s5.write(i5);
delay(20);
}
}
if (s5ppos < s5pos)
{
for (int i5 = s5ppos; i5 <= s5pos; i5++) {
s5.write(i5);
delay(20);
}
}
s5pos = s5ppos;
}
////////////////////////////////
void setup()
{
Blinker.begin(auth, ssid, pswd);
pinMode(led2, OUTPUT);
Slider1.attach(slider1_callback);
Slider2.attach(slider2_callback);
Slider3.attach(slider3_callback);
Slider4.attach(slider4_callback);
Slider5.attach(slider5_callback);
Button1.attach(Button1_callback);
Button2.attach(Button2_callback);
Button3.attach(Button3_callback);
s1.attach(D3);
s2.attach(D4);
s3.attach(D5);
s4.attach(D6);
s5.attach(D7);
s1pos = 90;
s1.write(s1pos);
Slider1.print(90);
s2pos = 0;
Slider2.print(0);
s2.write(s2pos);
s3pos = 0;
s3.write(s3pos);
Slider3.print(0);
s4pos = 90;
Slider4.print(90);
s4.write(s4pos);
s5pos = 0;
Slider5.print(0);
s5.write(s5pos);
}
void loop()
{
Blinker.run();
}
/////////////////////////////////////////
void runservo()
{
for (int i = 0; i <= a ; i++) // 遍历所有储存的数据
{
// Servo 1
if (servo01SP == servo01SP[i + 1]) { //如果数据一样不进行操作
}
if (servo01SP > servo01SP[i + 1]) { //如果存储的数据一,比数据二的位置(或者度数)大。那么就在要进行操作
for ( int j = servo01SP; j >= servo01SP[i + 1]; j--) {
s1.write(j);
delay(speedDelay);//这里的原始数据是20
}
}
if (servo01SP < servo01SP[i + 1]) {
for ( int j = servo01SP; j <= servo01SP[i + 1]; j++) {
s1.write(j);
delay(speedDelay);//这里的原始数据是20
}
}
// Servo 2
if (servo02SP == servo02SP[i + 1]) {
}
if (servo02SP > servo02SP[i + 1]) {
for ( int j = servo02SP; j >= servo02SP[i + 1]; j--) {
s2.write(j);
delay(speedDelay);
}
}
if (servo02SP < servo02SP[i + 1]) {
for ( int j = servo02SP; j <= servo02SP[i + 1]; j++) {
s2.write(j);
delay(speedDelay);
}
}
// Servo 3
if (servo03SP == servo03SP[i + 1]) {
}
if (servo03SP > servo03SP[i + 1]) {
for ( int j = servo03SP; j >= servo03SP[i + 1]; j--) {
s3.write(180 - j);
delay(speedDelay);
}
}
if (servo03SP < servo03SP[i + 1]) {
for ( int j = servo03SP; j <= servo03SP[i + 1]; j++) {
s3.write(180 - j);
delay(speedDelay);
}
}
// Servo 4
if (servo04SP == servo04SP[i + 1]) {
}
if (servo04SP > servo04SP[i + 1]) {
for ( int j = servo04SP; j >= servo04SP[i + 1]; j--) {
s4.write(j);
delay(speedDelay);
}
}
if (servo04SP < servo04SP[i + 1]) {
for ( int j = servo04SP; j <= servo04SP[i + 1]; j++) {
s4.write(j);
delay(speedDelay);
}
}
// Servo 5
if (servo05SP == servo05SP[i + 1]) {
}
if (servo05SP > servo05SP[i + 1]) {
for ( int j = servo04SP; j >= servo04SP[i + 1]; j--) {
s5.write(j);
delay(speedDelay);
}
}
if (servo05SP < servo05SP[i + 1]) {
for ( int j = servo04SP; j <= servo04SP[i + 1]; j++) {
s5.write(j);
delay(speedDelay);
}
}
}
}[/mw_shl_code]
功能是储存servo的位置 ,利用runservo函数 遍历servo,达到,自动运行的效果
操作设备和上一篇是一样的
如何有什么想法的,可以顺便告诉我一声吗,
大神们 qq1341180280,谢谢大家啦
转贴 请标明出处
|