Error analogWrite() but can't find it

  I can't seem to write to my analog pins but I can't find the error either. Someone who likes to help me look?


    // -----------------------------------
    // Controlling LEDs over the Internet
    // -----------------------------------
    // name the pins
    int led1 = D0;
    int led2 = D1;
    int analog5 = A5;
    int ledOne = 0;
    int analogOne = 0;
    
    // This routine runs only once upon reset
    void setup()
    {
       //Register our Spark function here
       Spark.function("digital", digitalControl);
       Spark.function("analog", analogControl);
    
       // Configure the pins to be outputs
       pinMode(led1, OUTPUT);
       pinMode(led2, OUTPUT);
       pinMode(A7, OUTPUT);
    
       // Initialize both the LEDs to be OFF
       digitalWrite(led1, LOW);
       digitalWrite(led2, LOW);
       analogWrite(analog5, 150);
       
       Spark.variable("ledOne", &ledOne, INT);
       Spark.variable("analogOne", &analogOne, INT);
    }
    
    
    // This routine loops forever 
    void loop()
    {
       ledOne = digitalRead(D0);
       analogOne = analogRead(analog5);
    }
    
    
    // This function gets called whenever there is a matching API request
    // the command string format is l,
    // for example: l1,HIGH or l1,LOW
    //              l2,HIGH or l2,LOW
    
    int digitalControl(String command)
    {
       int state = 0;
       //find out the pin number and convert the ascii to integer
       int pinNumber = (command.charAt(1) - '0') - 1;
       //Sanity check to see if the pin numbers are within limits
       if (pinNumber < 0 || pinNumber > 1) return -1;
    
       // find out the state of the led
       if(command.substring(3,7) == "HIGH") state = 1;
       else if(command.substring(3,6) == "LOW") state = 0;
       else return -1;
    
       // write to the appropriate pin
       digitalWrite(pinNumber, state);  
       return 1;
    }
    
    int analogControl(String setvalue)
    {
       String x = setvalue.substring(2);
       int state = x.toInt();
       
       // write to the appropriate pin
       analogWrite(analog5, state);  
       return 1;
    }

 And this is my javascript with 150 as testvalue.

           var baseURL = "https://api.spark.io/v1/devices/";
            
           function doMethod(method, data) {
                var url = baseURL + "MY-DEVICE-ID" + "/" + method;
                $.ajax({
                type: "POST",
                url: url,
                data: { access_token: "MY-ACCESS-TOKEN", args: data },
                success: function() { $("#result").text("Success"); } ,
                  dataType: "json"
                });
            }
    
           $("#var_one").on('click', '#set_one', function(){
                doMethod("analog", "150");
            });

 It does say Success but the LED doesn't light up with any value whatsoever.... Thanks for the help!

HI @Yannis

It looks like you are trying to read A5 in loop() but write A5 in your Spark.function. Doing the analog read automatically converts the pin to input mode, so whatever value you were writing will only appear very briefly, just until you go through loop again.

Are you having trouble with the digital outputs too? I wasn’t sure how the LEDs figured into it.

Okay thanks @bko

Got no problems with the leds and the digital outputs I can just read out without switching the pinMode. I’ll try to analogWrite/Read D1 and see if that makes any difference of else I’ll just read with another analog pin.

Just used my common sense and realised that it’s quite impossible indeed. Remove the analogRead and it works just fine :smiley:

2 Likes