int s1 = 4;
int s2 = 5;
int s3 = 6;
int s4 = 7;
unsigned long time1;
unsigned long time2;
unsigned long time3;
unsigned long time4;
int S1 = 0;
int S2 = 0;
int S3 = 0;
int S4 = 0;
void setup()
{
pinMode(s1,INPUT);
pinMode(s2,INPUT);
pinMode(s3,INPUT);
pinMode(s4,INPUT);
Serial.begin(9600);
while(1)
{
while(S1==LOW)
{ S1 = digitalRead(s1);
if (S1==HIGH)
time1=micros();
Serial.println(time1);
break;
}
while(S2==LOW)
{ S2 = digitalRead(s2);
if (S2==HIGH)
time2=micros();
Serial.println(time2);
break;
}
while(S3==LOW)
{ S3 = digitalRead(s3);
if (S3==HIGH)
time3=micros();
Serial.println(time3);
break;
}
while(S4==LOW)
{ S4 = digitalRead(s4);
if (S4==HIGH)
time4=micros();
Serial.println(time4);
break;
}
if (S1==HIGH&&S2==HIGH&&S3==HIGH&&S4==HIGH)
{break;
}
}
Serial.println('+++++++++++++++++');
Serial.println(time1);
Serial.println(time2);
Serial.println(time3);
Serial.println(time4);
}
void loop(){
}
I think that you must come up with a better description of the problem.
You are aware that your while loops don't loop? You enter the loop and next unconditionally break from it.
system
October 16, 2017, 6:37pm
4
while(S1==LOW)
{
S1 = digitalRead(s1);
if (S1==HIGH)
time1=micros();
Serial.println(time1);
break;
}
See the problem now?
Please remember to use code tags when posting code
sterretje:
I think that you must come up with a better description of the problem.
You are aware that your while loops don't loop? You enter the loop and next unconditionally break from it.
No, all the loops work properly, except that when I break out of the while(1) I can't
serial print variables any more time1, time2, time3, time4
system
October 16, 2017, 7:32pm
6
Serial.println('+++++++++++++++++');
Oops
I can't
serial print variables any more time1, time2, time3, time4
What does that mean?
AWOL:
Serial.println('+++++++++++++++++');
OopsWhat does that mean?
I commented it out as well as Serial.print(time1), it seems to be working now.
Thank you!!!!!!!!!!!!!!!!!!!!!!!!!!!!
I was supposed to use " instead of '
Thanks you again.
Delta_G:
If those while loops "work" as written then they really need to be if statements. You have a break that isn't contained by any if or anything else so it's only going over once. That's what if statements do.
Thank you for the tip, will try it
no, it doesn't work with if only instead of while
system
October 17, 2017, 4:10pm
12
What is "it" that doesn't work?
gfvalvo
October 17, 2017, 4:13pm
13
Post the complete new code.
gfvalvo:
Post the complete new code.
Will do - I think I found the mistake, missing curly brackets
Regards
This works perfectly:
int s1 = 4;
int s2 = 5;
int s3 = 6;
int s4 = 7;
unsigned long time1;
unsigned long time2;
unsigned long time3;
unsigned long time4;
int S1 = 0;
int S2 = 0;
int S3 = 0;
int S4 = 0;
void setup()
{
pinMode(s1,INPUT);
pinMode(s2,INPUT);
pinMode(s3,INPUT);
pinMode(s4,INPUT);
Serial.begin(9600);
while(1)
{
S1 = digitalRead(s1);
if (S1==HIGH)
{ time1=micros();
// Serial.println(time1);
}
S2 = digitalRead(s2);
if (S2==HIGH)
{ time2=micros();
// Serial.println(time2);
}
S3 = digitalRead(s3);
if (S3==HIGH)
{ time3=micros();
// Serial.println(time3);
}
S4 = digitalRead(s4);
if (S4==HIGH)
{ time4=micros();
// Serial.println(time4);
}
if (S1==HIGH&&S2==HIGH&&S3==HIGH&&S4==HIGH)
{break;
}
}
Serial.println("+++++++++++++++++");
Serial.println("time1");
Serial.println(time1);
Serial.println("time2");
Serial.println(time2);
Serial.println("time3");
Serial.println(time3);
Serial.println("time4");
Serial.println(time4);
}
void loop(){
}
Except this version only catches micros when button pressed consecutively, and I need random
(previous version did it)
gfvalvo
October 18, 2017, 6:54pm
18
Think it's about time you started posting your code with Code Tags as requested in Reply #3 .
Need to figure out yet how to do it
system
October 19, 2017, 4:47pm
20
telecom55:
Need to figure out yet how to do it
...which is why there's a handy explanation at the top of this forum section.