The third and final function for our integrated intelliPost and interctive Armour is the score keeping function.
Here it is in action...
This is the code...
// Keep score with 2 buttons and two neopixel strips.
// press a button and the corresponding strip lights by one LED
// when max score is reached an alarm sounds
// for starters, I'll try to make it work with one button and one neopixel strip
#include <Adafruit_NeoPixel.h>
#include <Bounce2.h>
// Digital IO pin connected to the button. This will be driven with a
// pull-up resistor so the switch pulls the pin to ground momentarily.
// On a high -> low transition the button press logic will execute.
#define BUTTON_PIN 2 // Defines the button pin
#define PIXEL_PIN 6 // Digital IO pin connected to the NeoPixels.
#define PIXEL_COUNT 8 // Number of NeoPixels
int piezoPin = 10; // set the piezo pin number
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_RGBW + 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_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Bounce debouncerA = Bounce(); // Instantiate a Bounce object - reads a button push
int SCORE = 0;
void setup() {
Serial.begin (9600);
strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
strip.show(); // Initialize all pixels to 'off'
debouncerA.attach(BUTTON_PIN, INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
debouncerA.interval(25); // Use a debounce interval of 25 milliseconds
}
void loop() {
uint32_t green = strip.Color(255, 0, 0);
uint32_t blue = strip.Color(0, 0, 255);
uint32_t red = strip.Color(0, 255, 0);
// Get current button state.
debouncerA.update(); // Update the Bounce instance
if (debouncerA.fell()) // press button A
{
SCORE = SCORE + 1;
tone(piezoPin, 3000, 80); //( pin number, frequency in hertz, duration in milliseconds);
delay (100);
}
if (SCORE>0) {
strip.fill(red, 0, SCORE); // turns on the LEDs(colour, starting LED, number of LEDs)
strip.show(); // updates the LED bar;
}
if (SCORE>=PIXEL_COUNT) {
tone(piezoPin, 10000, 700); //( pin number, frequency in hertz, duration in milliseconds);
delay(100);
while(1);
}
Serial.println (SCORE);
}
Here it is in action...
This is the code...
// Keep score with 2 buttons and two neopixel strips.
// press a button and the corresponding strip lights by one LED
// when max score is reached an alarm sounds
// for starters, I'll try to make it work with one button and one neopixel strip
#include <Adafruit_NeoPixel.h>
#include <Bounce2.h>
// Digital IO pin connected to the button. This will be driven with a
// pull-up resistor so the switch pulls the pin to ground momentarily.
// On a high -> low transition the button press logic will execute.
#define BUTTON_PIN 2 // Defines the button pin
#define PIXEL_PIN 6 // Digital IO pin connected to the NeoPixels.
#define PIXEL_COUNT 8 // Number of NeoPixels
int piezoPin = 10; // set the piezo pin number
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_RGBW + 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_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Bounce debouncerA = Bounce(); // Instantiate a Bounce object - reads a button push
int SCORE = 0;
void setup() {
Serial.begin (9600);
strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
strip.show(); // Initialize all pixels to 'off'
debouncerA.attach(BUTTON_PIN, INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
debouncerA.interval(25); // Use a debounce interval of 25 milliseconds
}
void loop() {
uint32_t green = strip.Color(255, 0, 0);
uint32_t blue = strip.Color(0, 0, 255);
uint32_t red = strip.Color(0, 255, 0);
// Get current button state.
debouncerA.update(); // Update the Bounce instance
if (debouncerA.fell()) // press button A
{
SCORE = SCORE + 1;
tone(piezoPin, 3000, 80); //( pin number, frequency in hertz, duration in milliseconds);
delay (100);
}
if (SCORE>0) {
strip.fill(red, 0, SCORE); // turns on the LEDs(colour, starting LED, number of LEDs)
strip.show(); // updates the LED bar;
}
if (SCORE>=PIXEL_COUNT) {
tone(piezoPin, 10000, 700); //( pin number, frequency in hertz, duration in milliseconds);
delay(100);
while(1);
}
Serial.println (SCORE);
}
Comments
Post a Comment