Synching cloud data with the analog value from pot

I am using Arduino MKR1500 with a ADC remote(which outputs value between 0-3.3V). I also have it connected to the cloud via MQTT broker. I am trying to achieve the following:

  1. When I receive fast, slow, on or off, the motor speed will change accordingly (I was able to achieve this)
  2. When I use the analog out from a receiver (and I regulate the output voltage) to change the speed of the motor(I was able to achieve this separately)
  3. when I am trying to combine both of the above requirement, that is cloud and the analog output. I am running into issues. I am using 2 different variables to store the values and when there is a change I copy the values into both the variables, so that it is synchronized. The issue is as follows:
  4. When I am using the analog output receiver and tune it to max voltage that is 2.1V and then send an off command from the cloud, the arduino receives it and then stars changing the output to 1V and then goes back up to 2.1V. the same happens with slow or fast command.

My final goal is that when there is no change in the analog output from the receiver(do not change the output) and same with the cloud. Only when there is a new change-> change the out to the desired value (or depending on the command). Below is my code for reference. Any help is greatly appreciated!

/*
  Adapted from ArduinoMqttClient - WiFi Simple Receive Callback
  This example connects to a MQTT broker and subscribes to a single topic.
  When a message is received it prints the message to the serial monitor,
  it uses the callback functionality of the library.
  This example also publishes a sensor value to a separate topic
  The circuit:
  - Arduino MKR 1000, MKR 1010 or Uno WiFi Rev.2 board
  This example code is in the public domain.
*/

#include <ArduinoMqttClient.h>
#include <MKRNB.h>
#include <FlashStorage.h>

// Please enter your sensitive data in the Secret tab or arduino_secrets.h
// PIN Number
const char PINNUMBER[]     = "";

// MQTT Broker and Port
const char broker[] = "test.mosquitto.org"; //
int        port     = 1883;

// Pub/Sub Topics - Make it something unique since we are using a public broker
const char subscribeTopic[]  = "test";
const char publishTopic[]  = "test";

// To connect with SSL/TLS:
// 1) Change NBClient to NBSSLClient.
// 2) Change port value from 1883 to 8883.
// 3) Change broker value to a server with a known SSL/TLS root certificate
//    flashed in the WiFi module.

NBClient client;
GPRS gprs;
NB nbAccess;
MqttClient mqttClient(client);

// Publish interval
long previousMillis = 0;
long interval = 10000;
String s1 = "fast";
String s2 = "slow";
String s3 = "on";
String s4 = "off";
int var1 = 0;// to store the adc value
FlashStorage(adc_one, int);// stores the ADC value in here
int ADC_out = 5; // declare pin 5 as output pin for controlling motor speed
int ADCvalue; // to determine which step it is at value ranges 0-12
int var2=0;// variable to store the adc stepvalue in the bucket and then will be used to compare the corresponding adc step with the cloud
int ADC_remote=0;// variable to get the actual adc value from the pin A1
int var3=0;// variable to get the adc step value in the function and then will be sent back to the loop
int prev_ADC_value=0;// store the previous value
int compare_ADC_val=0;

void setup() {

  pinMode(LED, OUTPUT); // Set built in LED pinMode

  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // attempt to connect to GSM and GPRS:
  Serial.print("Attempting to connect to GSM and GPRS");
  // connection state
  bool connected = false;

  // After starting the modem with GSM.begin()
  // attach the shield to the GPRS network with the APN, login and password
  while (!connected) {
    if ((nbAccess.begin(PINNUMBER) == NB_READY) &&
        (gprs.attachGPRS() == GPRS_READY)) {
      connected = true;
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("You're connected to the network");
  Serial.println();

  // You can provide a unique client ID, if not set the library uses Arduino-millis()

  // Each client can have a unique client ID
  //mqttClient.setId("1c47cbcb00244968be39188019dab900"); // D: originally commented out

  // You can provide a username and password for authentication
  //mqttClient.setUsernamePassword("hello", "Hello123"); // D: originally commented out

  Serial.print("Attempting to connect to the MQTT broker: ");
  Serial.println(broker);

  if (!mqttClient.connect(broker, port)) {
    Serial.print("MQTT connection failed! Error code = ");
    Serial.println(mqttClient.connectError());

    while (1);
  }

  Serial.println("You're connected to the MQTT broker!");
  Serial.println();

  // set the message receive callback
  mqttClient.onMessage(onMqttMessage);

  Serial.print("Subscribing to topic: ");
  Serial.println(subscribeTopic);
  Serial.println();

  // subscribe to a topic
  mqttClient.subscribe(subscribeTopic);

  // topics can be unsubscribed using:
  // mqttClient.unsubscribe(topic);

  Serial.print("Waiting for messages on topic: ");
  Serial.println(subscribeTopic);
  Serial.println();
}

void loop() {
  unsigned long currentMillis = millis();
prev_ADC_value=var2;
  // read the input on analog pin 0:
  ADC_remote = analogRead(A1);
  // print out the value you read:
  Serial.println(ADC_remote);
  var2=ADC_value_remote();
  Serial.println(var2);
  compare_ADC_val= compare_ADC_values();
 // if (var1!=var2)
  {
  if (compare_ADC_val==0)
  {
    Serial.println("Off");
        turn_off();
    ReadFromVariable();
    changeADCvalue();
    }
    else if (compare_ADC_val==1)
  {
    Serial.println("no change");
    }
    else if (compare_ADC_val==2)
  {
    Serial.println("increase speed");
        faster_function();
    changeADCvalue();
    }
    else if (compare_ADC_val==3)
  {
    Serial.println("decrease speed");
        slower_function();
    changeADCvalue();
    }
    else {}
    delay(2000); 
  }
  //else {}}
  // call poll() regularly to allow the library to receive MQTT messages and
  // send MQTT keep alives which avoids being disconnected by the broker
  mqttClient.poll();

  // Enforce Interval
  if (currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;

    // Call a function that handles publishing message
    publishSensorVal();

  }
}

void publishSensorVal() {
  // read the first Analog pin
  int sensorVal = analogRead(0);
  String speak = "The sensor val is " + String(sensorVal);

  // Publish our sensor value to the publish topic
  mqttClient.beginMessage(publishTopic);
  mqttClient.print(speak);
  mqttClient.endMessage();
}

void handleCommand(String cmd) {
  if (cmd == "on") {
    turn_on();
    ReadFromVariable();
    changeADCvalue();
  } else if (cmd == "off") {
    turn_off();
    ReadFromVariable();
    changeADCvalue();
  }
  else if (cmd == "slow") {
    slower_function();
    changeADCvalue();
  }
  else if (cmd == "fast") {
    faster_function();
    changeADCvalue();
  }
}

void onMqttMessage(int messageSize) {
  // we received a message, print out the topic and contents
  Serial.println("Received a message with topic '");
  Serial.print(mqttClient.messageTopic());
  Serial.print("', length ");
  Serial.print(messageSize);
  Serial.println(" bytes:");

  // We want to read each character into a useable String
  String content = "";
  char character;

  // use the Stream interface to print the contents
  while (mqttClient.available()) {
    character = (char)mqttClient.read();
    content.concat(character);
  }

  handleCommand(content); // This function does something with our incoming string
  Serial.println(content);

  Serial.println(); Serial.println(); // double spacing
}


int faster_function()
{
  var1 = adc_one.read(); // D v19 read the value from addr 205 and see if it has any ID already stored in it
  if (var1 < 11)
  {
    var1 = var1 + 1;
  }
  else
  {  }
  adc_one.write(var1); //D v19 store the ID value in the corresponding addr
  var2=var1;
  return var1;
  return var2;
  Serial.print("ADC value: ");
  Serial.println(var1);
  Serial.print("var2: ");
  Serial.println(var2);
}

int turn_on() {
  var1 = adc_one.read();
  if (var1 >= 0)
  {
    var1 = 1;
  }
  else
  {}
  adc_one.write(var1); //D v19 store the ID value in the corresponding addr
  var2=var1;
  return var1;
  return var2;
  Serial.print("ADC value: ");
  Serial.println(var1);
    Serial.print("var2: ");
  Serial.println(var2);
}

int turn_off() {
  var1 = adc_one.read();
  if (var1 > 0)
  {
    var1 = 0;
  }
  else
  {}
  adc_one.write(var1); //D v19 store the ID value in the corresponding addr
  var2=var1;
  return var1;
  return var2;
  Serial.print("ADC value: ");
  Serial.println(var1);
    Serial.print("var2: ");
  Serial.println(var2);
}
int slower_function()
{
  var1 = adc_one.read(); // D v19 read the value from addr 205 and see if it has any ID already stored in it
  if (var1 > 0)
  {
    var1 = var1 - 1;
  }
  else
  {  }
  adc_one.write(var1); //D v19 store the ID value in the corresponding addr
  var2=var1;
  return var1;
  return var2;
  Serial.print("ADC value: ");
  Serial.println(var1);
    Serial.print("var2: ");
  Serial.println(var2);
}

int WriteIntoVariable()
{
  adc_one.write(var1);
  Serial.print("ADC value: ");
  Serial.println(var1);
}

int ReadFromVariable()
{
  var1 = adc_one.read();
  Serial.print("ADC value:" );
  Serial.println(var1);
}

int changeADCvalue()
{
  ADCvalue = adc_one.read();

  if (ADCvalue == 0)
  {
    analogWrite(ADC_out, 0);
  }
  else if (ADCvalue == 1)
  {
    analogWrite(ADC_out, 41);
  }
  else if (ADCvalue == 2)
  {
    analogWrite(ADC_out, 52);
  }
  else if (ADCvalue == 3)
  {
    analogWrite(ADC_out, 65);
  }
  else if (ADCvalue == 4)
  {
    analogWrite(ADC_out, 76);
  }
  else if (ADCvalue == 5)
  {
    analogWrite(ADC_out, 88);
  }
  else if (ADCvalue == 6)
  {
    analogWrite(ADC_out, 99);
  }
  else if (ADCvalue == 7)
  {
    analogWrite(ADC_out, 111);
  }
  else if (ADCvalue == 8)
  {
    analogWrite(ADC_out, 124);
  }
  else if (ADCvalue == 9)
  {
    analogWrite(ADC_out, 134);
  }
  else if (ADCvalue == 10)
  {
    analogWrite(ADC_out, 146);
  }
  else if (ADCvalue == 11)
  {
    analogWrite(ADC_out, 159);
  }
  else
  {
  }
}

int ADC_value_remote()
{
  if (ADC_remote<=60)
  {
    var3=0;
    return var3;
}
else if(ADC_remote <= 140 && ADC_remote >= 61)
{
  var3=1;
    return var3;
  }
  else if(ADC_remote <= 230 && ADC_remote >= 141)
{
  var3=2;
    return var3;
  }
  else if(ADC_remote <= 330 && ADC_remote >= 231)
{
  var3=3;
    return var3;
  }
   else if(ADC_remote <= 420 && ADC_remote >= 331)
{
  var3=4;
    return var3;
  }
   else if(ADC_remote <= 520 && ADC_remote >= 421)
{
  var3=5;
    return var3;
  }
   else if(ADC_remote <= 610 && ADC_remote >= 521)
{
  var3=6;
    return var3;
  }
   else if(ADC_remote <= 700 && ADC_remote >= 611)
{
  var3=7;
    return var3;
  }
   else if(ADC_remote <= 800 && ADC_remote >= 701)
{
  var3=8;
    return var3;
  }
   else if(ADC_remote <= 890 && ADC_remote >= 801)
{
  var3=9;
    return var3;
  }
   else if(ADC_remote <= 990 && ADC_remote >= 891)
{
  var3=10;
    return var3;
  }
   else if(ADC_remote <= 1050 && ADC_remote >= 991)
{
  var3=11;
    return var3;
  }
   else 
{}
}

int compare_ADC_values()
{
  if (var2==0)
  {
    return 0;
    }
    else if (prev_ADC_value== var2)
  {
    return 1;
    }
    else if (prev_ADC_value<var2)
    {
      return 2;
      }
      else
      {
        return 3;
      }
}


I did not understand your description.

I'm unsure if you are talking about a single or two microcontrollers.

You are receiving a value from a MQTT-broker into a variable a
A different variable gets assigned a measuring-value from a potentiometer connected to an analog input
If there is a difference you are equaling those values?
How should this work in parallel with continiously receieving different values?

Makes no sense to me. I guess I did not understand how it should work.

Please post an example with all values named with names that are easy to distinguish from each other and to easy understand.

avoid saying "this value" always repeat clearly namimg the variable and quoting the real value again and again.

Describing it in this way elimiates all room for deviating interpretations of what you mean.

best regards Stefan

Hi Stefan, sorry if it was confusing.
Below is the code for just using the analog pot

/*
  AnalogReadSerial

  Reads an analog input on pin 0, prints the result to the Serial Monitor.
  Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogReadSerial
*/
int var2 = 0; // variable to store the adc stepvalue in the bucket and then will be used to compare the corresponding adc step with the cloud
int ADC_remote = 0; // variable to get the actual adc value from the pin A1
int var3 = 0; // variable to get the adc step value in the function and then will be sent back to the loop
int prev_ADC_value = 0; // store the previous value
int compare_ADC_val = 0;
// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  prev_ADC_value = var2;
  // read the input on analog pin 0:
  ADC_remote = analogRead(A1);
  // print out the value you read:
  Serial.println(ADC_remote);
  var2 = ADC_value_remote();
  Serial.println(var2);
  compare_ADC_val = compare_ADC_values();
  if (compare_ADC_val == 0)
  {
    Serial.println("Off");
  }
  else if (compare_ADC_val == 1)
  {
    Serial.println("no change");
  }
  else if (compare_ADC_val == 2)
  {
    Serial.println("increase speed");
  }
  else if (compare_ADC_val == 3)
  {
    Serial.println("decrease speed");
  }
  else {}

  delay(2000);        // delay in between reads for stability
}

int ADC_value_remote()
{
  if (ADC_remote <= 60)
  {
    var3 = 0;
    return var3;
  }
  else if (ADC_remote <= 140 && ADC_remote >= 61)
  {
    var3 = 1;
    return var3;
  }
  else if (ADC_remote <= 230 && ADC_remote >= 141)
  {
    var3 = 2;
    return var3;
  }
  else if (ADC_remote <= 330 && ADC_remote >= 231)
  {
    var3 = 3;
    return var3;
  }
  else if (ADC_remote <= 420 && ADC_remote >= 331)
  {
    var3 = 4;
    return var3;
  }
  else if (ADC_remote <= 520 && ADC_remote >= 421)
  {
    var3 = 5;
    return var3;
  }
  else if (ADC_remote <= 610 && ADC_remote >= 521)
  {
    var3 = 6;
    return var3;
  }
  else if (ADC_remote <= 700 && ADC_remote >= 611)
  {
    var3 = 7;
    return var3;
  }
  else if (ADC_remote <= 800 && ADC_remote >= 701)
  {
    var3 = 8;
    return var3;
  }
  else if (ADC_remote <= 890 && ADC_remote >= 801)
  {
    var3 = 9;
    return var3;
  }
  else if (ADC_remote <= 990 && ADC_remote >= 891)
  {
    var3 = 10;
    return var3;
  }
  else if (ADC_remote <= 1050 && ADC_remote >= 991)
  {
    var3 = 11;
    return var3;
  }
  else
  {}
}

int compare_ADC_values()
{
  if (var2 == 0)
  {
    return 0;// off
  }
  else if (prev_ADC_value == var2)
  {
    return 1;// no change
  }
  else if (prev_ADC_value < var2)
  {
    return 2;//faster
  }
  else
  {
    return 3;//slower
  }
}

best regards Stefan

Below is the code for just MQTT broker:

/*
  Adapted from ArduinoMqttClient - WiFi Simple Receive Callback
  This example connects to a MQTT broker and subscribes to a single topic.
  When a message is received it prints the message to the serial monitor,
  it uses the callback functionality of the library.
  This example also publishes a sensor value to a separate topic
  The circuit:
  - Arduino MKR 1000, MKR 1010 or Uno WiFi Rev.2 board
  This example code is in the public domain.
*/

#include <ArduinoMqttClient.h>
#include <MKRNB.h>
#include <FlashStorage.h>

// Please enter your sensitive data in the Secret tab or arduino_secrets.h
// PIN Number
const char PINNUMBER[]     = "";

// MQTT Broker and Port
const char broker[] = "test.mosquitto.org"; //
int        port     = 1883;

// Pub/Sub Topics - Make it something unique since we are using a public broker
const char subscribeTopic[]  = "test";
const char publishTopic[]  = "test";

// To connect with SSL/TLS:
// 1) Change NBClient to NBSSLClient.
// 2) Change port value from 1883 to 8883.
// 3) Change broker value to a server with a known SSL/TLS root certificate
//    flashed in the WiFi module.

NBClient client;
GPRS gprs;
NB nbAccess;
MqttClient mqttClient(client);

int LED = 6; // Onboard LED

// Publish interval
long previousMillis = 0;
long interval = 10000;
String s1 = "fast";
String s2 = "slow";
String s3 = "on";
String s4 = "off";
int var1 = 0;// to store the adc value
FlashStorage(adc_one, int);// stores the ADC value in here
int ADC_out = 5; // declare pin 5 as output pin for controlling motor speed
int ADCvalue; // to determine which step it is at value ranges 0-12

void setup() {

  pinMode(LED, OUTPUT); // Set built in LED pinMode

  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // attempt to connect to GSM and GPRS:
  Serial.print("Attempting to connect to GSM and GPRS");
  // connection state
  bool connected = false;

  // After starting the modem with GSM.begin()
  // attach the shield to the GPRS network with the APN, login and password
  while (!connected) {
    if ((nbAccess.begin(PINNUMBER) == NB_READY) &&
        (gprs.attachGPRS() == GPRS_READY)) {
      connected = true;
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("You're connected to the network");
  Serial.println();

  // You can provide a unique client ID, if not set the library uses Arduino-millis()

  // Each client can have a unique client ID
  //mqttClient.setId("1c47cbcb00244968be39188019dab900"); // D: originally commented out

  // You can provide a username and password for authentication
  //mqttClient.setUsernamePassword("hello", "Hello123"); // D: originally commented out

  Serial.print("Attempting to connect to the MQTT broker: ");
  Serial.println(broker);

  if (!mqttClient.connect(broker, port)) {
    Serial.print("MQTT connection failed! Error code = ");
    Serial.println(mqttClient.connectError());

    while (1);
  }

  Serial.println("You're connected to the MQTT broker!");
  Serial.println();

  // set the message receive callback
  mqttClient.onMessage(onMqttMessage);

  Serial.print("Subscribing to topic: ");
  Serial.println(subscribeTopic);
  Serial.println();

  // subscribe to a topic
  mqttClient.subscribe(subscribeTopic);

  // topics can be unsubscribed using:
  // mqttClient.unsubscribe(topic);

  Serial.print("Waiting for messages on topic: ");
  Serial.println(subscribeTopic);
  Serial.println();
}

void loop() {
  unsigned long currentMillis = millis();

  // call poll() regularly to allow the library to receive MQTT messages and
  // send MQTT keep alives which avoids being disconnected by the broker
  mqttClient.poll();

  // Enforce Interval
  if (currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;

    // Call a function that handles publishing message
    publishSensorVal();

  }
}

void publishSensorVal() {
  // read the first Analog pin
  int sensorVal = analogRead(0);
  String speak = "The sensor val is " + String(sensorVal);

  // Publish our sensor value to the publish topic
  mqttClient.beginMessage(publishTopic);
  mqttClient.print(speak);
  mqttClient.endMessage();
}

void handleCommand(String cmd) {
  if (cmd == "on") {
    turn_on();
    ReadFromVariable();
    changeADCvalue();
  } else if (cmd == "off") {
    turn_off();
    ReadFromVariable();
    changeADCvalue();
  }
  else if (cmd == "slow") {
    slower_function();
    changeADCvalue();
  }
  else if (cmd == "fast") {
    faster_function();
    changeADCvalue();
  }
}

void onMqttMessage(int messageSize) {
  // we received a message, print out the topic and contents
  Serial.println("Received a message with topic '");
  Serial.print(mqttClient.messageTopic());
  Serial.print("', length ");
  Serial.print(messageSize);
  Serial.println(" bytes:");

  // We want to read each character into a useable String
  String content = "";
  char character;

  // use the Stream interface to print the contents
  while (mqttClient.available()) {
    character = (char)mqttClient.read();
    content.concat(character);
  }

  handleCommand(content); // This function does something with our incoming string
  Serial.println(content);

  Serial.println(); Serial.println(); // double spacing
}


int faster_function()
{
  var1 = adc_one.read(); // D v19 read the value from addr 205 and see if it has any ID already stored in it
  if (var1 < 11)
  {
    var1 = var1 + 1;
  }
  else
  {
  }
  adc_one.write(var1); //D v19 store the ID value in the corresponding addr
  return var1;
  Serial.print("ADC value: ");
  Serial.println(var1);
}

int turn_on() {
  /*var1=1;
    adc_one.write(var1);
    return var1;
    Serial.print("ADC value: ");
    Serial.println(var1);*/
  var1 = adc_one.read();
  if (var1 >= 0)
  {
    var1 = 1;
  }
  else
  {}
  adc_one.write(var1); //D v19 store the ID value in the corresponding addr
  return var1;
  Serial.print("ADC value: ");
  Serial.println(var1);
}

int turn_off() {
  /* var1=0;
    adc_one.write(var1);
    return var1;
     Serial.print("ADC value: ");
    Serial.println(var1);*/
  var1 = adc_one.read();
  if (var1 > 0)
  {
    var1 = 0;
  }
  else
  {}
  adc_one.write(var1); //D v19 store the ID value in the corresponding addr
  return var1;
  Serial.print("ADC value: ");
  Serial.println(var1);
}
int slower_function()
{
  var1 = adc_one.read(); // D v19 read the value from addr 205 and see if it has any ID already stored in it
  if (var1 > 0)
  {
    var1 = var1 - 1;
  }
  else
  {
  }
  adc_one.write(var1); //D v19 store the ID value in the corresponding addr
  return var1;
  Serial.print("ADC value: ");
  Serial.println(var1);
}

int WriteIntoVariable()
{
  adc_one.write(var1);
  Serial.print("ADC value: ");
  Serial.println(var1);
}

int ReadFromVariable()
{
  var1 = adc_one.read();
  Serial.print("ADC value:" );
  Serial.println(var1);
}

int changeADCvalue()
{
  ADCvalue = adc_one.read();

  if (ADCvalue == 0)
  {
    analogWrite(ADC_out, 0);
  }
  else if (ADCvalue == 1)
  {
    analogWrite(ADC_out, 41);
  }
  else if (ADCvalue == 2)
  {
    analogWrite(ADC_out, 52);
  }
  else if (ADCvalue == 3)
  {
    analogWrite(ADC_out, 65);
  }
  else if (ADCvalue == 4)
  {
    analogWrite(ADC_out, 76);
  }
  else if (ADCvalue == 5)
  {
    analogWrite(ADC_out, 88);
  }
  else if (ADCvalue == 6)
  {
    analogWrite(ADC_out, 99);
  }
  else if (ADCvalue == 7)
  {
    analogWrite(ADC_out, 111);
  }
  else if (ADCvalue == 8)
  {
    analogWrite(ADC_out, 124);
  }
  else if (ADCvalue == 9)
  {
    analogWrite(ADC_out, 134);
  }
  else if (ADCvalue == 10)
  {
    analogWrite(ADC_out, 146);
  }
  else if (ADCvalue == 10)
  {
    analogWrite(ADC_out, 159);
  }
  else
  {

  }
}

Without describing

EXAMPLE-Values

like described above I will not look into your code
best regards Stefan

The way I wanted to work is have 2 variables storing 2 different value(one from the mqtt broker)
For example:

  1. I start by using the pot and set the output to 1.5V which will correspond to var2 value of 5
  2. later I use fast command from the cloud, this should increase the output of the analog pin to 1.65V which will correspond to var1 value 6
  3. The values for both the variable should be updated to 6 here(var and var 2 should be equal to 6)
  4. Then when I press off from mqtt broker the new Analog value output will be 0 and the corresponding variable value var2 is 0.this in turn should set var 1 to be zero too.

The issue I am having is from the code posted in my question, the arduino first reads the pot value sets the variable to 5 and then reads the data from MQTT broker, which changes the variable value to 6 and then goes back to setting it to 5 as it reads the pot value again from the loop.
This leads to the issue where I have the pot output as 1.5V and then I send off command, the arduino first sets the output to 1.5, then brings it down to 1V and immediately reads the pot value and pulls it back to 1.5 and does not let it turn off

Which IO-Pin-number or what else is the "output" you are talking about???
You have to be much more precise what you mean by that to elimante room for interpretation

Which IO-pin-number do you mean?
same here. You have to describe it with the precisision of

Example
later I use the command "fast" received over MQTT this should increase the output of the analog pin with IO-pinnumber "A4" to 1.65V

etc. etc. etc.

best regards Stefan

I am using digital pin 5 (to create PWM values) which is used as an output and I am using analog pin A1 to read the input of the pot.
Digital pin 5 is connected to the ADC of a motor controller to vary the speed of the motor

OK so I'm writing an example myself:

At the beginning:
The MQTT-Arduino receives a command which is the character-sequence "fast" which sets IO-pin5 of the MQTT-Arduino to PWM-value 230

The Potentiometer-Arduino reads in value 300 from Potentiometer-Arduino-IO-PinA1

Over a serial connection between Potentiometer-Arduino and MQTT-Arduino the MQTT-arduino receives the value "300" and assigns it to variable named "receivedPOT "
....
Of course this description is wrong. It shall just show how detailed you should describe the whole thing.

I'm saying explicitly:
I will

not

try to find out all the wiring and which controller does what by trying to analyse your code line by line that is using non-saying variable-names like "var2", "var3" etc.

either you are able to describe it in that grade of details or I will not answer anymore.
best regards Stefan

Addition: You are close to the edge that I will decide to mute.
Of course you are free to wait for other users to help

Hi Stefan, Sorry i am not being clear
Here is more explanation:

  1. When the arduino is turned on the IO pin 5 will have PWM value 0
  2. I change the pot value(which is an input to IO pin A1), it reads analog value of 700. This will make the IO pin 5 to give a corresponding output PWM value of 250.
  3. Now I send a command though MQTT broker to increase the speed by sending "fast" command. This will make the arduino read that command and increase the IO pin 5 output to PWM value of 300.
  4. in the mean time the analog value read by pin A1 is still 700(which has not changed)
  5. since this function is in loop, the variables I am using to output the required PWM value keeps jumping between 250 and 300.
    I want to have a variable which remembers the latest value and based on change in the pot(increase or decrease) or the command that is received from the MQTT broker(fast, slow, on or off) and I would like to change the future values.

I guess you have to make a decision which value will override which other value.

At this point it will be better to describe the functionality with normal words
by

avoiding any programming-terms

best regards Stefan

I guess I am the one in this thread who is posting similar to trolling.
So I'm gonna mute myself.
Sorry for any inconvenience. I am unable to switch to a different style.
So I just stop posting.

No worries Stefan, I know you are trying to help here.
I am using Arduino MKR1500. I am using Mosquitto server as the MQTT broker. The Arduino MKR1500 is the device which is receiving the MQTT messages . The commands/keywords that the arduino is programmed to listen to and react is fast, slow, on and off.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.