Button activating section of code in scetch

Hi,

I am very new to arduino, 3days to be exact. Im having trouble triggering certain parts of my code, assigned to a certain button when pushed. The arduino outputs are activating relays, which is working great. Its just the buttons i need assistance with. I’ve searched forums etc and came up empty handed.

Basically what must happen is:
There are 3 different programs in the scetch. 2day. 5day and seven day. On my breadboard i have 3 buttons. Each button need to activate an program,i.e 2day, 5day or 7day. Whenever the arduino is energised, nothing must happen until any of the buttons is pushed.

Ive came up with the code below… (dont laugh!!) Please Help! :o

int right=8;//right relay//
int left=10;//left relay//
int down=13;//down relay//
int ButtonTwo=2;//trigger 2 day programme//
int ButtonFive=4;//trigger 5 day programme//
int ButtonSeven=7;//trigger 7 day programme//

void setup() { //Setup runs once//

pinMode(right, OUTPUT); //Set right as an output//
pinMode(left, OUTPUT); //Set left as an output//
pinMode(down, OUTPUT); //Set down as an output//
pinMode(ButtonTwo, INPUT); //Set twoDay as an input//
pinMode(ButtonFive, INPUT); //Set fiveDay as an input//
pinMode(ButtonSeven, INPUT); //SetsevenDay as an input

}

void loop() { //Loop runs forever//
if(digitalRead(ButtonTwo)==HIGH)
{
delay(1000); //for 3.5 seconds//
digitalWrite(right,HIGH); //Motor runs clockwise, Step 1//
delay(1000); //for 3.5 seconds//
digitalWrite(right, LOW); //Motor stops//
delay(1000); //Motor right wait for 2hours//
digitalWrite(left,HIGH); //Motor runs left//
delay(1000); //for 1 second//
digitalWrite(left,LOW); //Motor left Stops//
delay(1000); //Motor left stop for 1 second//
digitalWrite(left,HIGH); //Motor runs left//
delay(1000); //for 1 second//
digitalWrite(left,LOW); //Motor left Stops//
delay(1000); //Motor left stop for 1 Second//

}

if(digitalRead(ButtonFive)==HIGH)
{
digitalWrite(down,HIGH); //Motor runs down,Step 1 //
delay(1000); //for 3.5 seconds//
digitalWrite(down, LOW); //Motor stops//
delay(1000);
digitalWrite(right,HIGH); //Motor runs clockwise, Step 2//
digitalWrite(down,HIGH); //Motor runs down, //
delay(1000); //for 3.5 seconds//
digitalWrite(right, LOW); //Motor stops//
digitalWrite(down, LOW); //Motor stops//
delay(1000); //Motor right wait for 2hours//
digitalWrite(right,HIGH); //Motor runs clockwise, Step 3//
digitalWrite(down,HIGH); //Motor runs down, //
delay(1000); //for 3.5 seconds//
digitalWrite(right, LOW); //Motor stops//
digitalWrite(down, LOW); //Motor stops//
delay(1000); //Motor right wait for 2hours//
digitalWrite(left,HIGH); //Motor runs left, Step 4//
digitalWrite(down,HIGH); //Motor runs down, //
delay(1000); //for 1 second//
digitalWrite(left,LOW); //Motor left Stops//
digitalWrite(down, LOW); //Motor stops//
delay(1000); //Motor left stop for 1 second//
}else{
digitalWrite(ButtonFive, LOW);
}

if(digitalRead(ButtonSeven)==HIGH)
{
digitalWrite(down,HIGH); //Motor runs down,Step 1 //
delay(1000); //for 3.5 seconds//
digitalWrite(down, LOW); //Motor stops//
delay(1000);
digitalWrite(right,HIGH); //Motor runs clockwise, Step 2//
digitalWrite(down,HIGH); //Motor runs down, //
delay(1000); //for 3.5 seconds//
digitalWrite(right, LOW); //Motor stops//
digitalWrite(down, LOW); //Motor stops//
delay(1000); //Motor right wait for 2hours//
digitalWrite(left,HIGH); //Motor runs clockwise, Step 3//
digitalWrite(down,HIGH); //Motor runs down, //
delay(1000); //for 3.5 seconds//
digitalWrite(left, LOW); //Motor stops//
digitalWrite(down, LOW); //Motor stops//
delay(1000); //Motor right wait for 2hours//
digitalWrite(left,HIGH); //Motor runs left, Step 4//
digitalWrite(down,HIGH); //Motor runs down, //
delay(1000); //for 1 second//
digitalWrite(left,LOW); //Motor left Stops//
digitalWrite(down, LOW); //Motor stops//
delay(1000); //Motor left stop for 1 second//
digitalWrite(left,HIGH); //Motor runs left, Step 5//
digitalWrite(down,HIGH); //Motor runs down, //
delay(1000); //for 1 second//
digitalWrite(left,LOW); //Motor left Stops//
digitalWrite(down, LOW); //Motor stops//
delay(1000); //Motor left stop for 1 second//
}else{
digitalWrite(ButtonTwo, LOW);
digitalWrite(ButtonFive, LOW);
digitalWrite(ButtonSeven, LOW);
}
}

Motor_control1.ino (23.2 KB)

Im having trouble triggering certain parts of my code

I really don't understand why you don't use switches. Properly wired, they are for more effective than buttons.

Buttons are for shirts. Switches are for Arduinos.

How long do you have to hold a switch down in order for the Arduino to notice?

How are your buttons sewn on? You are not using the internal pullup resistors, so you must have external pullup or pulldown resistors (the code suggests pulldown). You do have them, right?

@PaulS,

"How long do you have to hold a switch down in order for the Arduino to notice?" - Immidiatly when pushed.

"You are not using the internal pullup resistors, so you must have external pullup or pulldown resistors (the code suggests pulldown). You do have them, right?" - Yes I do have them - BUT - Will it make a difference if i only wired the first one(2day?)

"How long do you have to hold a switch down in order for the Arduino to notice?" - Immidiatly when pushed.

That may be what you want, but all those delay()s mean that that is NOT what actually happens.

You need to look at the blink without delay example and state machines, and scrap that code and start over.

  • Yes I do have them - BUT - Will it make a difference if i only wired the first one(2day?)

If you don't read from the other two pins, no. If you do, it most certainly does.

Nothing in that sketch will react immediately because it spends most of its time in delay commands where the board is comatose. You’ll need to study the blink without delay example to see how to handle timing without blocking.

Yes, it does make a difference. You need pull-downs on each switch. It would be much easier to use the internal pull-up resistors instead. Wire the switch so that it connects to ground when pressed. Use INPUT_PULLUP instead of INPUT in your pinMode calls. And you’ll have to reverse the logic that way. A pressed button would read as LOW and a not pressed button would read HIGH. That’s the more common way of doing things.

Thank you, then that's what i'll do for now. Thanks a lot for the advice!

Sorry, just to come back to the sketch. Is the code i used to trigger each section in the sketch correct?...... or rather, will it work?

Yes, you've got the if(digitalRead ... part right. The problem is that you'd have to press the button at exactly the right instant in order to have it be seen by the code. Most of the time this code is ignoring the buttons.

Switches it is then!

Rookie89: Switches it is then!

Paul has a rather narrow definition of the word button. What most of us who speak English would call a button, like the buttons on the TV remote that change the channel, he prefers to call switches. I guess his dictionary was missing a page or two.

Webster's definition of button. Note the third definition

Paul has a rather narrow definition of the word button.

Buttons are for shirts and dialogs. The thing on the remote control is a push-button switch. If one is going to drop a bunch of letters from the name, why not drop the stuff that distinguishes one switch from another, rather than the part that says that it IS a switch? Why not call them pushes? That's even shorter than buttons.

PaulS: Buttons are for shirts and dialogs. The thing on the remote control is a push-button switch. If one is going to drop a bunch of letters from the name, why not drop the stuff that distinguishes one switch from another, rather than the part that says that it IS a switch? Why not call them pushes? That's even shorter than buttons.

Got you. That's exactly what I said in that post. Paul only accepts the first two of the three definitions given by websters for the word button. Fortunately Paul is not the curator of the English language, so the fact that he rejects one valid, documented, and quite commonly used definition of the word has very little impact on the rest of us. You are welcome to redefine as you will, but you shouldn't admonish others for using the common English definition instead of your personal definition.

I bet if you did a poll of a million people in the US as to what the things on the remote are called you would get at least 999,900 responses saying button. It is distinct from switch in common usage as normally one thinks of a button as something you push or press and a switch as something you flip or physically move from one position to the other.

One might just as easily and rightly argue that buttons belong on the remote and the things on the shirt are fasteners.

@Delta,

I changed my input to INPUT_PULLUP and changed the wiring!works 100%!!!! Thanks!!

May I ask a question on timing of each section here?Ore need i post a new question?

Oxford definition just in case we dislike Webster's for some reason. Here it is 2nd definition.

Rookie89: @Delta,

I changed my input to INPUT_PULLUP and changed the wiring!works 100%!!!! Thanks!!

May I ask a question on timing of each section here?Ore need i post a new question?

Ask away. It would be preferable to keep it in one thread.

Great.

Will it be possible to have a LCD displaying the time left until the program is done?

Rookie89: Great.

Will it be possible to have a LCD displaying the time left until the program is done?

Not coded the way it is with delay used for timing.

ah, though the delay will be the problem. But I am going to re-write the program when I get some time, and I'll spam you again then.. Thank you very much!