[SOLVED] analogWrite issue

Wondering if I could get some advice on this issue. I am trying to use the DAC1 Analog output to send a voltage based on a variable I read from Ubidots. I have confirmed that the value is ok from the dashboard, and have made sure to use an int data type. I have tested using a numeral on the analog write command (4095) and it works fine. I only have the issue when I am trying to use a variable value:

void loop() {
    int value1 = analogRead(A0);
    int value2 = analogRead(A1);
    int value3 = analogRead(A2);
    int Analog3 = ubidots.getValue(VAR_ID);
    // analogWrite(A3, 4095); Works fine.
    analogWrite(A3, Analog3);
    ubidots.add("A0", value1);  // Change for your variable name
    ubidots.add("A1", value2);
    ubidots.add("A2", Analog3);
    ubidots.sendAll();
    
    //if(Analog3!=NULL){
    //Serial.println(Analog3);
    //}
    
    delay(5000);

Any help is appreciated.

@vtrufus,

Q. What version of Particle firmware are you running?

A simple test - add these lines:

static int value4 = 0;
Serial.printlnf("value4=%d", value4);
analogWrite(A3, value4);
value4 = value + 100;

You should see the voltage ramp up every 5 seconds. Is this what you see?

Thanks for the reply.

I am using the 0.6.3 firmware. The sample code you gave me worked. It steps up in brightness. Had to fix the increment line. value4=value4+100;

I then changed the code to the variable I am reading from Ubidots, and update via the Serial.printlnf(“value4=%d”, value4); line you have above, and it works! Any explanation for this?

@vtrufus,

Glad you spotted the typo re value instead of value4.

Can you provide the code as it is now so that we can analyse “why the difference”?

That’s why I prefer the lazy syntax value4 += 100; :sunglasses:

Here you are, I orignially tried the Serial PrintIn from the Ubibots Documentation, but I got an error with it,

#include "Ubidots.h"


/****************************************
 * Define Constants
 ****************************************/

#define TOKEN "InsertTokenHere"  // Put here your Ubidots TOKEN
#define VAR_ID1 "VariableIDFromUbidots"

Ubidots ubidots(TOKEN);

/****************************************
 * Auxiliar Functions
 ****************************************/

//Put here your auxiliar functions


/****************************************
 * Main Functions
 ****************************************/

void setup() {
    pinMode(A3, OUTPUT);
    Serial.begin(115200);
    //ubidots.setDebug(true); //Uncomment this line for printing debug messages
}

void loop() {
    int value4 = ubidots.getValue(VAR_ID1);
    //int Analog3Out = Analog3;
    //analogWrite(A3, 4095);
    //static int value4 = 0;
    Serial.printlnf("value4=%d", value4);
    analogWrite(A3, value4);
    //value4 = value4 + 1000;

    delay(2000);
}

Stating what the error was would help :wink:

Does the Serial.printlnf() statement render the correct value?

I've just tested and for me things work as expected (running 0.7.0-rc.6)

#include "Ubidots.h"

#define TOKEN "..." // "Put here your Ubidots TOKEN"
#define VAR_ID "..."

Ubidots ubidots(TOKEN);

void setup() {
    pinMode(DAC1, OUTPUT);
    pinMode(DAC2, OUTPUT);
}
void loop() {
    float value1 = ubidots.getValue(VAR_ID);
    int x = value1 * 4095; // scale up my test variable

    if(value1!=ERROR_VALUE) {
        analogWrite(DAC1, x);
        analogWrite(DAC2, 4095 - x);
    }
    
    Serial.println(analogRead(A0)); // bridge DAC1 or DAC2 to A0
    
    delay(5000);
}
1 Like

I see the difference. I tried to create an int from the ubidots.getValue command, ie int value1=ubidots.getValue(VAR_ID), by you scaling it on the particle side and converting to an int, it insures the proper datatype and the proper value. I will modify my code to do it this way.

The Serial.printlnf does work.

Thanks for the help.

@vtrufus, I did not realise that ubidots.getValue() returns a float… that explains everything. We should have asked the question “What type does ubidots.getValue() return?” right at the start…

So thanks goes to @ScruffR!

1 Like