How to average?

mewmew
edited January 2016 in Arduino

Hi, I'm completely new to all this and am now lost on how to 'average' my sequence. I have a sequence in png format of the horizon, arduino and an IR sensor. When you walk closer to the sensor the horizon should get bigger and walk away it should eventually just be a thin white line. My problem is that as you walk towards it, the sequence starts to flicker and it also is not completing the sequence. It only goes to about 60% of the walk even though I am standing right in front of it. How do I smooth it out so it transitions from image to image? Thanks for any advice at all you may have :)

CODE:

import processing.serial.*;

import cc.arduino.*;

Arduino arduino;

PImage img[] = new PImage[225];

void setup() { size(1280, 800); smooth(2);

// Prints out the available serial ports. println(Arduino.list());

arduino = new Arduino(this, "COM6", 1500);

for (int i = 0; i < img.length; i++) {

String sa = nf(i, 3);
img[i] = loadImage("horizon_00"+sa+".png");

} }

void draw() { int sensorValue = arduino.analogRead(0);

if (sensorValue > 260 && sensorValue < 630) { int keyFrame = int(map(sensorValue, 260, 630, 0, 224));

println(sensorValue + " " + keyFrame );

image(img[keyFrame], 0, 0);

} }

Tagged:

Answers

  • this line

              int keyFrame = int(map(sensorValue, 260, 630, 0, 224));
    

    gives you probably a fast change of images.

    Instead try to check if the sensorValue is higher or lower than the last one and if so just change keyFrame by 1.

    If the abs of the difference is higher 5 or so (test it) (this makes it more calm), just say keyFrame++; or keyFrame--; (this makes it more calm as well, because keyFrame can't jump)

    void draw() { 
    
        int sensorValue = arduino.analogRead(0);
    
        if (sensorValue > 260 && sensorValue < 630) 
            {
            if (sensorValue > lastSensorValue) 
                {
    
                if (abs(sensorValue-lastSensorValue) > 5 ) 
                    {
                    keyFrame++;            
                    ..............
                    ............
    

    maybe you could allow for a new image only every 3rd frame or 100 millis?

  • Great, thanks for your reply! I'll check it and see how it works out

  • try the above first

    but you could also average the sensorValue over the last 5 frames or 15 frames and only then use keyFrame = int(map(sensorValueAverage, 260, 630, 0, 224));

    sensorValueSum =  sensorValueSum + sensorValue;
    
    sensorValueAverage =  sensorValueSum / sensorValueCounting; 
    
    sensorValueCounting++; 
    
    if (sensorValueCounting==5) {
    
        // update screen and reset data 
        keyFrame = int(map(sensorValueAverage, 260, 630, 0, 224));
    
        sensorValueCounting = 0; 
        sensorValueAverage = 0; 
        sensorValueSum =  0; 
    
    }
    
Sign In or Register to comment.