使用Cayenne来控制舵机

准备

  • UNO
  • 舵机

编写程序

使用Cayenne来控制舵机_第1张图片
Paste_Image.png

在搜索框中找到servo,然后设置参数

使用Cayenne来控制舵机_第2张图片
Paste_Image.png

然后会出现新的Arduino程序,复制程序内容,我们这里需要做一些调整

、、、
/*
Cayenne Servo Motor Example
This sketch sample file shows how to connect a Servo Motor with Cayenne Library.
For this example we are going to use a slider widget and the servo will move
depending on the slider value.
The Cayenne Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
Steps:

  1. Install the Servo library from the Arduino Library Manager.
  2. In the Cayenne Dashboard add a new Custom Widget, and select Slider.
  3. Select a Virtual pin number.
  4. Set min value to 0 and max value of 1.
  5. Set SERVO_VIRTUAL_PIN to the pin number you selected.
  6. Connect the Servo's legs to GND, VCC (5.0), and a Digital pin.
  7. Set SERVO_DIGITAL_PIN to the pin number you selected.
  8. Set the token variable to match the Arduino token from the Dashboard.
  9. Compile and upload this sketch.
  10. Once the Arduino connects to the Dashboard you can use the slider to change servo position.
    */
    //#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
    // If you're not using the Ethernet W5100 shield, change this to match your connection type. See Communications examples.
    // #include

include

define SERVO_VIRTUAL_PIN 2

define SERVO_DIGITAL_PIN 9

// servo util lib

include

Servo s1;
// a flag to keep track of servos last position
double lastPosition = 0;
// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "oz7c7hb67k";
void setup()
{
// Serial.begin(9600);
Cayenne.begin(token);
s1.attach(SERVO_DIGITAL_PIN);
}
CAYENNE_IN(SERVO_VIRTUAL_PIN)
{
// get value sent from dashboard
double currentValue = getValue.asDouble();
int position = currentValue * 200;
// actually move the servo to the specified position
s1.write(position);
//update last position
lastPosition = currentValue;
}
void loop()
{
Cayenne.run();
}
、、、

做了几个调整

  • 注释了#define CAYENNE_PRINT Serial
  • 把#include 换成了#include
  • 注释了 Serial.begin(9600);
    然后上传程序就可以了,记得在上传的时候要断开终端的脚本程序。按Control和C就可以了
使用Cayenne来控制舵机_第3张图片
Paste_Image.png

然后把舵机添加到面板上。

使用Cayenne来控制舵机_第4张图片
Paste_Image.png

记得重新运行脚本程序,不出意外的话就可以实现控制了。
从控制到舵机执行会有一点延时,无法实现实时的操作。

使用Cayenne来控制舵机_第5张图片
Paste_Image.png

选择引脚的时候选择虚拟引脚,具体实际舵机的引脚在Arduino的程序上确定。

你可能感兴趣的:(使用Cayenne来控制舵机)