Im new to the arduino, so I need some very basic help. I want to write a sketch that will start when a button is pressed (I have a tactile switch for the button) and will run for a set amount of time before turning off. Pressing the button again will run it again for the same amount of time.
I think this needs to be written into the Setup part of the sketch. How do I get it to run when the button is pressed, and stop the Loop after a certain time.
Hope this is clear (my loop for running my motor is done, I just need to get it to start when I want and stop t seconds after it starts).
Thanks, and sorry if this is a repost. My quick search of the forum didnt yield the help I needed.
theliver:
I think this needs to be written into the Setup part of the sketch.
You seem to be misusing the term 'sketch'. That is the term for the whole program which runs when your Arduino is reset. You can't start and stop a sketch, except by turning the Arduino off or resetting it.
I think you probably want the sketch to detect the button press, do something for a short time and then wait for the next button press.
I guess you could do anything in the setup() function if you were determined enough, but conventionally setup() is used for the one-off initialisation and anything that happens after that is done in the loop() function. Since you want this behaviour to occur repeatedly (i.e. each time you press the button) loop() is the conventional place to put it.
You will have some design decisions to make:
Do you want the action to start when the button is pressed, or released? Do you need to debounce the button at all? What do you want to happen if the button is pressed again while it's still responding to the previous button press?
Thanks for the sketch johnwasser, its a nice base for helping me understand how to do this. Im getting an error in the setup though:
C:\Users\jack\Desktop\arduino-0023\hardware\arduino\cores\arduino/wiring.h:111: error: too many arguments to function 'int digitalRead(uint8_t)'
timing:12: error: at this point in file
not sure why this is happening.
Also thanks PeterH. You are right, I do need to evaluate more precisely when and what needs to happen, as well as learn better how to talk to my little 'duino. Ideally there would be no action if the button is pressed during a cycle, but within the scope of the project it shouldn't be anyways.
I'm gonna try to get everything I have working together, I'll hopefully be able to show off the result later. Thanks guys, hopefully you're around in the inevitable case I have more questions
while (digitalRead(2,HIGH)) /*DO NOTHING LOOP*/; // wait for button to ground pin 2
It's helpful to pay attention to the error messages. digitalRead() takes one argument, a pin number, not two. It returns a value that is either HIGH or LOW. You can't just stick all the words in a statement and add commas and parentheses where you feel like. You have to consider the compiler's feelings, too.
while (digitalRead(2,HIGH)) /*DO NOTHING LOOP*/; // wait for button to ground pin 2
It's helpful to pay attention to the error messages. digitalRead() takes one argument, a pin number, not two. It returns a value that is either HIGH or LOW. You can't just stick all the words in a statement and add commas and parentheses where you feel like. You have to consider the compiler's feelings, too.
Thanks. The compiler and I are on slightly better terms now, but I still don't understand her wants and needs.
That fixed the compile error though.Would the proper way to apply that logic be while(digitalRead(2)==HIGH)
Here is a simple sketch that will turn an LED on for 5 seconds when a button is pressed and then turn off until the button is pressed again, and so on:
PaulS:
You have to consider the compiler's feelings, too.
I agree with PaulS here.
I think this needs to be written into the Setup part of the sketch. How do I get it to run when the button is pressed, and stop the Loop after a certain time.
Just a caution. The names of those are setup and loop, not Setup and Loop. Like it or not, C++ is case-sensitive.