Flora with a Momentary Switch
Moderators: adafruit_support_bill, adafruit

Flora with a Momentary Switch

by joshuakane on Thu May 02, 2013 9:05 am

Hello,

I was going to make a wearable project with a momentary switch like this one

http://www.adafruit.com/products/1119

For wiring it up I was going to Wire one side of the switch to the VBatt on the FLora the other side of the switch would go to "D6" and the ground. There would be a 10K ohm,1/4 watt resistor along the Ground line. Is this the correct configuration for this?

I guess the circuit diagram would look like this.

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

Re: Flora with a Momentary Switch

by adafruit_support_bill on Thu May 02, 2013 9:11 am

There is a better way to do it. The processor has internal pullup resistors which can be enabled in code. http://arduino.cc/en/Reference/PinMode

So you can eliminate the resistor from your circuit and wire the switch between the digital input pin and ground. Of course now the logic is inverted: The switch input will read HIGH when open due to the pullup and LOW when off because it is shorted to ground.
User avatar
adafruit_support_bill
 
Posts: 31033
Joined: Sat Feb 07, 2009 10:11 am

Re: Flora with a Momentary Switch

by joshuakane on Thu May 02, 2013 9:25 am

Thanks Bill!

If I understand correctly then I should be able to use that by doing something like this in the code.

Code: Select all | TOGGLE FULL SIZE
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_FloraPixel.h>

#define LEDS 7

int power = LEDS;

//
// LEDS
//
int stripDataPin  = 10;    // Yellow wire on Adafruit Pixels
int stripClockPin = 9;    // Green wire on Adafruit Pixels

// Set the first variable to the NUMBER of pixels. 25 = 25 pixels in a row
Adafruit_FloraPixel strip = Adafruit_FloraPixel(8);

//
// Momentary Switch Pin
// constants won't change. They're used here to
// set pin numbers:

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

int buttonState = 0;         // variable for reading the pushbutton status


void setup()
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output

  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(1000);                  // waits for a second


  // Update the strip, to start they are all 'off'
  strip.show();
  //This should power up the Pixes, so 6 are set to green and 1 is set to blue.
  PowerUp();
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == LOW) {     
    // turn LED on:   
    Pulse();
    PowerUp(); 
  }
}
/* Helper functions */

// Create a 24 bit color value from R,G,B
RGBPixel Color(byte r, byte g, byte b)
{
  RGBPixel p;

  p.red = r;
  p.green = g;
  p.blue = b;

  return p;
}


// fill the dots one after the other with said color
// good for testing purposes
void colorWipe(RGBPixel c, uint8_t wait) {
  int i;

  for (i=0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}


void PowerUp(){
  int MAX = LEDS;
  int MIN = 0;
  if(LEDS >= MIN && LEDS < MAX){ //Powerup, and set to green
    MIN++;
    colorWipe(Color(0, 255, 0),50);  // green fill
    strip.show();
  }
  if (LEDS >= MAX){ // Powerup, and set to green
    colorWipe(Color(0, 128, 255),50);  // green blue
    strip.show();
  }
}

void Pulse()
{
  float in, out;
  int LEDNumber = 7;

  // rising
  for (in = 4.712; in < 7.854; in = in + 0.001)
  {
    out = sin(in) * 127.5 + 127.5;
    analogWrite(LEDNumber,out);
    delay(1);
  }

  delay(1000); // pause at peak brightness

  // falling
  for (in = 1.570; in < 4.712; in = in + 0.001)
  {
    out = sin(in) * 127.5 + 127.5;
    analogWrite(LEDNumber,out);
    delay(1);
  }

  delay(1000); // pause while dark
}

void PowerDown(){
  int i;
  //Green Leds
  if(power <= 6 && power > 2){
    for (i=0; i < power; i++) {
      strip.setPixelColor(i,Color(0, 255, 0));
      strip.show();
      delay(5);
    }

    for (i=power; i < 6; i++) {
      strip.setPixelColor(i,Color(0, 0, 0));
      strip.show();
      delay(5);

    }
  }


  //Red
  if(power <= 2){
    for (i=0; i < power; i++) {
      strip.setPixelColor(i,Color(255, 0, 0));
      strip.show();
      delay(5);
      Serial.println("PowerDown");
    }

    for (i=power; i < 6; i++) {
      strip.setPixelColor(i,Color(0, 0, 0));
      strip.show();
      Serial.println("PowerDown");
      delay(5);
    }
  }
  delay(5000);

  // Green
  if(power >= 3 && power <= 6){
    for (i=0; i < power; i++) {
      strip.setPixelColor(i,Color(0, 255, 0));
      strip.show();
      delay(5);
      Serial.println("PowerUp");
    }
  }


  //Red
  if(power <= 3){
    for (i=0; i < power; i++) {
      strip.setPixelColor(i,Color(255, 0, 0));
      strip.show();
      delay(5);
      Serial.println("PowerUp");
    }
  }
}


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

Re: Flora with a Momentary Switch

by adafruit_support_bill on Thu May 02, 2013 9:30 am

You need to enable the pullup in your setup too: http://arduino.cc/en/Reference/PinMode

Code: Select all | TOGGLE FULL SIZE
pinMode(buttonPin, INPUT_PULLUP);
User avatar
adafruit_support_bill
 
Posts: 31033
Joined: Sat Feb 07, 2009 10:11 am

Re: Flora with a Momentary Switch

by joshuakane on Thu May 02, 2013 10:09 am

Got it!

Changed the code to reflect that.


Code: Select all | TOGGLE FULL SIZE
void setup()
{
  pinMode(buttonPin, INPUT_PULLUP);      // sets the digital pin as input

  digitalWrite(buttonPin, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(buttonPin, LOW);    // sets the LED off
  delay(1000);                  // waits for a second


  // Update the strip, to start they are all 'off'
  strip.show();
  //This should power up the Pixes, so 6 are set to green and 1 is set to blue.
  PowerUp();
}
User avatar
joshuakane
 
Posts: 188
Joined: Sat Apr 13, 2013 4:40 pm