Hello the Cayenne community. I’m new to Cayenne and I’m working on a small solar tracker project with Arduino UNO and a W5100 Ethernet shield.
the idea being to be able to see in real terms the data provided by my photoresist light sensors and my small solar panel.
my follower must operate in automatic mode via the data coming from the photoresist sensors and also in manual mode where I want to control my two servomotors by their associated widgets.
the two modes are conditioned by the lighting of an LED which is associated with a button on cayenne. when the LED is on the tracker is in automatic mode and tracking the position of the light itself, and when it is off the tracker is in manual mode.
the problem being that, when I switch to manual mode, the control of two servomotors is successful, but when I switch to automatic mode, the follower hardly works because the servos move little by little apart from one second.
And when I comment Cayenne.begin and Cayenne.loop to disconnect me from Cayenne, the automatic mode works very well by following the light. I need your help on this! Note that my two servos are plugged into the arduino PWM port.
here is the arduino code:
#define CAYENNE_PRINT Serial
#include <CayenneMQTTEthernet.h>
#include <Servo.h>
//MQTT credentials
char username[]="47739f30-1e41-11ec-8da3-474359af83d7";
char password[]="45c57473905ac357a70d4368491d2f4df0525590";
char clientID[]="8f654910-1e41-11ec-9f5b-45181495093e";
Servo servo_x; //up-down servomotor
int servoh = 0;
int servohLimitHigh = 170;
int servohLimitLow = 10;
Servo servo_z; //left-right servomotor
int servov = 0;
int servovLimitHigh = 170;
int servovLimitLow = 10;
int topl,topr,botl,botr;
int threshold_value=10;
int Vout;
void setup()
{
Serial.begin(9600);
Cayenne.begin(username, password, clientID);
servo_x.attach(5);
servo_z.attach(6);
pinMode(3,OUTPUT);
digitalWrite(3,HIGH);
}
void loop()
{ topr= analogRead(A2);
topl= analogRead(A3);
botl= analogRead(A4);
botr= analogRead(A5);
Vout=(analogRead(A1) * 5.0) / 1023;
if (digitalRead(3)==LOW){
Serial.println(" Manual-mode");
}
Cayenne.loop();
if (digitalRead(3)==HIGH){
Serial.println(" Automatic-mode");
servoh = servo_x.read();
servov = servo_z.read();
int avgtop = (topr + topl) / 2;
int avgbot = (botr + botl) / 2;
int avgright = (topr + botr) / 2;
int avgleft = (topl + botl) / 2;
int diffhori= avgtop - avgbot;
int diffverti= avgleft - avgright;
/*tracking according to horizontal axis*/
if (abs(diffhori) <= threshold_value)
{
servo_x.write(servoh); //stop the servo up-down
}else {
if (diffhori > threshold_value)
{ Serial.println(" x - 2 ");
servo_x.write(servoh -2); //Clockwise rotation CW
if (servoh > servohLimitHigh)
{
servoh = servohLimitHigh;
}
delay(10);
}else {
servo_x.write(servoh +2); //CCW
if (servoh < servohLimitLow)
{
servoh = servohLimitLow;
}
delay(10);
}
}
/*tracking according to vertical axis*/
if (abs(diffverti) <= threshold_value)
{
servo_z.write(servov); //stop the servo left-right
}else{
if (diffverti > threshold_value)
{
servo_z.write(servov -2); //CW
if (servov > servovLimitHigh)
{
servov = servovLimitHigh;
}
delay(10);
}else{
servo_z.write(servov +2); //CCW
if (servov < servovLimitLow)
{
servov = servovLimitLow;
}
delay(10);
}
}
}
}
// Cayenne Functions
CAYENNE_IN(8){
int value = getValue.asInt();
CAYENNE_LOG("Channel %d, pin %d, value %d", 8, 3, value);
digitalWrite(3,value);
}
CAYENNE_IN(7){ //up-down servo motor
if (digitalRead(3)==HIGH){ //Automatic_modez
}
else{ //Manual_mode
servo_x.write(getValue.asDouble() * 180);
}
}
CAYENNE_IN(6){ //left-right servo motor
if (digitalRead(3)==HIGH){
}
else{
servo_z.write(getValue.asDouble() * 180);
}
}
CAYENNE_OUT(0) { //Current
int current = Vout/10;
Cayenne.virtualWrite(0, current);
Serial.print("Current: ");
Serial.println(current);
}
CAYENNE_OUT(1) { //Voltage
int voltage = Vout * 2;
Cayenne.virtualWrite(1, voltage);
Serial.print("Voltage: ");
Serial.println(voltage);
}
CAYENNE_OUT(2){ //LDR Top-right
Cayenne.virtualWrite(2, topr);
}
CAYENNE_OUT(3){ //LDR Top-left
Cayenne.virtualWrite(3,topl);
}
CAYENNE_OUT(4){ //LDR Bot-left
Cayenne.virtualWrite(4,botl);
}
CAYENNE_OUT(5){ //LDR Bot-right
Cayenne.virtualWrite(5,botr);
}