Creating timer with sectioned actions (noise and LED flash)

Hello,

I'm starting a project and would like to create a timer with my arduino which creates actions within loops. It will have three buttoned functions:

Function 1:

Triple LED flash (one per second) / Beep on final flash
• counts 60 seconds •
LED Flash
• counts 15 seconds •
LED Flash
• counts 15 seconds •
LED Flash
• counts 15 seconds •
LED Flash
• counts 15 seconds •
Triple LED Flash / Beep on first flash

Function 2 same as function 1

Function 3:
Triple LED flash (one per second) / Beep on final flash
• counts 60 seconds •
LED Flash / Beep
• counts 4 minutes •
Triple LED Flash / Beep on first flash

I haven't really found anything that I can read about this - so if anyone can provide some resources/help it would be much appreciated!

Thanks

Hi,Start with getting LEDS to blink and Buzzers to "Beep"..

See the page HERE: and search on "buzzer".

Then make a test program that does ONE of your functions. Then start to put it all together.

See the demo several things at a time. Most of what you will want is in the first post

This Thread planning and implementing a program may also have some useful stuff.

...R

Ok , so I'm getting going. I've only just started with processing and arduino, but am slowly getting it. I think my code is really sloppy and would appreciate some feedback on how to simplify it.
The circuit seems to begin with the LED always switched on and I am not really sure why and would like to also stop the function if another button is pressed..

const int LED = 9; //LED pin#
const int BUTTON = 2; //BUTTON 1 pin#
const int BUTTON2 = 4; //BUTTON 2 pin#
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = false;

int buttonState1= 0;
int buttonState2= 0;

void setup() {
pinMode(LED,OUTPUT); //LED setup as output
pinMode(BUTTON,INPUT); //BUTTON setup as input
pinMode(BUTTON2,INPUT);

}

boolean debounce(boolean last) {
boolean current = digitalRead(BUTTON);
if(last != current)
{
delay(5);
current = digitalRead(BUTTON);
}
return current;
//
// boolean current = digitalRead(BUTTON_SOLO);
// if(last != current)
// {
// delay(5);
// current = digitalRead(BUTTON_SOLO);
// }
// return current;

}

void loop() {
// currentButton = debounce(lastButton);
// if(lastButton == LOW && currentButton == HIGH)
// {
// ledOn = !ledOn;
//
// }
// lastButton = currentButton;
// if(currentButton == HIGH)
// {
// digitalWrite(LED, ledOn);

buttonState1 = digitalRead(BUTTON);
buttonState2 = digitalRead(BUTTON2);
if (buttonState1 == LOW) {
digitalWrite(LED, LOW); //GET READY
delay(500);
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW); //GET SET
delay(500);
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW); //GO!
delay(500);
digitalWrite(LED, HIGH); //Bloom 50g
delay(60000);
digitalWrite(LED, LOW);
delay(500);
digitalWrite(LED, HIGH); //First Pulse 200g (250g)
delay(14500);
digitalWrite(LED, LOW);
delay(500);
digitalWrite(LED, HIGH); //Second Pulse 50g (300g)
delay(14500);
digitalWrite(LED, LOW);
delay(500);
digitalWrite(LED, HIGH); //Third Pulse 50g (350g)
delay(14500);
digitalWrite(LED, LOW);
delay(500);
digitalWrite(LED, HIGH); //Forth Pulse 65g (415g)
delay(14500);
digitalWrite(LED, LOW); //Flash 1
delay(500);
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW); //Flash 2
delay(500);
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW); //Flash 3
delay(500);
digitalWrite(LED, HIGH);
delay(500);
while(1) { //SWITCH OFF LOOP
}
}
if (buttonState2 == LOW) {
digitalWrite(LED, LOW); //GET READY
delay(500);
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW); //GET SET
delay(500);
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW); //GO!
delay(500);
digitalWrite(LED, HIGH); //Bloom 50g
delay(60000);
digitalWrite(LED, LOW);
delay(500);
digitalWrite(LED, HIGH); //Bloom 50g
delay(240000);
digitalWrite(LED, LOW); //Flash 1
delay(500);
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW); //Flash 2
delay(500);
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW); //Flash 3
delay(500);
digitalWrite(LED, HIGH);
delay(500);
while(1) { //SWITCH OFF LOOP
}

}
}

Looks like you didn't read any of the links I suggested.

You are best never to use the delay() function as the Arduino can do nothing else while the delay period is running.

...R

Hello Robin,

I'm sorry, and I really do appreciate the advice, I'm just trying to pick up enough to get by for this function, but do want to learn more for the future. I will go over the links you suggested - its just when I see so much code I get a bit scared and so try to find a workaround.

I will post back once I have researched further and refined.

Thank you again.

So from looking at that, I think I understand some of it, but there are definitely parts I am confused about.

I changed the code to:

//LEDS
const int LED_ACT = 9;       //LED_ACTION pin#
const int LED_POUR = 11;     //LED_POUR_NOW pin#

//BUTTONS
const int CHEM = 2;          //BUTTON 1 pin# 
const int WAVE = 7;          //BUTTON 2 pin#
const int SOLO = 4;          //BUTTON 3 pin#

//LED Intervals
const int BLOOM = 60000;
const int PULSE = 15000;
const int BREW = 240000;
const int SERVE = 0;

//LED Blink Durations
const int blinkDuration = 500;

//Variables
byte LED_ACT_State = LOW;
byte LED_POUR_State = LOW;

//STORE VALUES OF ITERATIONS IN LOOP
unsigned long currentMillis = 0;
unsigned long previousLED_ACTMillis = 0;   // will store last time the LED was updated
unsigned long previousLED_POURMillis = 0;   // will store last time the LED was updated

//WHEN BUTTON WAS LAST PRESSED
unsigned long previousButtonMillis = 0;


void setup() {
  Serial.begin(9600);     //SETS SERIAL CONNECTION SPEED
  Serial.println("Midterm");  //LOOK IN CONSOLE FOR SKETCH NAME

  pinMode(LED_ACT,OUTPUT);          //LED setup as output
  pinMode(LED_POUR,OUTPUT);          //LED setup as output

  pinMode(CHEM,INPUT);        //BUTTON1 setup as input
  pinMode(WAVE,INPUT);       //BUTTON2 setup as input
  pinMode(SOLO,INPUT);       //BUTTON3 setup as input
  //  pinMode(SOUND, OUTPUT);       //Piezo set up as Output
}

void loop() {
  currentMillis = millis();   // capture the latest value of millis()
  //   this is equivalent to noting the time from a clock
  //   use the same time for all LED flashes to keep them synchronized
  readCHEM ();
  updateLED_ACT_State ();
  updateLED_POUR_State ();
}

void readCHEM(BLOOM, PULSE, PULSE, PULSE, PULSE) {

  if(millis() - previousButtonMillis >= SERVE) {
    if (digitalRead(CHEM)==LOW) {
      digitalWrite(LED_ACT, LOW);
      LED_ACT_STATE = !LED_ACT_STATE
      previousButtonMillis += SERVE;
    }
  }
}

This last part - the function of running the action reading the button CHEM. When the button is pressed, I want it to perform the same as it did in the previous post:

digitalWrite(LED, LOW);    //GET READY
    delay(500);
    digitalWrite(LED, HIGH);  
    delay(200);
    digitalWrite(LED, LOW);    //GET SET
    delay(500);
    digitalWrite(LED, HIGH);
    delay(200);
    digitalWrite(LED, LOW);    //GO!
    delay(500);
    digitalWrite(LED, HIGH);  //Bloom 50g
    delay(60000);
    digitalWrite(LED, LOW);
    delay(500);
    digitalWrite(LED, HIGH);  //First Pulse 200g (250g)
    delay(14500);
    digitalWrite(LED, LOW);
    delay(500);
    digitalWrite(LED, HIGH);  //Second Pulse 50g (300g)
    delay(14500);
    digitalWrite(LED, LOW);
    delay(500);
    digitalWrite(LED, HIGH);  //Third Pulse 50g (350g)
    delay(14500);
    digitalWrite(LED, LOW);
    delay(500);
    digitalWrite(LED, HIGH);  //Forth Pulse 65g (415g)
    delay(14500);
    digitalWrite(LED, LOW);   //Flash 1
    delay(500);
    digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);   //Flash 2
    delay(500);
    digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);   //Flash 3
    delay(500);
    digitalWrite(LED, HIGH);
    delay(500);

But I am having a tough time understanding the logic of the post you pointed out to make the LED work in the same way. As it is now, the LED is just constantly on and I haven't been able to debug the problem!

I don't know why the function is called readCHEM() - but I assume it means something to you.

However, regardless of what it is called don't understand why it has any timing or digitalWrite()s in it. My simple approach is that the readXXX() function should just collect data for other functions to act on.

Is there some pattern to the way the LEDs flash that can be put into a repetitive piece of code?

If not you will need a look that manages the timing using an array of msec values - for example (not complete - but should give the idea)

unsigned long ledInterval[6] = [500,500,200,6000,200,500]; // adjust to taste
byte maxIndx = 6;
byte intervalIndx = 0;
unsigned long ledStartMillis = 0;
unsigned long currentMillis;

void flashLED( ) {

  if (currentMillis - ledStartMillis >= ledInterval [ intervalIndx ] ) {
       byte ledState = ! digitalRead(ledPin);
       digitalWrite(ledPin, ledState); // toggle LED
       if (ledState == 1) {
           intervalIndx ++;
           if (intervalIndx > maxIndx) {
              intervalIndx = 0;
           }
       }
  }
}

...R