248Views14Replies

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.

Discussions

0
None
steveastrouk

Best Answer 5 years ago

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)) ;
;

}

0
None
mrmerino

5 years ago

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.

Arduino timer.jpg
0
None
steveastrouk

5 years ago

Have you written ANY Arduino code ?

0
None
frollardsteveastrouk

Answer 5 years ago

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

0
None
mrmerinofrollard

Answer 5 years ago

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.

0
None
frollardmrmerino

Answer 5 years ago

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.

0
None
steveastrouk

5 years ago


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) ;

}

0
None
mrmerinosteveastrouk

Answer 5 years ago

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.

0
None
steveastroukmrmerino

Answer 5 years ago

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

0
None
mrmerinosteveastrouk

Answer 5 years ago

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?

0
None
steveastroukmrmerino

Answer 5 years ago

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.

0
None
mrmerinosteveastrouk

Answer 5 years ago

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

0
None
frollardsteveastrouk

Answer 5 years ago

+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.

0
None
steveastroukfrollard

Answer 5 years ago

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