Pages: [1]   Go Down
Author Topic: trouble with timestamps  (Read 590 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

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24317
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

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

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24317
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you got pull-downs on the switches?

millis is very unlikely to return zero; I wouldn't even bother testing for it.
Logged

"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.

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

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24317
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd try putting in some debug prints, to give you some idea of what is going on.
Logged

"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.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46026
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: