1

i am brand new to arduino and was looking for a little help. i am looking to activate a motor once the button is pushed for 10 seconds and then have it stop. then the next time the button is pushed, it runs in reverse for ten seconds and stops. i need it to continue this cycle everytime the button is pushed. button push 1 = forward 10 seconds, button push 2 = reverse ten seconds, button push 3 = forward 10 seconds and so on... i believe i am very close to accomplishing this task however like i said i am new to all this and i could be miles away. any help would be appreciated. i am using a mega 2560 even though the picture shows an UNO. i dont think itll matter as long as i use the correct pins. link to tinkercad.com

int BUTTON = 9; int DIRA = 3; int DIRB = 4; int FOR = true; //forward - reverse int ENABLE = 5; void setup() { //---set pin direction pinMode (ENABLE, OUTPUT); pinMode (BUTTON, INPUT_PULLUP); pinMode(DIRA,OUTPUT); pinMode(DIRB,OUTPUT); Serial.begin(9600); } void loop() { if (digitalRead(BUTTON) == HIGH) { digitalWrite(ENABLE, HIGH); if (FOR == true) { digitalWrite(DIRA,HIGH); //on forward digitalWrite(DIRB,LOW); delay(10000); FOR = false; digitalWrite(ENABLE,LOW); //stop } else { digitalWrite(DIRB,HIGH); //on reverse digitalWrite(DIRA,LOW); delay(1000); FOR = true; digitalWrite(ENABLE,LOW); //stop } } }` 

enter image description here

8
  • you did not fully describe the desired behavior ... what should happen if the button is pressed and held? .... what about pressed, released and pressed again after one second? Commented Jul 10, 2022 at 19:44
  • And what is the problem with your code? Does it behave differently than expected? Then please describe in detail what you expected and what actually happened. Commented Jul 10, 2022 at 20:03
  • thanks for the replies. i hadnt even thought about different types of button presses. what i would like is that no matter if the button is pressed once, pressed and held, or pressed multipliple times, id like it to complete its cycle before the button can be pressed again. so on the push down it starts the cycle, and doesnt stop until the cycle is complete. nothing happens when i push the button with my code and circuit as is. Commented Jul 10, 2022 at 20:14
  • the description of the problem belongs in the question at top of this page ... this site is not a forum ... reduce your code to only a push button that lights an LED when pressed ... does that work as expected? Commented Jul 10, 2022 at 21:20
  • 1
    Yes I I can get the push button to work without the motor controlling a simple RGB LED Commented Jul 10, 2022 at 22:20

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.