准备
- UNO
- 舵机
编写程序
在搜索框中找到servo,然后设置参数
然后会出现新的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:
- Install the Servo library from the Arduino Library Manager.
- In the Cayenne Dashboard add a new Custom Widget, and select Slider.
- Select a Virtual pin number.
- Set min value to 0 and max value of 1.
- Set SERVO_VIRTUAL_PIN to the pin number you selected.
- Connect the Servo's legs to GND, VCC (5.0), and a Digital pin.
- Set SERVO_DIGITAL_PIN to the pin number you selected.
- Set the token variable to match the Arduino token from the Dashboard.
- Compile and upload this sketch.
- 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就可以了
然后把舵机添加到面板上。
记得重新运行脚本程序,不出意外的话就可以实现控制了。
从控制到舵机执行会有一点延时,无法实现实时的操作。
选择引脚的时候选择虚拟引脚,具体实际舵机的引脚在Arduino的程序上确定。