Two buttons and an led sequence control

Hey guys, I’m working on a project that warrants running a button sequence(along with the timing) on an LED. When the first button is pressed, the arduino starts reading the status of the second button. The pressing sequence on the second button is stored until the first button is pressed again. now, the button sequence that was stored, is replicated on the LED. I came up with a code that tries to do the basic functionality except the LED part. but I’m not getting the desired output. Can you guys check it for me ?

#include <Debounce.h>

const byte SWITCH = 2;
const byte SWITCH1 = 4;
const byte LED = 9;
int count = 0;
unsigned long startTime = 0;
unsigned long endTime = 0;
unsigned long startTime1 = 0;
unsigned long endTime1 = 0;
unsigned long elapsedTime = 0;
unsigned long elapsedTime1 = 0;
const byte debounceDelayTime = 50;

int a[100],i,j,flag = 0,flag1 = 0,b=0;

Debounce debouncer = Debounce(debounceDelayTime, SWITCH);

void setup(){
Serial.begin(9600);
pinMode(SWITCH, INPUT_PULLUP);
pinMode(SWITCH1, INPUT_PULLUP);
pinMode(LED, OUTPUT);
}

void loop(){
fun();
while(count == 1)
{
fun();
if(count == 1)
{
if(debouncer.update())
{
if(debouncer.read() == 1)
{
startTime = millis();
endTime1 = millis();
elapsedTime1 = endTime1 - startTime1;
Serial.println(“Button Pressed”);
Serial.println(“Button Released for:”);
Serial.print(elapsedTime1);

if(flag1 == 0)
{
for(j=b;j<100;j+2)
{
while(flag1 == 0)
{
b=j+2;
a[j] = elapsedTime;
flag1 = 1;
}
}

}
flag1 = 0;
}
else if(debouncer.read() == 0)
{
endTime = millis();
startTime1 = millis();
Serial.println(“Button Released”);
elapsedTime = endTime - startTime;
Serial.println(“Button Held Down For “);
Serial.print(elapsedTime);
Serial.println(” ms.”);

if(flag == 0)
{
for(j=b;j<100;j+2)
{
while(flag == 0)
{
b=j+2;
a[j] = elapsedTime;
flag = 1;
}
}

}
flag = 0;
}
}
}
else
count = 0;
}
Serial.println(“A*=”);*
for(i=0;i<100;i++)
{ *
_Serial.println(a
);_
_
}_
_
}_
void fun()
_
{_
_
if (digitalRead(SWITCH1) == 1)_
_
{_
_
count++;_
_
while (digitalRead(SWITCH1) == 1);_
_
{_
_
delay(75);_
_
}_
_
}_
_
else*_
* count = 1;*
}

Get TDuino and try this:

#include <TDuino.h>

#define BUTTON1_PIN 3
#define BUTTON2_PIN 4
#define LED_PIN 5
#define PATTERN_MAX 100

//Maximum pattern capacity = 100 changes with max. interval of 65 seconds
unsigned int pattern[PATTERN_MAX] = {0};
unsigned long pattern_start = 0;
bool reading = false;
byte pattern_index = 0;
TButton buttons[2];

void showPattern()
{
   byte state = 0;
   for (byte i = 0; i < pattern_index; i++)
   {
      digitalWrite(LED_PIN, state);
      delay(pattern[i]);
      state ^= 1;
   }
   digitalWrite(LED_PIN, 0);
}

void buttonHandler(byte pin, int state)
{
  if (pin == BUTTON1_PIN)
  {
    reading = !reading;
    if (reading)
    {
      pattern_index = 0;
      pattern_start = millis();
    }
    else showPattern();
  }
  else if (reading && (pattern_index < PATTERN_MAX))
  {
    pattern[pattern_index++] = millis() - pattern_start;
    pattern_start = millis(); //Added after edit
  }
}

void setup()
{
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
  buttons[0].attach(BUTTON1_PIN);
  buttons[0].onPress(buttonHandler);
  buttons[1].attach(BUTTON2_PIN);
  buttons[1].onPress(buttonHandler);
  buttons[1].onRelease(buttonHandler);
}

void loop()
{
   buttons[0].loop();
   buttons[1].loop();
}

Info: The code is untested.

EDIT: Code modified, see comment(s)

Thanks @Danoid90, this seems to be correct, Will try this and get back to you.

In your private message, it did not work.. Does it work or not? Please do not post private messages..

The code compiles perfectly but I’am not getting any output. Sorry for the private message Sir.

This is the code i compiled:

#include <TBase.h>
#include <TButton.h>
#include <TDefs.h>
#include <TDuino.h>
#include <TPin.h>
#include <TPinInput.h>
#include <TPinOutput.h>
#include <TTimeline.h>
#include <TTimer.h>

int BUTTON1_PIN = 7;
int BUTTON2_PIN = 10;
int LED_PIN = 12;
const int PATTERN_MAX = 100;

//Maximum pattern capacity = 100 changes with max. interval of 65 seconds
unsigned int pattern[PATTERN_MAX] = {0};
unsigned long pattern_start = 0;
bool reading = false;
byte pattern_index = 0;
TButton buttons[2];

void showPattern()
{
byte state = 0;
for (byte i = 0; i < pattern_index; i++)
{
digitalWrite(LED_PIN, state);
delay(pattern*);*

  • state ^= 1;*
  • }*
  • digitalWrite(LED_PIN, 0);*
    }
    void buttonHandler(byte pin, int state)
    {
  • if (pin == BUTTON1_PIN)*
  • {*
  • reading = !reading;*
  • if (reading)*
  • {*
  • pattern_index = 0;*
  • pattern_start = millis();*
  • }*
  • else showPattern();*
  • }*
  • else if (reading && (pattern_index < PATTERN_MAX))*
  • {*
  • pattern[pattern_index++] = millis() - pattern_start;*
  • pattern_start = millis();*
  • }*
    }
    void setup()
    {
  • pinMode(LED_PIN, OUTPUT);*
  • digitalWrite(LED_PIN, LOW);*
  • buttons[0].attach(BUTTON1_PIN);*
  • buttons[0].onPress(buttonHandler);*
  • buttons[1].attach(BUTTON2_PIN);*
  • buttons[1].onPress(buttonHandler);*
  • buttons[1].onRelease(buttonHandler);*
  • Serial.begin(9600);*
    }
    void loop()
    {
  • buttons[0].loop();*
  • buttons[1].loop();*
  • Serial.println(pattern_index);*
    }

Check your wiring.. The buttons must be connected as "Arduino PIN => button in => button out => GND", and make sure that you are using the correct pins or change the #define's in the sketch.

Sure sir, will check the connections again. Can you please give me a short commented explanation of the above code ? Some of these statements are new and will help me understand it better.

I've just tested the code on a Nano. Other than 3 (now corrected) typos (stray ";", 2 missing "s" in "buttons") it all works exactly as it should.