Editing an int value with push button (LASER PISTOL)

Hey All,

I have some neopixels mounted in a laser pistol. I am trying to create 'heat glow effect'

Basically I have the colourWipe command, and the brightness setting is a variable integer.

The value starts at zero, but then increases (up to 255) when the trigger is held down.

Once trigger is released, the number goes down. Both increase and decrease speeds I want to control with delays.

I want it to be actively additive.. i.e. i press for enough time for it to be at half brightness, i then release the trigger and it falls to 25%, I then repress the trigger and it goes up again (starting at 25%, not zero).

This seems super simple but I think it wants a primary expression before the int modifier parts of the code.

Any help would be greatly appreciated!

red_button_demo.ino (2.83 KB)

unorthodoxdesign:
(starting at 25%, not zero)

25% = 'current brightness value' whatever that may be.

Instead of uploading the .ino sketch (forcing us to download and open it externally), copy your code and include it in code tags in your post.

That way we can easily read your code along with your text and possibly catch any issues without downloading anything...

// A basic everyday NeoPixel strip test program.

// NEOPIXEL BEST PRACTICES for most reliable operation:
// - Add 1000 uF CAPACITOR between NeoPixel strip's + and - connections.
// - MINIMIZE WIRING LENGTH between microcontroller board and first pixel.
// - NeoPixel strip's DATA-IN should pass through a 300-500 OHM RESISTOR.
// - AVOID connecting NeoPixels on a LIVE CIRCUIT. If you must, ALWAYS
//   connect GROUND (-) first, then +, then data.
// - When using a 3.3V microcontroller with a 5V-powered NeoPixel strip,
//   a LOGIC-LEVEL CONVERTER on the data line is STRONGLY RECOMMENDED.
// (Skipping these may work OK on your workbench but can fail in the field)

#include <Adafruit_NeoPixel.h>
#define BUTTON_PIN   13 //trigger pull


// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
#define LED_PIN    8  

// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 2

int a=0; //set int to 0 as base value

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

void setup() {

pinMode (13, INPUT);       //trigger pin is input
  strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.show();            // Turn OFF all pixels ASAP
  strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
}

void loop() {
digitalRead(BUTTON_PIN);     //read trigger pin

  if (BUTTON_PIN == HIGH)    //if I pull trigger
  {(int a=a; a<255; a++);
  delay (500);}  //int a (brightness) increases to a max of 255
  else if (BUTTON_PIN == LOW)
  { (int a=a; a>=0; a--);
  delay (500);}   //when not pressed, brightness fades to zero

  // Fill along the length of the strip in various colors...
 // if (BUTTON_PIN == HIGH)
 { colorWipe(strip.Color(255,   0,   0), a); }// Red
 // if (BUTTON_PIN == HIGH)
 // {int a=a ; a<255 ; a++}
 // else if (BUTTON_PIN == LOW)
 // {int a=a ; a>=0 ; a--}

}

  void colorWipe(uint32_t color, int wait) {
  for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
    strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
    strip.show();                          //  Update strip to match
    delay(wait);                           //  Pause for a moment
  }
}

Ive rewritten your code to get rid of delay(), see the exellent forum post regarding doing many things at once for reference.

I also added a more straightforward counter system, to add / subtract to/from the a-variable.

I really dont understand how the colorswipe function is working, but it appears as the a-value is the actual delay between color changes, not the actual color change itself.
If it were my project the neopixels would start with a cool color and gradually change to red (hot) while button is pressed, but its NOT my project, so i hope youre happy with the way my code works out for you. :slight_smile:

// A basic everyday NeoPixel strip test program.

// NEOPIXEL BEST PRACTICES for most reliable operation:
// - Add 1000 uF CAPACITOR between NeoPixel strip's + and - connections.
// - MINIMIZE WIRING LENGTH between microcontroller board and first pixel.
// - NeoPixel strip's DATA-IN should pass through a 300-500 OHM RESISTOR.
// - AVOID connecting NeoPixels on a LIVE CIRCUIT. If you must, ALWAYS
//   connect GROUND (-) first, then +, then data.
// - When using a 3.3V microcontroller with a 5V-powered NeoPixel strip,
//   a LOGIC-LEVEL CONVERTER on the data line is STRONGLY RECOMMENDED.
// (Skipping these may work OK on your workbench but can fail in the field)

#include <Adafruit_NeoPixel.h>
#define BUTTON_PIN   13 //trigger pull


// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
#define LED_PIN    8  

// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 2

// old value int a=0; //set int to 0 as base value

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

// new code and variables:
uint32_t NOW = 0;
uint32_t lastUpdate = 0;
uint32_t lastCounterUpdate = 0;
uint32_t changeInterval = 500; // the interval for changing the color
uint8_t a = 0; //set int to 0 as base value, changed to unsigned int since you dont want negative values..

void checkElapsed(uint32_t timer,uint32_t limiter) {
    return (NOW - timer >= limiter);
}
// end new code

void setup() {

pinMode (13, INPUT);       //trigger pin is input
  strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.show();            // Turn OFF all pixels ASAP
  strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
}

void loop() {
// digitalRead(BUTTON_PIN);     //read trigger pin (but dont do anything with the read value??)

// new code
NOW = millis();
    // if button is read inverted (pressed button counts down), 
    // change the "if (digitalRead(BUTTON_PIN))"" to "if (!digitalRead(BUTTON_PIN))"
    if (digitalRead(BUTTON_PIN) {    //if I pull trigger
        if (checkElapsed(lastCounterUpdate,changeInterval)) {
            lastCounterUpdate = NOW;
            if (a < 255) {
                a++;
            }
        }
        colorWipe(strip.Color(255,   0,   0), a); // Red
    }
    else {
        if (checkElapsed(lastCounterUpdate,changeInterval)) {
            lastCounterUpdate = NOW;
            if (a) {
                a--;
            }
        }
    }
}

// end new code

/* OLD code
  if (BUTTON_PIN == HIGH)    //if I pull trigger
  {(int a=a; a<255; a++);
  delay (500);}  //int a (brightness) increases to a max of 255
  else if (BUTTON_PIN == LOW)
  { (int a=a; a>=0; a--);
  delay (500);}   //when not pressed, brightness fades to zero
 

  // Fill along the length of the strip in various colors...
 // if (BUTTON_PIN == HIGH)
 { colorWipe(strip.Color(255,   0,   0), a); }// Red
 // if (BUTTON_PIN == HIGH)
 // {int a=a ; a<255 ; a++}
 // else if (BUTTON_PIN == LOW)
 // {int a=a ; a>=0 ; a--}

}
*/
void colorWipe(uint32_t color, int wait) {
    for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
        strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
        strip.show();                          //  Update strip to match
        delay(wait);                           //  Pause for a moment
    }
}

Much appreciate the help! Just trying to wrap my head around the code you wrote :slight_smile: You are right about the color wipe command, I found a better effect to use.

Will be back soon with some sort of result! :slight_smile:

unorthodoxdesign:
..
Just trying to wrap my head around the code you wrote :slight_smile:
..

If there´s any unclear parts, copy/paste that part of the code (inside code tags) along with your questions and i´ll explain it for you. :slight_smile: