使用Cayenne来控制舵机

准备

  • UNO
  • 舵机

编写程序

Paste_Image.png

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

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 <CayenneEthernet.h>

include <CayenneSerial.h>

define SERVO_VIRTUAL_PIN 2

define SERVO_DIGITAL_PIN 9

// servo util lib

include <Servo.h>

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 <CayenneEthernet.h>换成了#include <CayenneSerial.h>
  • 注释了 Serial.begin(9600);
    然后上传程序就可以了,记得在上传的时候要断开终端的脚本程序。按Control和C就可以了
Paste_Image.png

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

Paste_Image.png

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

Paste_Image.png

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容