Go Down

Topic: trouble with timestamps (Read 833 times) previous topic - next topic

Jethro123

so  I started a project yesterday in which i am trying to get an event to happen when 2 buttons are pressed within a certain time of each other ie. button 1 is pushed and within 2 seconds button 2 is pushed and then an LED lights up.

I got a bit of help yesterday but am still struggling to get it to work any help pointing out where i am going wrong would be greatly appreciated.

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

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

void setup(){
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2,OUTPUT);
  pinMode(ledPin3,OUTPUT);
}

void loop(){
if(buttonPin1 == HIGH){
  TS1 = millis();
}
if(buttonPin2 ==HIGH){
   TS2 = millis();
}

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

AWOL

Quote
if(buttonPin1 == HIGH){

You've give buttonPin1 the value 1 - it is always HIGH.
Try reading the pin denoted by buttonPin1.

buttonPin2 has the value 2, so can never be HIGH, or LOW.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Jethro123

Ive altered it and am still getting the same result, at present pushing "button 1" will light the led regardless of the other button for what ever period is set in "(abs(TS1-TS2) < 1000))"

pressing button 2 does nothing at all

any ideas?

Quote
#define buttonPin1 1
#define buttonPin2 2
#define ledPin1  9

int button1 = 0;
int button2 = 0;

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

void setup(){

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

void loop(){
 
  button1 = digitalRead(buttonPin1);
  button2 = digitalRead(buttonPin2);
 
if(button1 == HIGH){
  TS1 = millis();
}
if(button2 ==HIGH){
   TS2 = millis();
}

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

AWOL

Have you got pull-downs on the switches?

millis is very unlikely to return zero; I wouldn't even bother testing for it.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Jethro123

yes i have pull downs however even if i put a much larger amount for instance   " if((TS1 >500) && (TS2 >500)"

even the slightest touch on the button gives exactly the same results as before.

AWOL

I'd try putting in some debug prints, to give you some idea of what is going on.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

I think that you want to record when the buttons first go HIGH, not that they are HIGH on this pass through loop(). This will require that you keep track of the previous state of the button. When the state changes to HIGH, record the time.

Debouncing the switches would be useful, too.

Go Up