Pages: [1]   Go Down
Author Topic: How to set a few programms to ONE BUTTON. LED POLICE FLASHER  (Read 1542 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So, can anyone tell me how to set a few programs to one button, I work on some LED police flasher and i have a many programs that loops throught program and that become boring, so i think that will be better to put one button inside circuit and set all this programs to him if that is possible???  maybe there are some function like counting presses or something else with i can set all that programs but i don't know which is!    smiley
SO IF ANYONE KNOWS!!! PLEASE HELPP

by: Berni
« Last Edit: July 29, 2012, 11:57:21 am by Bro2131 » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8854
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe there are some function like counting presses or something else with i can set all that programs

That's correct!  Each time you detect the button press you switch to the next pattern.  If you are already at the last pattern you switch back to the first pattern.  Like this:

Code:
const int patternCount = 5;

void loop()
    {
    static int pattern = 0;

    if (digitalRead(buttonPin))
        {
        pattern = (pattern + 1) % patternCount;
        }

    switch (pattern)
        {
    case 0:  doPattern0(); break;
    case 1:  doPattern1(); break;
    case 2:  doPattern2(); break;
    case 3:  doPattern3(); break;
    case 4:  doPattern4(); break;
        }
    }
       
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe there are some function like counting presses or something else with i can set all that programs

That's correct!  Each time you detect the button press you switch to the next pattern.  If you are already at the last pattern you switch back to the first pattern.  Like this:

Code:
const int patternCount = 5;

void loop()
    {
    static int pattern = 0;

    if (digitalRead(buttonPin))
        {
        pattern = (pattern + 1) % patternCount;
        }

    switch (pattern)
        {
    case 0:  doPattern0(); break;
    case 1:  doPattern1(); break;
    case 2:  doPattern2(); break;
    case 3:  doPattern3(); break;
    case 4:  doPattern4(); break;
        }
    }
       

YEAHHH!!  It works really nice!!
THANKS!!!  smiley
but
there is another problem and that is bounce. I write several code with debounce and nothnig doesn't work! really i don't know how to fix it!
if you know, please write!
thanks! smiley
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8854
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    static unsigned long lastButtonTime = 0;

    //  If the button is down and the last time it went down was more than 1/10th second ago.
    if (digitalRead(buttonPin) && (millis() - lastButtonTime) > 100)
        {
        lastButtonTime = millis();
        pattern = (pattern + 1) % patternCount;
        }
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    static unsigned long lastButtonTime = 0;

    //  If the button is down and the last time it went down was more than 1/10th second ago.
    if (digitalRead(buttonPin) && (millis() - lastButtonTime) > 100)
        {
        lastButtonTime = millis();
        pattern = (pattern + 1) % patternCount;
        }


Yeah it works! Thnaks!
 but only first time! I don't know why.  I was changing the time when it went down and i setting like this :

  if (digitalRead(buttonPin) && (millis() - lastButtonTime) > 10) and  nothing happens. it's the same like before.

Maybe I can put it in every case so when i press button he does this, and maybe it will work fine if that is possible? 

What do you think about it?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8854
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If it works at 100 and doesn't work at 10, why not just change it back to 100?

It's not clear what you mean by "but only the first time".  Perhaps you made a mistake. Could you show your sketch?
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

With this "but only the first time" I mean when i turn on arduino and first program running, and when i want to change program from 1 to 2 it works fine, other i need to hold button down for some time to change or i need to press a few times on it.
This is part of sketch! i didn't write all programs because it will be very long.
Code:
const int patternCount = 15;
int LED1=4;
int LED2=5;
int LED3=6;
int LED4=7;
int LED5=8;
int LED6=9;
int LED7=10;
int LED8=11;
int LED9=12;
int LED10=3;
int buttonPin=2;
static unsigned long lastButtonTime = 0;
void setup()
{

  pinMode((LED1,LED2,LED3,LED4,LED5,LED6,LED7,LED8,LED9,LED10),OUTPUT);  // set each LED to pins
  pinMode(buttonPin,INPUT);
 
}


void loop()
    {
    static int pattern = 1; 
//  If the button is down and the last time it went down was more than 1/10th second ago.
   if (digitalRead(buttonPin) && (millis() - lastButtonTime) > 100)
   
        {
        lastButtonTime = millis();
       
        pattern = (pattern + 1) % patternCount;
       
        }
   
    switch (pattern)
        {
 
     
    case 1:
   digitalWrite(LED1,HIGH); 
   digitalWrite(LED2,HIGH); 
   digitalWrite(LED3,HIGH);
   digitalWrite(LED4,HIGH);
   digitalWrite(LED5,HIGH);
   delay(50);
   digitalWrite(LED1,LOW); 
   digitalWrite(LED2,LOW); 
   digitalWrite(LED3,LOW);
   digitalWrite(LED4,LOW);
   digitalWrite(LED5,LOW);
   delay(50);

case 2: (program) break;
case 3: (program) break;
case 4: (program) break;
case 5: (program) break;
case 6: (program) break;
case 7: (program) break;
case 8: (program) break;
case 9: (program) break;
case 10: (program) break;
case 11: (program) break;
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8854
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  pinMode((LED1,LED2,LED3,LED4,LED5,LED6,LED7,LED8,LED9,LED10),OUTPUT);  // set each LED to pins

This probably doesn't do what you intend.  You can't pass a list of pin numbers to pinMode().

You only show one 'case' and you left the 'break;' statement at the end of that off. 

My guess is that some of your patterns take a long time to execute.  If they do, typically because there are delay() calls, you can't change patterns until they return.  The button will have to be down at the time one cycle of the pattern finishes in order to switch to the next pattern.  One way to fix that is to use code from the "BlinkWithoutDelay" example and write your patterns WITHOUT delays.

Code:
void pattern1()
    {
    static int patternStep = 0;
    static unsigned long stepTime = 0;

    switch (patternStep)
        {
    case 0:
        digitalWrite(LED1,HIGH); 
        digitalWrite(LED2,HIGH); 
        digitalWrite(LED3,HIGH);
        digitalWrite(LED4,HIGH);
        digitalWrite(LED5,HIGH);
        patternStep = 1;
        stepTime = millis();
        break;

    case 1:
        if (millis() - stepTime > 50)
            {
            patternStep = 2;
            }
        break;

    case 2:
        digitalWrite(LED1,LOW); 
        digitalWrite(LED2,LOW); 
        digitalWrite(LED3,LOW);
        digitalWrite(LED4,LOW);
        digitalWrite(LED5,LOW);
        patternStep = 3;
        stepTime = millis();
        break;

    case 3:
        if (millis() - stepTime > 50)
            {
            patternStep = 0;
            }
        break;
        }
    }
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, thanks you so much!!! I finished with this police flasher!!
You can watch this on YT :       







Berni
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8854
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wow!  That's neat!  Good work.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah, Thank you so much!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

NOWW WITH CODEEE!!!!!!

You can find everything you need to make one on my BLOG here:

http://electronicandcomputerone.blogspot.com/
« Last Edit: July 23, 2013, 06:30:55 am by Bro2131 » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8854
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

NOWW WITH CODEEE!!!!!!

http://getmyfile.org/file/03n5i0

Unfortunately that site won't let me download the file unless I buy something or give them information with which to spam me:

Complete a Quick Survey to Continue!
 
You'll have your download in no time! Just complete any survey below with your valid information and the download will unlock.

Survey List:
Get a prepaid debit card!
Which is better, Pepsi or Coke?
RealPlayer. FREE Media Player Download!
Get Approved for a Pre-Paid Debit card ASAP!
Download Music Oasis!
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1]   Go Up
Jump to: