Timer project

Hi,

as a newbie I need some help.

I would need a timer that when you start it with push button it turns led from state one to another and starts 7 sec timer when 7 sec is gone it turns led back to original state and starts 3 sec timer.
This 7 sec - 3 sec should be repeated 5 times. Then from pushing the button sequence should start again.

Timo

In words, write the exact sequence of occurrences.
What is your background?
Is this a school project?

Review:
http://forum.arduino.cc/index.php?topic=261445.0

Hi Larry,

This is just my own project.

In words:

push button green led turns to red 7 sec delay led turns green 3 sec delay led turns red 7 sec delay led turns green 3 sec delay led turns red 7 sec delay led turns green 3 sec delay led turns red 7 sec delay led turns green 3 sec delay led turns red 7 sec delay led turns green 3 sec delay led turns red 15 sec delay led turns green. You can start sequence again by push button.

Timo

You can do something along the lines of press the button and enter a while loop. Inside you have a simple variable that increments by 1 all the way to 5 every ten seconds. (I chose to start at 5 and decrement instead)

Now out of those ten seconds, you have an IF statement looking at the seconds increase. First you turn on the red LED and once the seconds is greater than 7, you turn off the red LED and turn on the green LED.

Something like this, (this was not tested). Make sure your button is wired to be brought to ground when pressed

#define RED_ON do{digitalWrite(redLED, HIGH);digitalWrite(greenLED, LOW);}while(0)
#define GRN_ON do{digitalWrite(redLED, LOW);digitalWrite(greenLED, HIGH);}while(0)
#define BothOff do{digitalWrite(redLED, LOW);digitalWrite(greenLED, LOW);}while(0)

const byte buttonPin = 2;
const byte redLED = 3;
const byte greenLED =4;

unsigned long prevTime = 0;
byte seconds = 0;
byte cycle = 5;

void setup() {
  // put your setup code here, to run once:
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(buttonPin) == LOW) // if the button was pressed ie brought to GND, go in
  {
    prevTime = micros(); // get the current time in microseconds
    cycle = 5; // reset cycle to 5
    
    while (cycle != 0) // keep checking until cycle == 0
    {
      if (micros() - prevTime >= 1000000UL) // 1 second timer
      {
        seconds++; // increment the variable "seconds" by 1
        
        if (seconds > 10) // if seconds is greater than 10, reset it back to 0 and decrement cycle by 1
        {
          seconds = 0;
          cycle--;
        }
        
        prevTime += 1000000UL; // increment prevTime by the interval of 1 second (1000000 microseconds)
        
        if (seconds < 7) // if seconds is less than 7, turn on red LED
          RED_ON;
        else // otherwise turn on green LED
          GRN_ON;
      }
    }
    BothOff;
  }
}

*You might need to play with the cycle and seconds numbers. I think they may be 1 too many.

Here is code using the State Machine concept.
You will have to add some code to finish your sequence.

//State Machine example
//LarryD

unsigned long TaskWait;
unsigned long currentmillis; 

const byte redLED      = 2;
const byte greenLED    = 3;
const byte startSwitch = 4;

//define the available machine states that we can have for this sketch
//add more states as needed
enum States{
  stateZERO, stateONE, stateTWO, stateTHREE, stateFOUR, stateFIVE, stateSIX, stateSEVEN, stateEIGHT, stateTEN};

//mState = machine State
States mState = stateZERO;              //we start out in this machine state 

void setup()
{
  Serial.begin(9600);

  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  //start with the green LED on
  digitalWrite(greenLED,HIGH);           //anode to the pin, cathode to 220 ohm resistor, other end of 220 to GND

  pinMode(startSwitch, INPUT_PULLUP);    //one side of my switch is connected to GND, the other to the pin

  TaskWait = millis();                   //initialize the next wait time
  mState = stateZERO;                    //we start out in this machine state 

} //  >>>>>>>>>>>>>> E N D   O F   s e t u p ( ) <<<<<<<<<<<<<<<<<


void loop()
{
  //leave this line of code at the top
  currentmillis  = millis();

  //***************************


  //Put your non-blocking regular stuff here


  //****************************************************************
  //Check machine state and do things that must be done accordingly 
  switch (mState)
  {
    //***************************
  case stateZERO:
    {
      //let us see if the switch is pushed
      if(!digitalRead(startSwitch))
      {
        digitalWrite(greenLED,LOW); //this LED OFF
        digitalWrite(redLED,HIGH);  //this LED ON

        TaskWait = millis(); //initialize the next wait time
        mState = stateONE; //advance to the next state
      }
    }
    beak; //end case stateZERO:

    //***************************
  case stateONE: 
    {
      if (CheckTime(TaskWait, 7000UL))
      {
        digitalWrite(greenLED,HIGH); //this LED ON
        digitalWrite(redLED,LOW);    //this LED OFF

        TaskWait = millis(); //initialize the next wait time
        mState = stateTWO;   //advance to the next state        
      }
    }
    break; //end case stateONE:

    //***************************
  case stateTWO:
    {
      if (CheckTime(TaskWait, 3000UL)) 
      {
        digitalWrite(greenLED,LOW); //this LED OFF
        digitalWrite(redLED,HIGH);  //this LED ON

        TaskWait = millis(); //initialize the next wait time
        mState = stateTHREE; //advance to the next state
      }
    }
    break;  //end of stateTWO

    //***************************
  case stateTHREE:
    {
      if (CheckTime(TaskWait, 7000UL)) 
      {
        digitalWrite(greenLED,HIGH); //this LED ON
        digitalWrite(redLED,LOW);    //this LED OFF

        TaskWait = millis(); //initialize the next wait time
        mState = stateFOUR;  //advance to the next state
      }
    }
    break;  //end of stateTHREE

    //***************************
  case stateFOUR:
    {
      if (CheckTime(TaskWait, 3000UL)) 
      {
        digitalWrite(greenLED,LOW); //this LED OFF
        digitalWrite(redLED,HIGH);  //this LED ON

        TaskWait = millis(); //initialize the next wait time
        mState = stateFIVE;  //advance to the next state
      }
    }
    break;  //end of stateFOUR

    //***************************
  case stateFIVE:
    {
      if (CheckTime(TaskWait, 7000UL)) 
      {
        digitalWrite(greenLED,HIGH); //this LED ON
        digitalWrite(redLED,LOW);    //this LED OFF

        TaskWait = millis(); //initialize the next wait time
        mState = stateZERO;  //advance to the next state  <------<<<<  Change to stateSIX
      }
    }
    break;  //end of stateFIVE

    //Y O U R   T U R N   T O   A D D   S O M E   S T A T E S 

  } // end of switch(mState)


} //   >>>>>>>>>>>>>> E N D  O F  l o o p ( ) <<<<<<<<<<<<<<<<<


//======================================================================
//                       F U N C T I O N S
//======================================================================

//***************************
//Delay time expired function
boolean CheckTime(unsigned long &lastMillis, unsigned long wait) 
{
  //is the time up for this task?
  if (currentmillis - lastMillis >= wait) 
  {
    lastMillis += wait;  //get ready for the next iteration

    return true;
  }

  return false;
} //END of CheckTime()

//***************************

//======================================================================
//                       E N D   O F   C O D E
//======================================================================

.

Hi Larry,

Something I can't figure out from the code.

Where do you connect switch and which outputs are for leds?

Best regards

Timo McKeown

redLED is D2
greenLED is D3
startSwitch is D4

Hi Newbie

I really like newbies. And they need simple answers.

So my code is just doing what you are looking for without any state-of-the art code.

Read my comments in the code and you will understand how it works.

If you need LED timing AND other code execution look for things like "blink without delay".

I think my code will just do what you asked for.

I just set up the hardware and tested the code - so this does work - according to German style

arduinoaleman - June 2015

// simple code by arduinoaleman
//
// codestructure is optimized for understanding by newbies and nor for efficiency
// 
// HARDWARE SETUP:
//
// connect your LEDS to your output pins using 220 ohms resistors
//
// connect your switch to your INPUT pin and ground
// also connect a 10 kilo ohms resistor from your INPUT pin to +5 Volts


const int LED_green = 8;        // use your own pin numbers here
const int LED_red = 9;
const int inputswitch = 3;

void setup() {
  pinMode (LED_green, OUTPUT);
  pinMode (LED_red, OUTPUT);
  pinMode (inputswitch, INPUT);    // this line is not required - just tells you it is INPUT (default setting)
  digitalWrite (LED_red,LOW);
  digitalWrite (LED_green,LOW);
}

void loop() {
  
  while (digitalRead(inputswitch)==HIGH);          // wait until switch is pressed
    
  for (int number=1; number <=5; number=number+1)  {     // this loop will execute the following code 5 times
    
    digitalWrite (LED_red,HIGH);      // switch ON red LED
    delay (7000);                     // wait 7000 milliseconds = 7 seconds
    digitalWrite (LED_red,LOW);      // switch OFF red LED    
    
    digitalWrite (LED_green,HIGH);      // switch ON green LED    
    delay (3000);                     // wait 3000 milliseconds = 3 seconds
    digitalWrite (LED_green,LOW);      // switch OFF green LED    
  }                                      // end of loop ("number" is how often you want to reapeat the loop
 }

@arduinoaleman
The point is non blocking code.

The forum tries to present techniques that are transferable to future and likely more advanced sketches.

.

tmckeown:
Hi,

as a newbie I need some help.

I would need a timer that when you start it with push button it turns led from state one to another and starts 7 sec timer when 7 sec is gone it turns led back to original state and starts 3 sec timer.
This 7 sec - 3 sec should be repeated 5 times. Then from pushing the button sequence should start again.

Timo

What happens if you push the button during the sequence? Do you want the sequence to continue normally? Or do you want it to start over from the beginning? Or what?

LarryD:
@arduinoaleman
The point is non blocking code.

The forum tries to present techniques that are transferable to future and likely more advanced sketches.

.

Newbies are NOT looking for advanced code. Just keep simple things as simple as possible.

In this case the guy who looked for help told us EXACTLY what he needs.

In most cases people cannot even describe what they need/want (that is a different story).

the original post screams school work.

the OP did not offer any reason why it needs to be 7 seconds, why it needs to repeat 5 times.

anyone who is working on a project and comes up with such an idea is mote than able to write it themselves.

I would have to side with arduinoaleman on this. dead simple.

all the OP has to do is to pass in the homework.

the code works, nothing more is needed.

why swap to something that is not needed ? why get rid of delay ?

did you know the bootloader suckes up a huge amount of memory ?
does that mean that we all should get rid of it ?

C++ is much slower than asm. doesn't that also dictate that we must stop using C++ because somewhere out there, there is something faster ?

now, writing to the 328 without a bootloader using asm, to time out to 7... man that is the most ut.

Hi Larry,

I'm using Arduino Nano and for some reason it burns red and green led at the same time, but not green at all on it own state?!
Any ideas or suggestions?

Timo

We need to see a photo of your wiring.
Are you using the sketch in reply #4.

Your switch should be between D4 and GND.

Hi Larry,

I've connected them as follows:

D4 switch, switch between gnd and D4, also Vin+ thru 10Kohm resistor to D4
D3 green led, thru resistor to gnd
D2 red led, thru resistor to gnd

Vin +
Gnd -

Timo

tmckeown:
Hi Larry,

I've connected them as follows:

D4 switch, switch between gnd and D4, also Vin+ thru 10Kohm resistor to D4
D3 green led, thru resistor to gnd
D2 red led, thru resistor to gnd

Vin +
Gnd -

Timo

There is an internal pull-up resistor on D4 already so you don't need the 10K (wont hurt though).
LED Anode to the pin, cathode to 220 ohm resistor, other end of 220 to GND

  1. Are you using the Sketch in reply #4?
  2. Will you show us a photo of your wiring?

There is pictures. Yes I copied that code straight from #4 just to test it.

Timo

Try this, I took out the while loop.

#define RED_ON do{digitalWrite(redLED, HIGH);digitalWrite(greenLED, LOW);}while(0)
#define GRN_ON do{digitalWrite(redLED, LOW);digitalWrite(greenLED, HIGH);}while(0)
#define BothOff do{digitalWrite(redLED, LOW);digitalWrite(greenLED, LOW);}while(0)

const byte buttonPin = 2;
const byte redLED = 3;
const byte greenLED = 4;

unsigned long prevTime = 0;
byte seconds = 0;
byte cycle = 5;

void setup() {
  // put your setup code here, to run once:
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(buttonPin) == LOW) // if the button was pressed ie brought to GND, go in
  {
    prevTime = micros(); // get the current time in microseconds
    cycle = 5; // reset cycle to 5
  }

  if (cycle != 0) // keep checking until cycle == 0
  {
    if (micros() - prevTime >= 1000000UL) // 1 second timer
    {
      prevTime += 1000000UL; // increment prevTime by the interval of 1 second (1000000 microseconds)

      seconds++; // increment the variable "seconds" by 1

      if (seconds > 10) // if seconds is greater than 10, reset it back to 0 and decrement cycle by 1
      {
        seconds = 0;
        cycle--;
      }

      if (seconds < 7) // if seconds is less than 7, turn on red LED
        RED_ON;
      else // otherwise turn on green LED
        GRN_ON;
    }
  }
  else
    BothOff;
}

The code works on an UNO as you requested.

The picture in Dropbox is to dark.

Ribbon cable jumpers: rip off some of them, use them as separte conductors.

Hi Larry,

There was something funky going on with veroboard....

Timo