Servo control?

Looking to drive a standard little servo motor (0 to 180 degrees) using a slider control. Can anyone share a tutorial on how to do this or point me to any reference projects? Google and searching the community projects not helping too much.

Many Thanks,

tett

add a custom slider widget with channel 1 and set minimum position to 0 and maximum to 180 with slider step value to 1.
next add this code:

CAYENNE_IN(1)
{
  int x= getValue.asInt(); 
myservo.write(x); 
}

Looks very easy so I tried in a program but got an error when test compiling.
/Users/tett/Documents/Arduino/Cayenne_Wemos_test/Cayenne_Wemos_test.ino: In function β€˜void loop()’:
Cayenne_Wemos_test:29: error: a function-definition is not allowed here before β€˜{’ token
{
^

And, following is the code in question:

#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include<Servo.h>
Servo servo;

// WiFi network info.
char ssid = β€œβ€;char wifiPassword = β€œβ€;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = β€œβ€;
char password = β€œβ€;
char clientID = β€œβ€;
long lastMillis = 0;int x = 0;

void setup() {
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
servo.attach(2);
}

void loop() {
Cayenne.loop();

CAYENNE_IN(1)
{
int y= getValue.asInt();
myservo.write(y);
}

//Publish data every 1 seconds (1000 milliseconds). Change this value to publish at a different interval.
if (millis() - lastMillis > 100) {
lastMillis = millis();
x = digitalRead(4);
Cayenne.virtualWrite(1, x, β€œdigital_sensor”, β€œd”);

}
}

try this

#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include<Servo.h>
Servo servo;

// WiFi network info.
char ssid[] = β€œβ€;char wifiPassword[] = β€œβ€;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = β€œβ€;
char password[] = β€œβ€;
char clientID[] = β€œβ€;
long lastMillis = 0;int x = 0;

void setup() {
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
servo.attach(2);
}

void loop() {
Cayenne.loop();

//Publish data every 1 seconds (1000 milliseconds). Change this value to publish at a different interval.
if (millis() - lastMillis > 100) {
lastMillis = millis();
x = digitalRead(4);
Cayenne.virtualWrite(1, x, β€œdigital_sensor”, β€œd”);

}
}
CAYENNE_IN(1)
{
int y= getValue.asInt();
myservo.write(y);
}

This is working well. Many thnaks for your input.

1 Like