Temporarily ignore value from sensor?

Hi Everyone,

I am using a parallax x-band sensor to detect movement and change an image displayed.

Its all working fine but I now need to refine it;

What I need to do is once the motion is detected I want the program to ignore the values from the sensor for a certain amount of time, to prevent the images changing rapidly.

Im just struggling with the code on this one!

its done in processing with the Arduino library, ill post the code so you can see but it will most likely be a nuisance for anyone to run it!

hope someone can help!


import processing.serial.*;
import cc.arduino.*;

Arduino arduino;

//put image names in here ready to be loaded in to array
String[] imageNames = {
  "cheerful.jpeg", "cool.jpeg", "chatterbox.jpeg", "fussy.jpeg"
};

//creates an image array with the amount of field specified by String[]  
PImage[] images = new PImage[imageNames.length];

// counter to select picture
int counter = 0;


int ledPin = 13;


void setup() {
  size(500, 500);
  background(255);
  strokeWeight(5);
  imageMode(CENTER);

  arduino = new Arduino(this, Arduino.list()[11], 57600);
  
  // set pin numbers for each input/output
int sensorDigitalPin = 2;
int sensorAnalogPin = 0;

  // declared the pin mode for each pin
  arduino.pinMode(sensorDigitalPin, arduino.INPUT);
  arduino.pinMode(sensorAnalogPin, arduino.INPUT);
  arduino.pinMode(ledPin, arduino.OUTPUT);

  //loads the images names from String[] in to PImage[] in order
  for (int i=0; i < imageNames.length; i++) {
    String imageName = imageNames[i];
    images[i] = loadImage(imageName);
  }

  //counter increment
  int inc = 1;
}


void draw() {

  int digitalReading = arduino.digitalRead(2);
  int analogReading = arduino.analogRead(0);


  println(digitalReading);
  println(analogReading);

  // draws the image at the currently index of PImage[]
  image(images[counter], width/2, height/2);

  if (analogReading > 50) {
    counter ++;
    arduino.digitalWrite(ledPin, arduino.HIGH);
    delay(1000);
    arduino.digitalWrite(ledPin, arduino.LOW);

    if (counter > 3) {
      counter = 0;
    }
  }
}

Please, read your post again.

Now do you see why we ask you to use CODE TAGS?

Read this before posting a programming question

How to use this forum

Hi Everyone,

I am using a parallax x-band sensor to detect movement and change an image displayed.

Its all working fine but I now need to refine it;

What I need to do is once the motion is detected I want the program to ignore the values from the sensor for a certain amount of time, to prevent the images changing rapidly.

Im just struggling with the code on this one!

its done in processing with the Arduino library, ill post the code so you can see but it will most likely be a nuisance for anyone to run it!

hope someone can help!


import processing.serial.*;
import cc.arduino.*;

Arduino arduino;

//put image names in here ready to be loaded in to array
String[] imageNames = {
  "cheerful.jpeg", "cool.jpeg", "chatterbox.jpeg", "fussy.jpeg"
};

//creates an image array with the amount of field specified by String[]  
PImage[] images = new PImage[imageNames.length];

// counter to select picture
int counter = 0;


int ledPin = 13;


void setup() {
  size(500, 500);
  background(255);
  strokeWeight(5);
  imageMode(CENTER);

  arduino = new Arduino(this, Arduino.list()[11], 57600);
  
  // set pin numbers for each input/output
int sensorDigitalPin = 2;
int sensorAnalogPin = 0;

  // declared the pin mode for each pin
  arduino.pinMode(sensorDigitalPin, arduino.INPUT);
  arduino.pinMode(sensorAnalogPin, arduino.INPUT);
  arduino.pinMode(ledPin, arduino.OUTPUT);

  //loads the images names from String[] in to PImage[] in order
  for (int i=0; i < imageNames.length; i++) {
    String imageName = imageNames[i];
    images[i] = loadImage(imageName);
  }

  //counter increment
  int inc = 1;
}


void draw() {

  int digitalReading = arduino.digitalRead(2);
  int analogReading = arduino.analogRead(0);


  println(digitalReading);
  println(analogReading);

  // draws the image at the currently index of PImage[]
  image(images[counter], width/2, height/2);

  if (analogReading > 50) {
    counter ++;
    arduino.digitalWrite(ledPin, arduino.HIGH);
    delay(1000);
    arduino.digitalWrite(ledPin, arduino.LOW);

    if (counter > 3) {
      counter = 0;
    }
  }
}

[/quote]

Sorry, is this better? (see above)

//counter increment
  int inc = 1;

It’s about to go out of scope - what’s that for?

(Better, thanks - the italics make it difficult to understand when the [i] subscripts disappear)

Hi thanks for the reply

//counter increment
  int inc = 1;

//is related to this, its scrolls through the available images

// draws the image at the currently index of PImage[]
  image(images[counter], width/2, height/2);

inc "goes out of scope" at the next curly brace (}) which means the variable is thrown away and does you no good.

also,

// draws the image at the currently index of PImage[]
  image(images[counter], width/2, height/2);

Doesn't use inc?

Sorry what I posted didn't make sense, I rechecked and I think that variable was a useless piece of code I left in there.

Any suggestions on ingnoring the input from the sensor temporarily?

Regards,

Oli

olimason:
Sorry what I posted didn’t make sense, I rechecked and I think that variable was a useless piece of code I left in there.

Any suggestions on ingnoring the input from the sensor temporarily?

Regards,

Oli

How long would you like the sensor to be “ignored”?

Hint - Arduino only checks your sensor when you tell it to, and not when it’s doing something else (in this application).

Possible ideas —

while

delay()

millis // (Blink w/o delay example)

olimason:
Any suggestions on ingnoring the input from the sensor temporarily?

The same way you ignore anything. Don't look at it (don't read it).

@gammon

I might not have explained what I need to do too clearly;

I need to read the input until the value gets > 50.
Once one value is registered above 50 I want all values from the pin to be ignored for 5 seconds.

@chicagodave

I've already had a little play with those but still not quite figured it out.
I keep trying and see where I get and post back.

Thanks for you input everyone

So, when you hit a threshold, read the time, and then don't read anything more until 5000 milliseconds has elapsed.
Have I missed something?

yes pretty much,

The application will be in a window, when someone passes it will change image.

I need to ensure that once its triggered, enough time if left so they can read/look at the display with out the movement triggering the image again.

then after the predetermined amount of time the program resume to looking for movement.

I cant figure out how to get the value ignored though.

I know your thinking 'use delay()'
but I cant because the code needs to read to the end before anything changes.

so i get;

sensor detect movement
tell image to change
delay()

}

but the image wont actually change until after the delay and i need it to change instantly.

olimason:
I know your thinking 'use delay()'
but I cant because the code needs to read to the end before anything changes.

so i get;

sensor detect movement
tell image to change
delay()

}

but the image wont actually change until after the delay and i need it to change instantly.

Can you repost code? This version of it?

From previous code, looks like things are a bit out of order from what you say here.

It's really....
• Reading sensor
• Displaying image
• Detecting movement
• Selecting image
.....in that order.

Where did you put delay?

You probably want this order of actions....
•Read sensor
•Detect movement
•Select next image
•Display image
• delay()

I’ve been playing with Finite State Machines, so for just fun I put your code into one to see what it would look like.

/*
====================
FSM (Finite State Machine)
====================
*/

// First, let's define our states:
#define READstate 0
#define MOTIONstate 1
#define IMAGEWAITstate 2
// It doesn't matter what numbers you use, as long as they are 0 to 255 and you use a different number for each state.

// Next, we set up some variables to hold state information:
byte state = READstate;
/* You can set this to whatever state you want your machine to boot up in to. It's recommended that this be some
   kind of 'failsafe' state -- you probably want your machine to not take any actions until getting some input data. */

byte previousState = -1; // previousState is used in scheduling state changes.
unsigned long fsmtimer = millis(); // fsmtimer is used to schedule when to change states
// done with FSM setup

import processing.serial.*;
import cc.arduino.*;

Arduino arduino;

//put image names in here ready to be loaded in to array
String[] imageNames = {
  "cheerful.jpeg", "cool.jpeg", "chatterbox.jpeg", "fussy.jpeg"
};

//creates an image array with the amount of field specified by String[]  
PImage[] images = new PImage[imageNames.length];

// counter to select picture
int counter = -1;

int ledPin = 13;

// We call this function when we're ready to change states, and tell it what state we want to change to.
void changeState(const byte newstate)
{
previousState = state;
state = newstate;
Serial.print("leaving state "); Serial.print(previousState); Serial.print(" for state "); Serial.println(state);
}


// this function is used to schedule a state change after a given interval
void scheduleChangeState(const byte nextState, const unsigned long when)
{
if(previousState != state) // first call from a new state
   {
   previousState = state;
   fsmtimer = millis() + when; // schedule the next state change
   }
else if( (long)( millis() - fsmtimer ) >= 0  ) // have we been in this same state for long enough?
   { changeState(nextState); } // if so, go ahead and change to the new state
}


void setup()
{
Serial.begin(38400);

 size(500, 500);
  background(255);
  strokeWeight(5);
  imageMode(CENTER);

  arduino = new Arduino(this, Arduino.list()[11], 57600);
  
  // set pin numbers for each input/output
int sensorDigitalPin = 2;
int sensorAnalogPin = 0;

  // declared the pin mode for each pin
  arduino.pinMode(sensorDigitalPin, arduino.INPUT);
  arduino.pinMode(sensorAnalogPin, arduino.INPUT);
  arduino.pinMode(ledPin, arduino.OUTPUT);

  //loads the images names from String[] in to PImage[] in order
  for (int i=0; i < imageNames.length; i++) {
    String imageName = imageNames[i];
    images[i] = loadImage(imageName);
  }
}


void fsm() // finite state machine
{
// 'switch' is the heart of the FSM. This is the logic used to decide which code to run based on what state we are in.
switch(state)
  {
  case MOTIONstate:
    // This is where you'd put the code you want to run while you're in the MOTIONstate state.

    // draws the image at the currently index of PImage[]
    image(images[counter], width/2, height/2);

    changeState(IMAGEWAITstate);
    break;

  case IMAGEWAITstate:
    // This is where you'd put the code you want to run while you're in the IMAGEWAITstate state.

    // In this example, we choose to stay in this state for 10,000ms before switching to READstate
    scheduleChangeState(READstate, 10000);
    break;

  /* You'll notice that we have no 'case READstate'. That's deliberate. In case we ever get into an unexpected state, 
     we want to wind up here. */
  default: // READstate
    /* This is the state that the machine boots up in to, so this is where you might do some checks for input and 
       select an appropriate machine state based on that input. Maybe you don't want to change states at all yet, and 
       that's fine too -- this code will be run over and over forever until you decide to change to a new state. */
    int digitalReading = arduino.digitalRead(2);
    int analogReading = arduino.analogRead(0);

    println(digitalReading);
    println(analogReading);
  
    if (analogReading > 50)
      {
      counter ++;
      if (counter > 3)
        { counter = 0; }
      changeState(MOTIONstate);  
      }
  }
}

void loop()
{

// you can run pre-fsm code here if you like. 

fsm();

// you can run post-fsm code here if you like. 
  
}

olimason:
I know your thinking 'use delay()'

I don't usually suggest that, rather the opposite.