Two different Blink intervals.....


hi.

working on 2 lane timer. got of done far.

question, have 2 led blinking when 2 timers running. fun , learn, set them @ different intervals: lane1 @ 500ms , lane2 @ 200ms.

if start lane2 blinks @ 200ms when start lane1 blinks same lane1, 500ms.
seem lane1 overides lane2 blinking.  

can't figure out.


code: [select]

#include <liquidcrystal.h>   //seriel lcd here
#include <wire.h>
#include <liquidcrystal_i2c.h>
#define ledtimingpin  10
#define ledreadypin  11
#define ledrunoverpin  12
#define startpin 6
#define stoppin 8
#define resetpin 7

// lane2----------------------------------------------
#define ledtimingpin2 5
#define ledreadypin2  13
#define ledrunoverpin2  9
#define startpin2 2
#define stoppin2 3


liquidcrystal_i2c lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, positive);  // set lcd i2c address

int value = low;                    // previous value of led
int buttonstate1;                   // variable store button state 1
int buttonstate2;                   // variable store button state 2
int lastbuttonstate1;               // variable store last button state 1
int lastbuttonstate2;               // variable store last button state 2
int statereset;                     // variable store reset button state
int blinking;                       // condition blinking - timer timing
long interval = 500;                // blink interval - change suit
long previousmillis = 0;            // variable store last time led updated
long starttime ;                    // start time stop watch
long elapsedtime ;                  // elapsed time stop watch
int fractional;                     // variable used store fractional part of time

//lane 2
int value2 = low;                    // previous value of led
int buttonstate3;                   // variable store button state 2
int buttonstate4;                   // variable store button state 3
int lastbuttonstate3;               // variable store last button state 3
int lastbuttonstate4;               // variable store last button state 4
int blinking2;                       // condition blinking - timer timing
long interval2 = 200;                // blink interval - change suit
long previousmillis2 = 0;            // variable store last time led updated
long starttime2 ;                    // start time stop watch
long elapsedtime2 ;                  // elapsed time stop watch
int fractional2;                     // variable used store fractional part of time


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

  pinmode(ledtimingpin, output);       // sets digital pin output
  pinmode(ledreadypin, output);        // sets digital pin output
  pinmode(ledrunoverpin, output);      // sets digital pin output
  pinmode(startpin, input);            // not necessary, pins default input anyway
  pinmode(stoppin, input);             // not necessary, pins default input anyway
  pinmode(resetpin, input);            // not necessary, pins default input anyway
  digitalwrite(startpin, high);        // turn on pullup resistors. wire button press shorts pin ground.
  digitalwrite(stoppin, high);         // turn on pullup resistors. wire button press shorts pin ground.
  digitalwrite(resetpin, high);

  //lane2
  pinmode(ledtimingpin2, output);       // sets digital pin output
  pinmode(ledreadypin2, output);        // sets digital pin output
  pinmode(ledrunoverpin2, output);      // sets digital pin output
  pinmode(startpin2, input);            // not necessary, pins default input anyway
  pinmode(stoppin2, input);             // not necessary, pins default input anyway
  digitalwrite(startpin2, high);        // turn on pullup resistors. wire button press shorts pin ground.
  digitalwrite(stoppin2, high);         // turn on pullup resistors. wire button press shorts pin ground.

  lcd.begin(16, 2);                      // set lcd's number of columns , rows:
  lcd.print("  slalom timer");         // print message lcd
  lcd.setcursor(0, 1);
  lcd.print("  timer ready!");

  digitalwrite(ledreadypin, high);
  digitalwrite(ledrunoverpin, low);

  //---lane2-
  digitalwrite(ledreadypin2, high);
  digitalwrite(ledrunoverpin2, low);
}


void loop()
{
  buttonstate1 = digitalread(startpin);                  // read start button state , store
  buttonstate2 = digitalread(stoppin);                   // read stop button state , store
  statereset = digitalread(resetpin);                    // read reset button state , store

  if (buttonstate1 == low && lastbuttonstate1 == high)   // check high low transition
  {
    if(!blinking)                                           // if true found new button press while clock not running - start clock
      starttime = millis();                                   // store start time
    blinking = true;                                        // turn on blinking while timing
    delay(5);                                               // short delay debounce switch
    lastbuttonstate1 = buttonstate1;                        // store buttonstate1 in lastbuttonstate1, compare next time

    //lcd.clear();
    lcd.setcursor(0, 0);
    lcd.print("l1 running....");
    digitalwrite(ledreadypin, low);

  }

rest of code
code: [select]
else if (buttonstate2 == low && lastbuttonstate2 == high)// check high low transition
  {
    if(blinking)                                           // if true found new button press while clock running - stop clock , report
      elapsedtime =   millis() - starttime;                  // store elapsed time
    blinking = false;                                      // turn off blinking, done timing
    lastbuttonstate2 = buttonstate2;                       // store buttonstate2 in lastbuttonstate2, compare next time

    // routine report elapsed time
    serial.print( (int)(elapsedtime / 1000l));             // divide 1000 convert seconds - cast int print
    serial.print(".");                                     // print decimal point

      fractional = (int)(elapsedtime % 1000l);              // use modulo operator fractional part of time

    if (fractional == 0)                                  // pad in leading zeros - wouldn't nice if
      serial.print("000");                                // add 3 zero's
    else if (fractional < 10)                             // if fractional < 10 0 ignored giving wrong time, add zeros
      serial.print("00");                                 // add 2 zeros
    else if (fractional < 100)
      serial.print("0");                                  // add 1 zero
    serial.println(fractional);                            // print fractional part of time

    //lcd l1
    lcd.setcursor(0, 0);  
    lcd.print("l1:  ");
    lcd.setcursor(5, 0);
    lcd.print((int)(elapsedtime / 1000));
    lcd.print(":");
    lcd.print(fractional);
    lcd.print(" ms");

    digitalwrite(ledrunoverpin, high);
  }
  else
  {
    lastbuttonstate1 = buttonstate1;                         // store buttonstate in lastbuttonstate, compare next time
    lastbuttonstate2 = buttonstate2;                         // store buttonstate in lastbuttonstate, compare next time
  }

  // blink routine - blink led while timing

  if ( (millis() - previousmillis > interval) )
  {
    if (blinking == true)
    {
      previousmillis = millis();                         // remember last time blinked led

      // if led off turn on , vice-versa.
      if (value == low)
        value = high;
      else
        value = low;
      digitalwrite(ledtimingpin, value);
    }
    else
    {
      digitalwrite(ledtimingpin, low);                         // turn off led when not blinking
    }

    //lane2

    // check button press
    buttonstate3 = digitalread(startpin2);                  // read start button state , store
    buttonstate4 = digitalread(stoppin2);                   // read stop button state , store


    if (buttonstate3 == low && lastbuttonstate3 == high)   // check high low transition
    {
      if(!blinking2)
        // if true found new button press while clock not running - start clock

      starttime2 = millis();                                   // store start time
      blinking2 = true;                                        // turn on blinking while timing
      delay(5);                                               // short delay debounce switch
      lastbuttonstate3 = buttonstate3;                        // store buttonstate1 in lastbuttonstate1, compare next time

      //lcd.clear();
      lcd.setcursor(0, 1);
      lcd.print("l2 running....");
      digitalwrite(ledreadypin2, low);

    }

    else if (buttonstate4 == low && lastbuttonstate4 == high)// check high low transition
    {
      if(blinking2)      
        // if true found new button press while clock running - stop clock , report

      elapsedtime2 =   millis() - starttime2;              // store elapsed time
      blinking2 = false;                                  // turn off blinking, done timing
      lastbuttonstate4 = buttonstate4;                   // store buttonstate4 in lastbuttonstate, compare next time

      // routine report elapsed time
      serial.print( (int)(elapsedtime2 / 1000l));         // divide 1000 convert seconds - cast int print

      serial.print(".");                             // print decimal point

        // use modulo operator fractional part of time
      fractional2 = (int)(elapsedtime2 % 1000l);

      // pad in leading zeros - wouldn't nice if
      // arduino language had flag this? :)
      if (fractional2 == 0)
        serial.print("000");      // add 3 zero's
      else if (fractional2 < 10)    // if fractional < 10 0 ignored giving wrong time, add zeros
        serial.print("00");       // add 2 zeros
      else if (fractional2 < 100)
        serial.print("0");        // add 1 zero

      serial.println(fractional2);  // print fractional part of time


      lcd.setcursor(0, 1);
      lcd.print("l2:  ");
      lcd.setcursor(5, 1);
      lcd.print((int)(elapsedtime2 / 1000));
      lcd.print(":");
      lcd.print(fractional2);
      lcd.print(" ms");
      digitalwrite(ledrunoverpin2, high);  //red led l2 lights - run over
    }

    else
    {
      lastbuttonstate3 = buttonstate3;                         // store buttonstate in lastbuttonstate, compare next time
      lastbuttonstate4 = buttonstate4;                         // store buttonstate in lastbuttonstate, compare next time
    }


    if (statereset == low) {
      blinking = false;
      lcd.clear();
      lcd.print("  slalom timer");
      lcd.setcursor(0, 1);
      lcd.print("  timer ready!");
      digitalwrite(ledreadypin, high);
      digitalwrite(ledrunoverpin, low);

      // lane 2-
      if (blinking2 = false);
      digitalwrite(ledreadypin2, high);
      digitalwrite(ledrunoverpin2, low);
    }

    // blink routine - blink led while timing

    if ( (millis() - previousmillis2 > interval2) )
    {
      if (blinking2 == true)
      {
        previousmillis2 = millis();                         // remember last time blinked led

        // if led off turn on , vice-versa.
        if (value2 == low)
          value2 = high;
        else
          value2 = low;
        digitalwrite(ledtimingpin2, value2);
      }
      else
      {
        digitalwrite(ledtimingpin2, low);                         // turn off led when not blinking
      }
    }
  }
}


Arduino Forum > Using Arduino > Programming Questions > Two different Blink intervals.....


arduino

Comments

Popular posts from this blog

invalid use of void expresion in FlexiTimer2 library

error: a function-definition is not allowed here before '{' token

LED Strip Code