Hi,
i have two switch's that connected to two digital input's.
how can i Determine which input got first '1' ?
Thank you.
Hi,
i have two switch's that connected to two digital input's.
how can i Determine which input got first '1' ?
Thank you.
It depends on the timing resolution you need. If it's for a game button probably just do a loop and take the first pressed button.
If it's really tight timing you may even have to use hardware.
What's the application?
Rob
Put each switch on an interrupt pin. Have the interrupt service routine disable interrupts. Only one can be first.
The app is a parking lot with two sensores that give '1' if car past the sensor.
I said switch because it do the same job.
So if the first sensor gave '1' and after that the secound sensor gave '1' the cat getting in, and oppesite the car get out.
s1 and then s2 = car got in
s2 and then s1 = car got out
thank you
You will have a relatively long time lag there.
Create a flag for each one, and when a flag is set compare to the other flag if it was already set you will know the direction.
I think this pseudo-code will give you the idea. You'll have to come up with a debouncing scheme so that 2nd flag being triggered doesn't get seen as the next car going across too soon.
You might have to address one being set by something other than a car, maybe wait some predefined time after a flag is set and then reset it.
s1_flag = 0;
s2_flag =0;
void loop()
s1_state = digitalRead (s1); // assume low = activated for this example
if (s1_state == 0){s1_flag = 1}
if (s1_flag ==1 and s2_flag ==1){ // is other flag already set?
direction = out;
s1_flag = 0; // clear for next car
s2_flag = 0; // clear for next car
}
s2_state=digitalRead(s2);
if(s2_state == 0){s2_flag =1;}
if (s2_flag==1 and s1_flag == 1){ //is other flag already set?
direction = in;
s2_flag = 0; // clear for next car
s1_flag = 0; // clear for next car
}
} // end void loop
i tried this code, but it dont work.
what do you think is the problem:
int s1_flag = 0;
int s2_flag =0;int s1 = 2;
int s2 = 3;int led1 = 7;
int led2 = 8;int s1_status = 0;
int s2_status = 0;int way = 0;
void setup() {
pinMode(s1, INPUT);
pinMode(s2, INPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
}void loop() {
int s1_status = digitalRead(s1);
if (s1_status == 0) {
s1_flag = 1;
}
if(s1_flag == 1 and s2 == 1) {
way = 1; //1 mean out
digitalWrite(7, HIGH);
s1_flag = 0; // clear for next car
s2_flag = 0; // clear for next car
}int s2_status = digitalRead(s2);
if( s2_status == 0) {
s2_flag = 1;
}
if(s2_flag == 1 and s1 == 1) {
way = 0; //0 mean in
digitalWrite(8, HIGH);
s1_flag = 0; // clear for next car
s2_flag = 0; // clear for next car
}
delay(1000); // waits for a second} // end void loop
if(s1_flag == 1 and s2 == 1) {
if(s2_flag == 1 and s1 == 1) {
Why is the pin number important?
PaulS:
if(s1_flag == 1 and s2 == 1) {
if(s2_flag == 1 and s1 == 1) {
Why is the pin number important?
what a stupid mistake of mine, i dont know what i thought when i wrote that.
it should be:
if(s1_flag == 1 and s2_flag == 1) {
if(s2_flag == 1 and s1_flag == 1) {
Thank you.
i will check it today.
replace 'and' with &&
turn on the internal pullups:
void setup() {
pinMode(s1, INPUT);
digitalWrite (s1, HIGH); // add this
pinMode(s2, INPUT);
digitalWrite(s2, HIGH); // add this
i dont know what i thought when i wrote that.
It would be a good idea to give variables names that make them stand out. Had the variable names been s1_pin, s1_state, and s1_pressed, I'm sure you would have been less likely to make that mistake.