• Just place all your processing into the anoymous funtion - either inline defined - or outside defined.

    For inline/anonymous, it would look - for channel 0 - like this:

    ads.getADC(0, function(val0) {
      // do calculations specific for channel 0 with read value val0
      // print the result 
    });
    

    A 'better' way would be to define upfront functions for the different - channel specific - haendling which include: receiving, processing, and printing. This allows to re-use the handlers for more than just one channel, if needed.

    var handle_the_A_way(val)  {
      // do calculations specific for A  with read value val
      // print the result
    };
    
    var handle_the_B_way(val)  {
      // do calculations specific for B  with read value val
      // print the result
    };
    

    The 'main' flow of your application will include something like that - logically:

    ads.getADC(0, handle_the_A_way);
    ads.getADC(1, handle_the_A_way};
    ads.getADC(2, handle_the_B_way};
    ads.getADC(3, handle_the_A_way};
    ...
    ...
    ...
    

    I said logically, because a next adsGetADC() invocation can only be issued when the previous one has finished... In other words, the requests have to be enqueued, so that only one after the other is executed. Complexity depends on the reading schedule for each channel. What is for sure, you cannot read faster than a channel can deliver.

    Other postings cover the sequencing of (dependent) callbacks (see squence controller - lines 20..30.

  • Just place all your processing into the anoymous funtion - either inline defined - or outside defined.

    For inline/anonymous, it would look - for channel 0 - like this:

    ads.getADC(0, function(val0) {
      // do calculations specific for channel 0 with read value val0
      // print the result 
    });
    

    A 'better' way would be to define upfront functions for the different - channel specific - haendling which include: receiving, processing, and printing. This allows to re-use the handlers for more than just one channel, if needed.

    var handle_the_A_way(val)  {
      // do calculations specific for A  with read value val
      // print the result
    };
    
    var handle_the_B_way(val)  {
      // do calculations specific for B  with read value val
      // print the result
    };
    

    The 'main' flow of your application will include something like that - logically:

    ads.getADC(0, handle_the_A_way);
    ads.getADC(1, handle_the_A_way);
    ads.getADC(2, handle_the_B_way);
    ads.getADC(3, handle_the_A_way);
    ...
    ...
    ...
    

    I said logically, because a next adsGetADC() can only be issued when the previous one has finished... In other words, the requests have to be enqueued, so that only one after the other is executed. Complexity depends on the reading schedule for each channel. What is for sure, you cannot read faster than a channel can deliver.

    Other postings cover the sequencing of (dependent) callbacks (see squence controller - lines 20..30).

    When all channels have to be read with the same frequency at about the same time, a sequence controller does just fine.

    If the channels have to be read whit a different frequency, it becomes a bit more complex: the sequenc controller has to be extended to handle time dependent evnt list, and the addition of an even has to insert the future event taking into acount when in the future it has to happen, how long it takes to complete. Complexity increases with handling efficiently dependent AND independent events, because independent events can overlap (something can be done between the initiation and the callback of the event in order to be efficient: either just some initiation(s) or complete independent event(s), all depending on the times involved.

  • thank you, got my code construction and can deal with my data ;)

  • is it possible to make for loop with some delay? I want to make an average sum for first few readings from ADS but for loop without delay between following readings pico return only 0 value,
    when with the same code but analogRead function instead of ads read it works fine, maybe ADS need some time to send another value and for loop in espruino is to fast? some debounce kind operation is necessary?

  • @bigplik you can't use a for loop for that kind of thing - because getADC is asynchronous.

    You need to use callbacks, so that you issue the next reading only after you got the last one. For instance:

    var idx = 0;
    var array = new Float32Array(10);
    function getValue(value) {
      array[idx] = value;
      idx++;
      if (idx<array.length) 
        ads.getADC(0, getValue);
    } 
    
    ads.getADC(0, getValue);
    
    
  • ...and for the processing you have to apply the 'delay pattern' to 'wait until all readings happened'. Therefore, add something like this after line 7 in code above (note the curly bracket added to the end of line 6):

    var idx = 0;
    var array = new Float32Array(10);
    function getValue(value) {
      array[idx] = value;
      idx++;
      if (idx<array.length) {
        ads.getADC(0, getValue);
      } else {
        // processing of the 10 values, for example, calculating the mean:
        console.log( "Mean of 10 channel 0 values: " 
            + array.reduce(function(s,v){ return s + v; }, 0) / array.length );
        } 
      }
    } 
    ads.getADC(0, getValue);
    

    Mean may not be the right thing to do because there may be values outside expected confidence interval.

    This solves only the ten first readings for channel 0... What about the other channels... and then the subsequent readings at intervals of...

    With all this nesting of callbacks, you end up in callback hell. Most sophisticated solution are Promises... and these aren't the easiest thing to grasp either.

    For a first 'cut' to get all four channels read 10 times and after that every 30 seconds, see next post.

  • The code below reads ten times channel 0..3 one ofter the the other, and after that, all channels one right after the other every 30 seconds.

    To understand the idea behind the implementation: the array's size is used to determine if 10 reads or 1 read has to be done. After all channels have been read ten times, the array is resized to 1, and the code branches into the interval logic.

    var ch = 0;
    var idx = 0;
    var array = new Float32Array(10);
    function getValue(value) {
      array[idx] = value;
      idx++;
      if (idx<array.length) { // keep reading until array full
        ads.getADC(ch, getValue);
      } else { // array full, process it
        if (array.length > 1) { // processing of 10 values
          console.log( "Mean of 10 channel " + ch + " values: " 
              + array.reduce(function(s,v){ return s + v; }, 0) / array.length );
          ch++;
          idx = 0;
          if (ch < 4 ) { // read 10 values for the next channel
            ads.getADC(ch, getValue);
          } else { // all channels done with 10 readings, 
            // switch to 1 reading for each channel every 30 seconds
            array = new Float32Array(1);
            ch = 0;
            setTimeout(function(){ ads.getADC(ch,getValue); }, 30000);
          }
        } else { // processing of 1 value
          console.log( "Channel " + ch + " value: " + array[0]);
          idx = 0;
          ch++;
          if (ch < 4) { // keep going with immediate read for next channel
            ads.getADC(ch, getValue);
          } else { // start over with channel 0 and defer the read for it for 30 secs
            ch = 0;
            setTimeout(function(){ ads.getADC(ch,getValue); }, 30000);
        } 
      }
    } 
    ads.getADC(ch, getValue);
    

    Some refactoring will make the code leaner (with less repetition of ads.getADC(... invocation... etc.)

  • I am afraid it's to hard for me at the moment, don't understand well this array construction

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

ADS1115 analog gain signal module - how to run it with espruino?

Posted by Avatar for bigplik @bigplik

Actions