Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've used a <a href="http://download.oracle.com/javase/1.4.2/docs/api/java/util/HashMap.html" rel="nofollow">HashMap</a> to replace the js {} dynamic object instance and floats, but @weberik's port looks more straightforward.</p> <pre><code>void setup() { int numValues = 10; float[] values = new float[numValues]; //Create an empty sample array for(int i = 0 ; i &lt; numValues ; i++) values[i] = random(0.0,100.0); //Populate it with random values. println("mode: " + mode(values)); } float mode(float[] source) { if (source.length == 0) return -1; HashMap&lt;Float,Integer&gt; modeMap = new HashMap&lt;Float,Integer&gt;(); float result = source[0]; int maxCount = 1; for (int i = 0; i &lt; source.length; i++) { float el = source[i]; if (modeMap.get(el) == null) modeMap.put(el,1); else modeMap.put(el,modeMap.get(el)+1); if (modeMap.get(el) &gt; maxCount) { result = el; maxCount = modeMap.get(el); } } return result; } </code></pre> <p>You've mentioned sensor input, so I presume data will be sampled continuously, so values could be stored at a certain interval, then sent to Processing for the mode. Just a wild guess, but isn't she looking to average/smooth out sensor readings a bit? If so, she could cache a few values (say 10) in an array in Arduino and get the average everytime a new values is added:</p> <pre><code>int vals[10]; //Array to store caches values. void setup() { Serial.begin(9600); for (int i=0 ; i &lt; 10 ; i++) vals[i] = 0; //Init with zeroes } void loop() { delay(100); int currentVal = average(analogRead(0)); //Serial.print(currentVal,BYTE); Serial.println(currentVal); } int average(int newVal) { int total = 0; //Used to store the addition of all currently cached values for(int i = 9; i &gt; 0; i--) { //Loop backwards from the one before last to 0 vals[i] = vals[i-1]; //Overwrite the prev. value with the current(shift values in array by 1) total += vals[i]; //Add to total } vals[0] = newVal; //Add the newest value at the start of the array total += vals[0]; //Add that to the total as well return total *= .1; //Get the average (for 10 elemnts) same as total /= 10, but multiplication is faster than division. } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload