Hi @Joecool,
The code from my previous post already contains the initialization of the button.
PropButton
is an object that contains generic code to work with all your buttons. It’s done that way so you can reuse the code inside the object to work with buttons connected to different pins. When you do:
PropButton colorChangeButton; // The button for color changing
You are creating a button called colorChangeButton
. And when you do this:
colorChangeButton.begin(3, ButtonActiveLow); // Active-low button on pin 3
You are initializing the colorChangeButton
on pin 3 in ButtonActiveLow
mode. That begin
here above is the one you see in the documentation, but redacted to in a way to explain its syntax.
The same applies to objects like WavPlayer
, LedStrip
or HBLED
.
Here is the whole code from the example with the modifications from the other post. It compiles, but I didn’t tested it. I’ve also modified the ledRampDown
, ledRampUp
and ledShimmer
functions to follow the current color combination. As is, the color combinations are pure red, pure green and pure blue. You can later modify said functions to do other color combinations.
#include <PropButton.h>
// Button pins
#define BUTTON_PIN 1
#define BUTTON_LED_PIN 2
// Lightsaber states
#define STATE_OFF 0
#define STATE_POWER_ON 1
#define STATE_IDLE 2
#define STATE_POWER_OFF 3
#define STATE_HIT 4
#define STATE_BLASTER 5
// Blade color combination
#define BLADE_RED 1
#define BLADE_GREEN 2
#define BLADE_BLUE 3
// Five minutes before entering low power
#define TIME_BEFORE_LOW_POWER 300000
// Red LED on output 1
HBLED red(1);
// Green LED on output 2
HBLED green(2);
// Blue LED on output 3
HBLED blue(3);
// One WAV player for the hum sound, one for the sound effects
WavPlayer hum, fx;
// The player to play a sound when color changes
WavPlayer colorChangerSound;
// Our push button with LED
PropButton OnOffButton;
// The button for color changing
PropButton colorChangeButton;
// This variable holds the current color combination,
// starting with RED
uint8_t colorCombination = BLADE_RED;
// The current state
uint8_t state = STATE_OFF;
// Two booleans to signal motion events
volatile bool on_swing = false;
volatile bool on_hit = false;
// This variable holds the time (millis() ticks)
// since the program in the STATE_OFF state.
uint32_t time_off;
void setup()
{
// Initialize audio
Audio.begin();
// Initialize motion sensor
Motion.begin();
Motion.configPulse(AxisAll, 3.78f, 500, 100, MotionInterrupt1 );
Motion.configTransient(AxisAll, 0.1f, 120, MotionInterrupt2);
Motion.attachInterrupt(MotionInterrupt1, MotionPulse);
Motion.attachInterrupt(MotionInterrupt2, MotionTransient);
// Initialize LEDs
blue.begin(700); // Blue LED with 700mA maximum current
red.begin(700); // Red LED with 700mA maximum current
green.begin(700); // Green LED 700mA maximum current
// Initialize On/Off button
OnOffButton.begin(BUTTON_PIN, ButtonActiveLow);
// Initialize the button for color change
colorChangeButton.begin(3, ButtonActiveLow); // Active-low button on pin 3
// Configure the OnOffButton to declare a long press after 1000ms
OnOffButton.setLongPressTime(1000);
// Initial blinking LED sequence, cycle: 2000ms, time on: 100ms
OnOffButton.blink(BUTTON_LED_PIN, 2000, 100);
// We entered in the STATE_OFF state
time_off = millis();
}
void loop()
{
ButtonEvent button_event;
// Check the current state
switch(state)
{
case STATE_OFF:
// If On/Off button is pressed
if (OnOffButton.pressed())
{
// New blinking sequence, cycle: 500ms, time on: 100ms
OnOffButton.blink(BUTTON_LED_PIN, 500, 100);
// Wait for the user to release the OnOffButton
while (OnOffButton.pressed());
// Go to STATE_POWER_ON
state = STATE_POWER_ON;
} else {
// Check if we have to go into low-power
if (millis() - time_off >= TIME_BEFORE_LOW_POWER)
{
// Stop blinking the button LED
OnOffButton.blink(BUTTON_LED_PIN, 0, 0);
// Enter low power mode. Wait for a FALLING edge
// on the button pin.
enterLowPowerMode(BUTTON_PIN, FALLING);
}
}
break;
case STATE_POWER_ON:
// Play ignition sound
fx.play("on0.wav");
// Ramp LEDs up
ledRampUp(fx.duration());
// Wait a little before launching the hum sound
delay(500);
// Play hum sound in loop mode
hum.play("idle1.wav", PlayModeLoop);
// Wait until ignition sound finishes
while (fx.playing());
// Start shimmering LEDs
ledShimmer();
// Check if user still pressing the On/Off button. If so, wait.
while (OnOffButton.pressed());
// Ignore any motion detected previous to STATE_IDLE
on_swing = on_hit = false;
// Clear any OnOffButton queued events
OnOffButton.resetEvents();
// Go to STATE_IDLE
state = STATE_IDLE;
break;
case STATE_IDLE:
// This is the main state. The hum sound is looping in the background
// while we monitor for motion and buttons events.
button_event = colorChangeButton.getEvent();
if (button_event == ButtonShortPressAndRelease)
// Button pressed and released. Call the color changing function.
changeColor();
button_event = OnOffButton.getEvent();
// If On/Off button is long-pressed, go to STATE_POWER_OFF.
if (button_event == ButtonLongPressed)
{
// Go back to initial blinking sequence
OnOffButton.blink(BUTTON_LED_PIN, 2000, 100);
state = STATE_POWER_OFF;
break;
}
// If OnOffButton is pressed and released, go to STATE_BLASTER.
if (button_event == ButtonShortPressAndRelease)
{
state = STATE_BLASTER;
break;
}
// Check hits
if (on_hit)
{
on_hit = 0;
state = STATE_HIT;
break;
}
// Check swings
if (on_swing)
{
on_swing = 0;
// Play a random swing sound (from swing0.wav to swing7.wav)
fx.playRandom("swing", 0, 7);
}
break;
case STATE_HIT:
// Flash the LEDs
ledFlash();
// Play a random strike sound (from strike0.wav to strike2.wav)
fx.playRandom("strike", 0, 2);
while (fx.playing())
{
// Check if another clash happened while we were playing the sound
if (on_hit)
{
on_hit = 0;
// Play again
fx.playRandom("strike", 0, 2);
}
}
// Go back to IDLE shimmering and to STATE_IDLE state
ledShimmer();
state = STATE_IDLE;
break;
case STATE_BLASTER:
// Blaster hit, start flashing the LEDs
ledFlash();
// Play a random blaster sound (from hit0.wav to hit4.wav)
fx.playRandom("hit", 0, 4);
while (fx.playing())
{
// Check if the OnOffButton was pressed againg while playing the blaster sound
if (OnOffButton.getEvent() == ButtonShortPressAndRelease)
// Play again
fx.playRandom("hit", 0, 4, PlayModeNormal);
}
// No more blasters, stop flashing and go back to STATE_IDLE
ledShimmer();
state = STATE_IDLE;
break;
case STATE_POWER_OFF:
// Stop the hum sound
hum.stop();
// Play the OFF sound
fx.play("off0.wav");
// Ramp the LEDs down
ledRampDown(fx.duration());
while (fx.playing());
// Wait until OnOffButton is released
while (OnOffButton.pressed());
// Initial blinking LED sequence, cycle: 2000ms, time on: 100ms
OnOffButton.blink(BUTTON_LED_PIN, 2000, 100);
// Remember when we entered in the STATE_OFF state
time_off = millis();
// Go back to OFF state
state = STATE_OFF;
break;
}
}
// Motion transient interrupt (swing)
void MotionTransient()
{
on_swing = true;
}
// Motion pulse interrupt (hit)
void MotionPulse()
{
on_hit = true;
}
void ledShimmer()
{
// Check which color combination we should show
switch(colorCombination)
{
case BLADE_RED: // red
// Shimmer from 220 to 255 and back with a 2Hz frequency
red.shimmer(220, 255, 2, 0);
// Turn off the other LEDS
green.setValue(0);
blue.setValue(0);
break;
case BLADE_GREEN: // green
// Shimmer from 220 to 255 and back with a 2Hz frequency
green.shimmer(220, 255, 2, 0);
// Turn off the other LEDS
red.setValue(0);
blue.setValue(0);
break;
case BLADE_BLUE: // blue
// Shimmer from 220 to 255 and back with a 2Hz frequency
blue.shimmer(220, 255, 2, 0);
// Turn off the other LEDS
red.setValue(0);
green.setValue(0);
break;
}
}
static void ledFlash()
{
// Flash all LEDs from a value of 255 to 100, frequency 20Hz, infinite duration
blue.flash(255, 100, 20, 0);
red.flash(255, 100, 20, 0);
green.flash(255, 100, 20, 0);
}
void ledRampUp(uint32_t duration)
{
// Check which color combination we should ramp up
switch(colorCombination)
{
case BLADE_RED: // red
// Red LED ramp up from 0 to 50
red.ramp(0, 220, duration);
break;
case BLADE_GREEN: // green
// Green LED ramp up from 0 to 50
green.ramp(0, 220, duration);
break;
case BLADE_BLUE: // blue
// Blue LED ramp up from 0 to 50
blue.ramp(0, 220, duration);
break;
}
}
void ledRampDown(uint32_t duration)
{
// Check which color combination we should ramp down
switch(colorCombination)
{
case BLADE_RED: // red
// Ramp down the red LED from 255 to 0
red.ramp(255, 0, duration);
break;
case BLADE_GREEN: // green
// Ramp down the green LED from 255 to 0
green.ramp(255, 0, duration);
break;
case BLADE_BLUE: // blue
// Ramp down the blue LED from 255 to 0
blue.ramp(255, 0, duration);
break;
}
}
void changeColor()
{
// Play a sound
colorChangerSound.play("color_change.wav");
colorCombination++;
if (colorCombination == 4)
colorCombination = BLADE_RED;
ledShimmer();
}
I know if you have none or little programming experience it can be shocking the first time you try some modification. But once you grasp it, you can make the PropBoard do many wonderful things.
Since you can program the PropBoard from the Arduino IDE, you can learn more about programming from here, specially the Programming section.
Let me know how it goes.