Pages: [1]   Go Down
Author Topic: output dependant on 2 inputs.  (Read 755 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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--;
}

}
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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?
Logged

Rob Tillaart

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

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8475
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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

Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
}

}
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8475
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: April 08, 2011, 10:15:13 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@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 smiley-wink 
Logged

Rob Tillaart

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

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8475
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah I noticed your extra test for that gotcha.

_____
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Pages: [1]   Go Up
Jump to: