Go Down

Topic: Two buttons and an led sequence control (Read 163 times) previous topic - next topic

Vishwa98

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;
}


Danois90

#1
Jul 13, 2018, 03:32 pm Last Edit: Jul 15, 2018, 02:32 pm by Danois90
Get TDuino and try this:

Code: [Select]
#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)
Instead of mocking what's wrong, teach what's right! ;)
Do NOT send me any private messages!!

Vishwa98

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

Danois90

In your private message, it did not work.. Does it work or not? Please do not post private messages..
Instead of mocking what's wrong, teach what's right! ;)
Do NOT send me any private messages!!

Vishwa98

#4
Jul 14, 2018, 01:46 pm Last Edit: Jul 14, 2018, 01:50 pm by Vishwa98
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);
}

Danois90

#5
Jul 14, 2018, 01:52 pm Last Edit: Jul 14, 2018, 01:52 pm by Danois90
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.
Instead of mocking what's wrong, teach what's right! ;)
Do NOT send me any private messages!!

Vishwa98

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.

Danois90

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.
Instead of mocking what's wrong, teach what's right! ;)
Do NOT send me any private messages!!

Go Up