Code Question: Multiple modes via a push button

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
joshuakane
 
Posts: 282
Joined: Sat Apr 13, 2013 4:40 pm

Code Question: Multiple modes via a push button

Post by joshuakane »

Hello:

I have been playing with the NeoPixels and Trinket for a while now to make some pretty cool blinky things :-)

I wanted to try to make something that I could have the Pixels Cycle through colors at the push of a button. They would start Red, then with a button push go to Orange, another push of the button would go to Yellow, and so on.

What would the best way to code this be so I could go through all 7 colors and then cycle back to the original color? Also any recomendations on a button? Can I use a momentary button, or something like a slide on/off switch?

I currently have a project that uses a slide on/off to toggle modes, I will copy the example code below.

Thanks,

Joshua

Code: Select all

//NeoPixel LED HALO Sword with Switch Mode
#include <Adafruit_NeoPixel.h>

//Settings:
#define PIN 0 //The Pin out your Neopixel DIN strip/stick is connected to (Default is 6)
#define TPIXEL 23 //The total amount of pixel's/led's in your connected strip/stick 
//To change the timing of between pulses change the number in int 'refresh', to change the speed of it scrolling change the number in int 'wait_T' and 1000 is equal to 1second. 
int wait_T=40; //This is the delay between moving back and forth and per pixel
int refresh=400;
int PixelCount=23; //Set this to the AMOUNT of Led's/strip you have or want to use on your strip And It can be used to tell where to Stop then return the eye at in the strip
int Pixel_Start_End=0; //Set this to where you want it to Start/End at
boolean UsingBar = false; //Set this to true If you are using the 8x1 Neopixel Bar Or you want to only use 3 leds for the scanner. 

Adafruit_NeoPixel strip = Adafruit_NeoPixel(23, PIN);

//
// RF Pins
//


int inPinA = 2;             // the number of the input pin (RX) - Button "A"
int counterA = 0;           // how many times we have seen new value    
int readingA;               // the current value read from the input pin A
int current_stateA = LOW;   // the debounced input value



// the following variable is a long because the time, measured in milliseconds,
// will quickly become a bigger number than can be stored in an int.
long timeA = 0;         // the last time the output pin was sampled

int debounce_count = 2; // number of millis/samples to consider before declaring a debounced input

void setup()
{
  strip.begin();
  colorWipe(strip.Color(0, 255, 32), 0); // Red //Default Seagreen for Powerup.
  strip.show(); // Initialize all strip to 'off'
  int i;
  pinMode(inPinA, INPUT);
 
}
void loop()
{
    if (digitalRead(inPinA) == HIGH)
    {
     Do Stuff;
    }
    else
    {
     Do Other Stuff;
    }
}


// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Code Question: Multiple modes via a push button

Post by adafruit_support_bill »

There are lots of ways to do it. Here's one:

Code: Select all

int mode = 0;  // initialize to mode 0

void loop()
{
    if (digitalRead(inPinA) == HIGH) // detect button press
    {
        mode = (mode + 1) % 7;  // count up modulo-7
    }
    switch(mode)
    {
        case 0: // do stuff 0
            break;
        case 1: // do stuff 1
            break;
        case 2: // do stuff 2
            break;
        case 3: // do stuff 3
            break;
        case 4: // do stuff 4
            break;
        case 5: // do stuff 5
            break;
        case 6: // do stuff 6
            break;
        default:
            break;
     }
}

User avatar
Franklin97355
 
Posts: 23911
Joined: Mon Apr 21, 2008 2:33 pm

Re: Code Question: Multiple modes via a push button

Post by Franklin97355 »

You need to create a "state machine" In your loop() test for the switch press and if it (after debounce) is HIGH then move the state up one and run that part of the program.

User avatar
joshuakane
 
Posts: 282
Joined: Sat Apr 13, 2013 4:40 pm

Re: Code Question: Multiple modes via a push button

Post by joshuakane »

Thank you Franklin and Bill!

Do you guys have a suggestion on a button type to use?

would you suggest something like this?

http://www.radioshack.com/product/index ... &gclsrc=ds

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Code Question: Multiple modes via a push button

Post by adafruit_support_bill »

For that kind of application, most any button will do. Arcade buttons are fun if you have the space for them. https://www.adafruit.com/search?q=arcade+button&b=1
Lesson 6 in the Learning System is a good reference for wiring and programming buttons: https://learn.adafruit.com/adafruit-ard ... tal-inputs

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”