Go Down

Topic: output dependant on 2 inputs. (Read 999 times) previous topic - next topic

I am trying to put together a project that when both a microphone and a pressure sensor both give a peak reading a output is triggered however at present i am experimenting with simple push buttons before using analog inputs.

what I am trying to achieve is that when both buttons are pressed or pressed within a given time of each other (ie. one button is pushed and the other is pushed within 2 seconds) that an event is triggered.

any help at all getting this to work with at least the buttons would be greatly appreciated.

so far this is what i have, but have been having trouble getting the two buttons to operate independently, the LedPins are currently just being used for testing purposes. I havnt got this far yet but the intention was that when both counters = 1 they would trigger an event.

int buttonPin1 = 1;
int buttonPin2 = 2;
int ledPin1 = 9;
int ledPin2 = 10;

int counter1 = 0; //using a counter to determine button state
int counter2 = 0;

int buttonState1;
int buttonState2;

void setup(){


  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
}

void loop(){
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);

  if (buttonState1 == HIGH){
    counter1++;
    if (counter1 >1){
      counter1 = 0;
    }
  }
  if(buttonState2 == HIGH){
    counter2++;
    if (counter2 >1){
      counter2 = 0;
    }
  }
if (counter1 == 1){
    digitalWrite(ledPin1, HIGH);
  }
  else if(counter1 != 1){
    digitalWrite(ledPin1, LOW);
  }
  if (counter2 == 1){
    digitalWrite(ledPin2, HIGH);
  }
  else if(counter2 != 1){
    digitalWrite(ledPin2, LOW);
  }
if(counter1 ==1){     //this part had the intention of resetting each counter to 0 after 1 second.
  delay(1000);
  counter1--;
}
if(counter2 ==1){
  delay(1000);
  counter2--;
}

}

robtillaart

check millis(),
you can make a timestamp with that when an event occurs, in pseudo code

Code: [Select]
unsigned long TS1 = 0;
unsigned long TS2 = 0;

if (button1 is pressed) TS1 = millis();
if (button2 is pressed) TS2 = millis();

if ((TS1 != 0) && (TS2 != 0) && (abs(TS1 - TS2) < 2seconds))
{
   do event;
}


get it?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Graynomad

I think you can do it a lot simpler than keeping track of states and counters. Something like

Code: [Select]
unsigned long t_event1;
unsigned long t_event2;


if (event1_happened())
t_event1 = millis();

if (event2_happened())
t_event2 = millis();

if (t_event1 - t_event2 < 2000)
    // both events happened within 2 seconds


______
Rob

Rob Gray aka the GRAYnomad www.robgray.com

Had a quick go and got stuck with "< 2seconds"

sorry all of this is very new to me. is this getting closer to what i need?

Code: [Select]
int buttonPin1 = 1;
int buttonPin2 = 2;
int ledPin1 = 9;
int ledPin2 = 10;


int buttonState1;
int buttonState2;

unsigned long TS1 = 0;
unsigned long TS2 = 0;

void setup(){


  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
}

void loop(){

buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
 
if (buttonState1 ==HIGH){
  TS1 = millis();
}

if (buttonState2 == HIGH){
  TS2 = millis();
}

if ((TS1 != 0) && (TS2 != 0) && (abs(TS1 - TS2) < 2seconds))
{
    digitalWrite(ledPin1, HIGH);
  }
  else{
    digitalWrite(ledPin1, LOW);
}

}

Graynomad

#4
Apr 08, 2011, 05:13 pm Last Edit: Apr 08, 2011, 05:15 pm by Graynomad Reason: 1
Read up on the millis() function then look at my version, that should give you a hint.

Both Rob and I were showing pseudo code, it's not supposed to work out of the box.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

robtillaart

@Rob
Quote
if (t_event1 - t_event2 < 2000)
    // both events happened within 2 seconds

If both are zero (just after init) this condition is also true ;) 
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Graynomad

Yeah I noticed your extra test for that gotcha.

_____
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy