235Views14Replies

Author Options:

Arduino with timer? Answered

I'm not very good at coding so I want to come to you guys again. I need help with a sketch or code that allows me to have three inputs and one output. When one button is pressed, that Arduion pulls the output high for an amount of time dependent on which button is pressed. These times are 3 seconds, eight seconds, and twelve seconds. The output will be connected to a small relay, which I already know how to do. If I could get any help that would be great. Thanks.

16 Replies

user
steveastroukBest Answer (author)2013-03-13

Sort out your pin allocations, and try this. It compiles, I haven't run it.


int outputpin;
int btn1 = 13;
int btn2 = 7;
int btn3 = 0;



void setup()
{
pinMode(outputpin, OUTPUT); // sets the digital pin 13 as output
pinMode(btn1, INPUT); // sets the digital pin 7 as input
pinMode(btn2, INPUT);
pinMode(btn3, INPUT);
}

void loop(){
if ((digitalRead (btn1)==HIGH) | (digitalRead (btn2)==HIGH) | (digitalRead (btn3)==HIGH)) digitalWrite(outputpin,HIGH);
if (digitalRead (btn1)==HIGH) {delay (3000);}
else if (digitalRead (btn2)==HIGH) {delay (8000);}
else if (digitalRead (btn3)==HIGH) {delay (12000);}

digitalWrite(outputpin,LOW);

while ( (digitalRead (btn1)==HIGH) | (digitalRead (btn2)==HIGH) | (digitalRead(btn3)==HIGH)) ;
;

}

Select as Best AnswerUndo Best Answer

user
mrmerino (author)2013-05-08

Ok, several months later I have finally acquired a computer of my own. I downloaded the Arduino program, plugged in my board, and uploaded the sketch. It actually does work, pic related.

I'd like to thank steveastrouk and frollard for their patience and help. I couldn't have done without you guys.

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)2013-03-13

Have you written ANY Arduino code ?

Select as Best AnswerUndo Best Answer

user
frollard (author)steveastrouk2013-03-13

I feel he may be getting confused with the bitwise or.

Select as Best AnswerUndo Best Answer

user
mrmerino (author)frollard2013-03-13

I'm getting jargonated hard, but I'm trying to keep up. I've never done more than html, but I'd really like to be able to do Arduino.

Select as Best AnswerUndo Best Answer

user
frollard (author)mrmerino2013-03-14

Here's the deal.

Goto http://www.arduino.cc and click on Learning. Go through the tutorials. Someone spent a LOT of time making them easy to understand building blocks that you can use to wrap your head around the basics.

Reference (open in a new window or tab) -- have that ready at all times to remember the syntax of the commands. It doesn't hurt to read through each of those links to see what each of those functions does.

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)2013-03-12


Assuming your input buttons are pulled HIGH, and go LOW when pressed all you need is the delay function.

loop(){

if(button1==LOW) | (button2==LOW) | (button3==LOW) digitalwrite(outputpin,HIGH);

if (button1==LOW) {delay (3000)}
else if (button2==LOW) {delay (8000)}
else if (button3==LOW) {delay (12000)};

digitalwrite(outputpin,LOW);

while (button1==LOW) | (button2==LOW) | (button3==LOW) ;

}

Select as Best AnswerUndo Best Answer

user
mrmerino (author)steveastrouk2013-03-12

Oops, I meant the inputs are LOW then are made HIGH when the buttons are pressed. Would it be best to use analog or digital inputs? If I wanted A1, A2, and A3 to be the inputs and like D4 to be the output, how would the code look? Thanks a lot for your help.

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)mrmerino2013-03-12

Analog pins are quite usable as digital inputs, just declare it in the setup code.

Select as Best AnswerUndo Best Answer

user
mrmerino (author)steveastrouk2013-03-12

I wish I had a computer.

So if I just switch around all the parts where it says LOW with HIGH and likewise that'll keep the output LOW when all the inputs are LOW right? And in the setup part I have to tell it that D2=button1, D3=button2, et cetera, right?

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)mrmerino2013-03-13

Yep. But replace the end while with
while (digitalread (button1)==LOW) | (digitalread (button2)==LOW) | digitalread(button3)==LOW) ;

Also, you need three digitalreads in the main loop to check the buttons.

Select as Best AnswerUndo Best Answer

user
mrmerino (author)steveastrouk2013-03-13

Uh huh. Do I just stick those in front of the the "If else else" clause?

Select as Best AnswerUndo Best Answer

user
frollard (author)steveastrouk2013-03-12

+1, just needs the digitalreads in there to update the buttons during the while{} at the bottom.

Depending on if the code needs to be able to listen for more buttons while the current delay is in progress - I like using the 'set a flag to see if x millis have elapsed yet' then you can keep looping and doing other stuff while waiting, including listening for the other buttons to potentially extend the time - again all down to the needs of the script.

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)frollard2013-03-12

On yeah. Missed that bit. I'd usually subsume it into an interrupt routine.

Select as Best AnswerUndo Best Answer