Go Down

Topic: NEW HERE - arduino timer (Read 500 times) previous topic - next topic

joodias

May 31, 2015, 12:16 pm Last Edit: May 31, 2015, 12:17 pm by joodias
Hello everyone,

It's my first post here. I'm new to the Arduino and I still can't do anyrthing other than blink a led.
I have a request. I want to make a circuit which consists in a timer, which is activated with a push button and lights up two leds, each for a certain amount of time. I'll be using times of more than a minute. Can someone please help me with the code? From where do I start?

Best regards,
JDias

weedpharma

Do you know how to connect the switch and LEDs?

Have you read the information under the Learning tab above?

Weedpharma


dave-in-nj

use the sample sketch debounce. 
it will introduce you to the idea of a timer.

you willl have to figure out how it works and what part of it you need to get the job done

Robin2

Have a look at the code in several things at a time and the Thread planning and implementing a program. You should find most of what you need in them.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

jurs

From where do I start?
You start writing a "setup" and a "loop" function.
During 'setup' you should activate Serial, so you can later print serial debug messages that become visible in the serial monitor.

Perhaps give that a try:
Code: [Select]


#define BUTTONPIN 5
#define LEDPIN1 12
#define LED1DURATION 5000 // in milliseconds
#define LEDPIN2 13
#define LED2DURATION 10000 // in milliseconds

boolean led1isOn, led2isOn;
unsigned long buttonPressTime;

void setup() {
  Serial.begin(9600); // for serial debug output
  pinMode(BUTTONPIN, INPUT_PULLUP); // button is HIGH when not pressed and LOW when pressed
  pinMode(LEDPIN1, OUTPUT);
  pinMode(LEDPIN2, OUTPUT);
}

void loop() {
  if (led1isOn==false && led2isOn==false && digitalRead(BUTTONPIN)==LOW)
  {
    Serial.println("BUTTON PRESSED - switch both LEDs ON");
    led1isOn=true;
    led2isOn=true;
    buttonPressTime=millis();
  }
  if (led1isOn && millis()-buttonPressTime>=LED1DURATION)
  {
    Serial.println("LED-1 OFF");
    led1isOn=false;
  }
  if (led2isOn && millis()-buttonPressTime>=LED2DURATION)
  {
    Serial.println("LED-2 OFF");
    led2isOn=false;
  }
  digitalWrite(LEDPIN1, led1isOn);
  digitalWrite(LEDPIN2, led2isOn);  
}


Wiring:
Connect button between GND and pin-5
Connect first LED and 220 Ohm resistor in series from GND to pin-12
Connect second LED and 220 Ohm resistor in series from GND to pin-13
(pin-13 is also the board-LED with most Arduino boards)

For longer times in the minutes define longer times like
#define LED2DURATION 300000L // in milliseconds = 300 seconds = 5 minutes

joodias

I've setup the circuit in 123D Circuits and have been trying to make it do what I want. I've achieved the desired result using the delay function, but just for some seconds. I understand it is not stable to use for long periods. I also tried that code, but it seems it's not working. The leds stay on. I've tried to change some phrases but have still not achieved the desired result. Also, there's no need to have serial output, the simpler the best. Can you revise the code please?

Best regards

jurs

#6
Jul 02, 2015, 02:20 pm Last Edit: Jul 02, 2015, 02:22 pm by jurs
I've setup the circuit in 123D Circuits
What's that in detail?

Can you revise the code please?
Can you post a full list of parts used and schematics?

Which Arduino-Board are you using?
What type of LEDs are you using?
What other parts and how are they connected?

Either post a circuit diagram with every part labelled, or a list of parts used along with a detailed cabling description! Circuit diagram may be handwritten and photographed.

joodias

123D schematics is a website by Autodesk for schematic drawing and also has Arduino simulation.
I'm using an Arduino Uno rev.3, where I use pin 5 as the push button Input and 10 and 11 as the digital Outputs. The idea is to turn on both leds when the button is pressed, and for these to stay lit up for different amounts of time. I've accomplished this by using this code:

const int but = 5;
const int resa = 10;
const int resb = 11;

int butState = 0;

void setup() {
 
     pinMode(but, INPUT);
     pinMode(resa, OUTPUT);
     pinMode(resb, OUTPUT);
 
}

void loop() {
  butState = digitalRead(but);
   
    if (butState==HIGH)
  {digitalWrite(resa, HIGH);
   delay(10);
   digitalWrite(resb,HIGH);
//tempo resistencia A
   delay(300000L);
   
   digitalWrite(resa,LOW);
//tempo excedente resistencia B
   delay(5000);
   
   digitalWrite(resb,LOW);
  }
 
  digitalWrite(resb, LOW);
  digitalWrite(resa, LOW);
 
}
 

Yet, I've been said that there are more reliabe options.

jurs

123D schematics is a website by Autodesk for schematic drawing and also has Arduino simulation.
I'm using an Arduino Uno rev.3, where I use pin 5 as the push button Input and 10 and 11 as the digital Outputs.
So here is the revised code. Instead of declaration:
Code: [Select]

#define LEDPIN1 12
#define LED1DURATION 5000 // in milliseconds
#define LEDPIN2 13
#define LED2DURATION 10000 // in milliseconds

change to:
Code: [Select]

#define LEDPIN1 10
#define LED1DURATION 5000 // in milliseconds
#define LEDPIN2 11
#define LED2DURATION 10000 // in milliseconds

while your LEDs are connected to pins 10 and 11 instead of 12 and 13 like posted in my code example.

Go Up