Peristaltic Doser - Doser set amount over 24hr period

Ok, I'm going to try and address a few things here:

Again, you're still not using any function arguments, so why bother creating them?

I assume you're referring to this statement - $.getJSON(requestURL, function(json)
I tried to remove the function, but I continued to get errors, so reverted to what worked.

Having the deviceID and accesstoken inside every function is redundant, and could be handles as a global variable.

I changed this, they are now a global variables.

What exactly is wrong with the example I provided you here, that made you go "ah, nah, I'll use JQuery instead"?

The example you provided worked as expected. I was having problems understanding how to get that example to return the data back to the html page. I'm pretty sure it needs to use JSON, and I was having a hard time find JSON examples. I read through this but I couldn't relate their examples to what I need.

As for the to do list, here it is:
Doser To Do List

1. Manual ON/OFF
2. Calibrate 60 Seconds ON
3. Refresh Current Settings
4. setupChannels with Set Button
Listed in order of string command
- Channel#
- time (convert seconds to 24hr TOD)
- dose rate (determined through calibration = total amount dosed divided by 60)
- total dose per 24hr period
5. Optional - In Order of Priority
-Current programming is set for 12 doses over 24hr period. Would like select field to change that for options       between 1 - 24 doses per 24hr period.
-CSS for cleaner visuals
-Support for stepper motors
-HTML formatting for mobile devices
6. Pumps - Give customer multiple vendors for different grades of pumps.
Options:
-High Quality / Low Quality
-AC & DC
-Stepper
-Variable speed
7. Enclosures
8. Power Supply
9. Thorough writeup for ā€œHow Toā€ with vendor list and cost breakdown.

Nope, that one actually does use the argument, which is the one in the line above:

requestURL = "https://api.particle.io/v1/devices/" + deviceID + "/" + varName + "/?access_token=" + accessToken;

How does the code you made display it to the HTML, and why would that be any different from my example? I made it output the data to the console since I didn't feel like creating a bunch of extra HTML which you'd have to change anyhow.

JSON is just a way of formatting a string of data, nothing more, nothing less. It's no black magic.
In fact, you're also using JSON in your version by doing

function(json) {
  document.getElementById("r2").innerHTML = json.result;
}

That json object is the response from the variable request, and looks like this:

{
  "cmd": "VarReturn",
  "name": "temp",
  "result": 20.799999237060547,
  "coreInfo": {
    "last_app": "",
    "last_heard": "2016-04-06T15:08:35.026Z",
    "connected": true,
    "last_handshake_at": "2016-04-06T13:23:42.004Z",
    "deviceID": "50ff6c065yadayada6580487",
    "product_id": 0
  }
}

So that whole thing can be addressed as json. If you want any of the key:value pairs, you use the selector. Say you want the name, then you'd use json.name, for the result you'd use json.result, which is exactly what you did.

Now, compare that to my version, and spot the differences/similarities...

function(data) {
  console.log('Function called succesfully:', data);
  console.log(data.body.return_value);
}

Alright, Iā€™m down to something stupid nowā€¦not that the rest of my questions havenā€™t been stupidā€¦sorry guys. :disappointed_relieved:

I have the code below returning the proper results for the corresponding button, but clearly something in the ā€œinnerHTML=data.body.resultā€ is wrongā€¦or at least thatā€™s my suspicion. If I click Relay1, I get the results for relay1 next to each button, if I click Relay2 I get all the relay2 results next to each buttonā€¦and so on. Here is a screen shot:

It should look more like this:

Here is the current code Iā€™m using:

<html>
  <head>
    <title>Particle Javascript Example</title>
    
    <script type="text/javascript" src="http://cdn.jsdelivr.net/particle-api-js/5/particle.min.js"></script>
  </head>

  <body>
  <br>
    <span id="r1"></span>
    <button type="button" onclick="variableGet('getRelay1')">Relay1</button><br>
    <span id="r2"></span>
    <button type="button" onclick="variableGet('getRelay2')">Relay2</button><br>
    <span id="r3"></span>
    <button type="button" onclick="variableGet('getRelay3')">Relay3</button><br>
    <span id="r4"></span>
    <button type="button" onclick="variableGet('getRelay4')">Relay3</button><br>
 
  
    <script>
      var particle = new Particle();
      
      var token;
      var ActualDeviceID = "35002400094734343231xxxx";      

      function variableGet(variableName){
        var vrGr = particle.getVariable({ deviceId: ActualDeviceID, name: variableName, auth: token })
        
        vrGr.then(
          function(data) {
            document.getElementById("r1").innerHTML = data.body.result
          }
        );
        vrGr.then(
          function(data) {
            document.getElementById("r2").innerHTML = data.body.result
          }
        );
        vrGr.then(
          function(data) {
            document.getElementById("r3").innerHTML = data.body.result
      }
      );
          vrGr.then(
          function(data) {
            document.getElementById("r4").innerHTML = data.body.result
      }
      );
      }

      particle.login({username: 'xxx@xxx.net', password: 'xxxx'}).then(
        function(data){
          console.log('API call completed on promise resolve: ', data.body.access_token);
          token = data.body.access_token;
        },
        function(err) {
          console.log('API call completed on promise fail: ', err);
        }
      );
    
    </script>
  </body>
</html>

Yes it is ā€¦

Why should it?
I guess if you just looked at your own code and did your own homework you might come to the same conclusion.

I have seen the reason in the code you posted further up, but we are not there to stop you from learning to find this yourself.
You would not even have to browse through several posts and guess what your current code is, since you have the current code right there at your desk - we only have the info scattered all over several threads.

The code is fine, but the logic is wrongā€¦

int functionNumber = 1;

if (functionNumber == 1){
  function1();
  function2();
  function3();
  function4();
}

If I were to run that code, which of the four functions would get called?

I'd say function1();

But I feel like this is a trick question....

Why would you think function1()?

This is the way I read it.

We started here - int functionNumber = 1;
and if the number =1, then call function1();

Okayyā€¦
At this point Iā€™m starting to feel like you should really pick up some books/tutorials about the very very basics of programming. If youā€™re planning on turning this into a kit, and struggle with things like this, Iā€™m afraid youā€™re going to run into a whole lot more issues.

Letā€™s try this:

int numberOfPizzas = 7;

if (numberOfPizzas > 3){
  addCheese();
  addHam();
  putItInTheOven();
  TakeItOutTheOven();
  CutIt();
  EatIt();
}

What would happen?

1 Like

To be honest, youā€™re right, I do need tutorials, I just havenā€™t found what I feel is a good source. Iā€™ve never denied knowing this stuff, I do it as a hobby because I enjoy the challengeā€¦what I donā€™t enjoy is frustrating other people. I desire to learn and the reason I go to forums is because I like to get explanations based on the projects I am doing.

Anyway, hereā€™s how I see thisā€¦(iā€™m not going to write in codeā€¦)
number of pizzas = 7, if the 7 (number of pizzas) is great than 3ā€¦which is true, then we would add all 6 items.

I've had to teach it myself as well, as a hobby. I still don't know a lot, but I find it much more useful to start of small, and then gradually increase. You have to learn how to walk before you can run.

With that in mind, let's try this one again:

int functionNumber = 1;

if (functionNumber == 1){
  function1();
  function2();
  function3();
  function4();
}

And try to explain why it does what you think it does. The thought is much more interesting than the answer.

I agree, and thank you for taking the time.

The functionNumber is 1, and IF (and only IF) the functionNumber (1) is equal to 1 then call four items.

So you agree that what's in the conditional statement has nothing to do with what's inside the function?
If so, then what does this function do?

function variableGet(variableName){
  var vrGr = particle.getVariable({ deviceId: ActualDeviceID, name: variableName, auth: token })

  vrGr.then(
    function(data) {
      console.log("1");
    }
  );
  vrGr.then(
    function(data) {
      console.log("2");
    }
  );
  vrGr.then(
    function(data) {
      console.log("3");
    }
  );
  vrGr.then(
    function(data) {
      console.log("4");
    }
  );
}

What would I see if I checked the console?

Since everything in this statement is true -

var vrGr = particle.getVariable({ deviceId: ActualDeviceID, name: variableName, auth: token })

I should see the 1,2,3,4 in the console.

Though the answer is correct, the reasoning is a bit different here.
The variable call uses promises. Something like ā€œIā€™ll be doing my thing, but Iā€™ll let you know when Iā€™m doneā€, so you can do other things in the meantime. To react to that you set up these then parts. So, if vrGr is done, then do the following. Because you registered four thens to the same variable call, theyā€™ll all be activated, regardless of which variable you called.
How could you differentiate between variables, and have it only print ā€œ2ā€ for example? Letā€™s say you call variableGet('pizza'), and you want it to display ā€œ2ā€, and when you call variableGet('burger'); it displays ā€œ4ā€?

Iā€™m not exactly sure what the answer is, but there has to be something that says, in laymanā€™s termsā€¦if the variable equals ā€œpizzaā€ then display 2, if the variable equals burger, then display 4.

Where what I currently have is, if the variable equals pizza, then display 1,2,3,4 because there is nothing that is telling the code to stop at the first ā€œthenā€ statement. Iā€™m saying do this, and this and this and this.

Exactly! Now we're getting somewhere :smile:

if (functionNumber == 1){
function1();
function2();
function3();
function4();
}

Spot the similarities :wink:

Absolutely! This certainly helps and this is what Iā€™ve been looking for by coming here.

Now, as far as stopping the function at the first ā€œThenā€ statement. I shouldnā€™t have to repeat the initial function

ā€˜function variableGet(variableName){
var vrGr = particle.getVariable({ deviceId: ActualDeviceID, name: variableName, auth: token })ā€™

Correct?

Better yet, you only need one then, in which you do the magic :wink:

So hereā€™s where Iā€™m at, I can get the value of each pump, but it only ever appears in the pump 1 html span.

This code always gives me the data on the first line:

      function variableGet(variableName){
        var vrGr = particle.getVariable({ deviceId: ActualDeviceID, name: variableName, auth: token });
        
        vrGr.then(
          function(data) {
            document.getElementById("r1").innerHTML = data.body.result;
          }
          );
      }

This code gives me the info of the button I click on all four lines, but again, its always the same text.

      function variableGet(variableName){
        var vrGr = particle.getVariable({ deviceId: ActualDeviceID, name: variableName, auth: token });
        
        vrGr.then(
          function(data) {
            document.getElementById("r1").innerHTML = data.body.result;
            document.getElementById("r2").innerHTML = data.body.result;
            document.getElementById("r3").innerHTML = data.body.result;
            document.getElementById("r4").innerHTML = data.body.result;
          }
          );
      }

I tried this as well, but I get the same as I got in the first version, I get the proper data, but only ever on the first line:

      function variableGet(variableName){
        var vrGr = particle.getVariable({ deviceId: ActualDeviceID, name: variableName, auth: token });
        
        vrGr.then(
          function(data) {
            document.getElementById("r1").innerHTML = data.body.result
          },
          function(data) {
            document.getElementById("r2").innerHTML = data.body.result
          },
          function(data) {
            document.getElementById("r3").innerHTML = data.body.result
          },
          function(data) {
            document.getElementById("r4").innerHTML = data.body.result
          }
          );

And lastly, I tried this as well, again, I get the proper data, but always on the same line:

      function variableGet(variableName){
          requestURL = "https://api.particle.io/v1/devices/" + ActualDeviceID + "/" + variableGet + "/?access_token=" + token;
            var vrGr = particle.getVariable({ deviceId: ActualDeviceID, name: variableName, auth: token });
        
        vrGr.then(
          function(requestURL) {
            document.getElementById("r1").innerHTML = requestURL.body.result
          },
          function(requestURL) {
            document.getElementById("r2").innerHTML = requestURL.body.result
          },
          function(requestURL) {
            document.getElementById("r3").innerHTML = requestURL.body.result
          },
          function(requestURL) {
            document.getElementById("r4").innerHTML = requestURL.body.result
          }
          );
      }