Servo control?


#1

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


#2

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); 
}

#3

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
{
^


#4

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”);

}
}


#5

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);
}

#6

This is working well. Many thnaks for your input.