Hello guys....first i wonder if i can make a counter and something like D-latch with arduino to make a project as shown in the image in the attachment....i searched for these but i think the methods i found were complicating to me so i started to make a simpler code i know it's not that perfect one but i think it's easy to understand so....my question : "is there another simple way to right that code ?.....and thanks in advance
const int led1=7;
const int led2=8;
const int button1=3;
const int button2=5;
int i=0;
int z=0;
double counter=0;
int b1s;
int b2s;
void setup()
{
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
}
void loop()
{
b1s=digitalRead(button1);
b2s=digitalRead(button2);
if(b1s==HIGH&&b2s==LOW){i=1;}
if(i==1){digitalWrite(led1,HIGH); digitalWrite(led2,LOW); counter++;}
else if(i==0 && z==0){digitalWrite(led1,LOW); digitalWrite(led2,LOW);}
if(b2s==HIGH&&b1s==LOW){z=1; i=0;}
if(z==1)
{
if(counter != 0){digitalWrite(led1,LOW); digitalWrite(led2,HIGH); counter--;}
else if(counter == 0){digitalWrite(led1,LOW); digitalWrite(led2,LOW); z=0;}
}
}
moh50000:
No,it's just sparked my curiosity....so i consider it more than just a class homework problem
What is it you want to count? A counter and a timer are not exactly the same thing.
One could use millis() to record the time when the switch BECAME pressed (see the state change detection example) and to record when the switch BECAME released. One could then use the two values to determine what the interval was.
the thing i want is to start a timer just like a stopwatch... starts when button is pressed, stops when another button is pressed and uses the time interval between the start and stop to make a led on for an example for that certain time interval
the thing i want is to start a timer just like a stopwatch
No, I don't think so. At only one point in time do you care how long ago the button became pressed, and that is when it becomes released. So, recording when the switch became pressed is all that you need.
Having a "timer like a stopwatch" would mean that you could continuously see how much time had passed. Given that you know when the timer started, you could compute that, on any pass through loop() that you thought such information was relevant.
Having a "timer like a stopwatch" would mean that you could continuously see how much time had passed. Given that you know when the timer started, you could compute that, on any pass through loop() that you thought such information was relevant.
and that what i did where i put a counter in a loop.....so isn't there another way to do this (replacing the counter on my code) ?
and that what i did where i put a counter in a loop
But, what are you counting? As near as I can tell, you are counting the number of times that loop() iterates. Now, all you need to do is multiply that by the amount of time that each iteration takes to get the total time that the switch was pressed. And, how long is that?
Counting number of times the loop starts and making use of that count to enter another loop to reach the same number of entering the first loop.....so as i see counter is a tool to calculate the time i need and reuse this time or i can say reuse this counter.....sorry for my language
so as i see counter is a tool to calculate the time
If I give you 100 playing cards, what time is it?
The state change detection example that I keep pointing you to shows how to determine that a switch has become pressed. That would be a good time to record the start time. It shows how to determine that a switch has become released. That would be a good time to record the end time.
The difference between the end time and the start time is the time (NOT A COUNT OF ANYTHING) that the switch was pressed.
PaulS:
If I give you 100 playing cards, what time is it?
i consider...if you give me 100 playing cards one by one...every card will have its own time interval from the beginning...if you give me a card every one second then if you give me the card number 60 i will deduce that a minute has passed since you have started to give me the first card...that means i count the cards(num: 60) to calculate the time interval (1:min passed)
....i wounder if you understand what i want ?!!
i consider...if you give me 100 playing cards one by one...every card will have its own time interval from the beginning
A valid assumption. Of course, you need to know what that interval is and you need to make sure that you don't take a card before that interval has expired. So, how fast are you assuming that I'm giving you cards?
Wouldn't it be easier to look at your watch, and note that I gave you the first card at 3:19 and the last card at 7:42? That way, it won't matter if I (or you) don't keep the interval constant.
i wounder if you understand what i want ?
Of course. I just think that you are making too many unreasonable assumptions. Counting iterations lf loop() is NOT the way to determine how long something took.
PaulS:
how fast are you assuming that I'm giving you cards?
i don't...the time that makes the counter increases until reaching specific number will be the same time of that the counter decreases until reaching the very beginning number (that i made it zero in the code)....it's relative where i can make it the half time through increasing by 1 then decreasing by 2 to reach zero faster
By the way i did all this to make a car for an example moving at straight line until facing an obstacle then moving back to the beginning point....and the line length won't be constant....so the only way i could do this is by this code...and what i want is another code doing the same job
i don't...the time that makes the counter increases until reaching specific number will be the same time of that the counter decreases until reaching the very beginning number (that i made it zero in the code)
moh50000:
By the way i did all this to make a car for an example moving at straight line until facing an obstacle then moving back to the beginning point....and the line length won't be constant....so the only way i could do this is by this code...and what i want is another code doing the same job
Instead of trying to measure time, you should be trying to measure distance.
First thing: don't use single character variables as global variables. Give them sensible names.
Why don't you count milliseconds instead of loop iterations? If you go forward for 10000 ms, you can go back for 10000 ms.
Record the start time (e.g. in a static variable) when the first button is pushed. When the second button is pushed, you calculate the difference (duration) in ms, store it and set a new start time.
Next you go backwards and compare the current time with the recorded time plus recorded duration and stop when the duration is exceeded. Have a look at the BlinkWithoutDelay example that comes with the IDE how to use milliseconds for timing.