#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#include <FastLED.h>
#include "pitches.h"
// Push button params...
#define LED_PIN 13
#define BUTTON_PIN 12
int led_state = LOW; // the current state of LED
int button_state; // the current state of button
int last_button_state; // the previous state of button
// Speaker params...
#define SPEAKER_PIN 14
const int gameTones[] = {NOTE_G3, NOTE_C4, NOTE_E4, NOTE_G5};
int delayMS = 100;
// LED ring params...
#define LED_RING1_PIXELS 16
#define LED_RING_GREEN1_PIN 15
#define LED_RING_RED1_PIN 15
#define LED_RING_GREEN2_PIN 15
#define LED_RING_WHITE_PIN 15
#define LED_RING_GREEN3_PIN 15
#define LED_RING_RED2_PIN 15
#define LED_RING_GREEN4_PIN 15
Adafruit_NeoPixel led_ring_green1 = Adafruit_NeoPixel(LED_RING1_PIXELS, LED_RING_GREEN1_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel led_ring_red1 = Adafruit_NeoPixel(LED_RING1_PIXELS, LED_RING_RED1_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel led_ring_green2 = Adafruit_NeoPixel(LED_RING1_PIXELS, LED_RING_GREEN2_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel led_ring_white = Adafruit_NeoPixel(LED_RING1_PIXELS, LED_RING_WHITE_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel led_ring_green3 = Adafruit_NeoPixel(LED_RING1_PIXELS, LED_RING_GREEN3_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel led_ring_red2 = Adafruit_NeoPixel(LED_RING1_PIXELS, LED_RING_RED2_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel led_ring_green4 = Adafruit_NeoPixel(LED_RING1_PIXELS, LED_RING_GREEN4_PIN, NEO_GRB + NEO_KHZ800);
// LED strip params...
#define LED_STRING_DATA_PIN 4
#define LED_STRING_PIXELS 72 // 5 /*the number of leds that will light. If */
CRGB ledsA[LED_STRING_PIXELS]; // sets number of pixels that will light on each strip.
// Function declarations...
void playTone(byte ledIndex);
void iterate_pixels(uint32_t c);
void lightLedRing();
void led_crawl();
void setup()
{
// Serial.begin(115200);
Serial.begin(9600);
Serial.println("Hello Homestead Holidays!\n");
// initialize the pushbutton pin as an pull-up input
pinMode(BUTTON_PIN, INPUT_PULLUP);
button_state = digitalRead(BUTTON_PIN);
// set the indicagtor LED as output...
pinMode(LED_PIN, OUTPUT);
pinMode(SPEAKER_PIN, OUTPUT);
led_ring1.begin(); // This initializes the NeoPixel library.
FastLED.addLeds<NEOPIXEL, LED_STRING_DATA_PIN>(ledsA, LED_STRING_PIXELS); // GRB ordering is assumed
}
bool clicked_once = false;
void loop()
{
last_button_state = button_state; // save the last state
button_state = digitalRead(BUTTON_PIN); // read new state
if (last_button_state == HIGH && button_state == LOW)
{
Serial.println("The button is pressed");
// toggle state of LED
led_state = !led_state;
// control LED arccoding to the toggled state
digitalWrite(LED_PIN, led_state);
playTone(0);
playTone(1);
playTone(2);
playTone(3);
delay(10);
// led_crawl();
lightLedRing();
clicked_once = true;
}
// lightLedRing();
if (clicked_once) {
led_crawl();
}
}
void playTone(byte ledIndex)
{
Serial.print("playing tone ");
Serial.print(String(gameTones[ledIndex]));
Serial.println("...");
tone(SPEAKER_PIN, gameTones[ledIndex]);
// Serial.println("tone played. delaying...");
delay(300);
// Serial.println("delay done. setting noTone...");
noTone(SPEAKER_PIN);
// Serial.println("set noTone done.");
}
//**************************************************
// LED Ring...
// From https://wokwi.com/projects/377944172932184065
//**************************************************
static uint32_t color_sequence[] = {
Adafruit_NeoPixel::Color(255, 0, 0),
Adafruit_NeoPixel::Color(0, 255, 0),
Adafruit_NeoPixel::Color(0, 0, 255),
Adafruit_NeoPixel::Color(255, 255, 0),
Adafruit_NeoPixel::Color(255, 0, 255),
Adafruit_NeoPixel::Color(0, 255, 255),
};
void iterate_pixels(uint32_t c)
{
for (int i = LED_RING1_PIXELS - 1; i >= 0; i--)
{
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
led_ring1.setPixelColor(i, c);
led_ring1.show(); // This sends the updated pixel color to the hardware.
delay(delayMS); // Delay for a period of time (in milliseconds).
led_ring1.setPixelColor(i, 0);
led_ring1.show();
} // 1st For
led_ring1.clear();
led_ring1.show();
}
void lightLedRing()
{
for (unsigned int i = 0; i < sizeof(color_sequence) / sizeof(color_sequence[0]); ++i)
{
iterate_pixels(color_sequence[i]);
}
}
//**************************************************
// LED Strip...
// From https://wokwi.com/projects/393526500318889985
//**************************************************
int direction = 1; // 1 for left, -1 for right
int currentLED = 0;
void led_crawl()
{
Serial.println("Starting LED crawl...\n");
delay(10); // this speeds up the simulation
// Clear all LEDs
for (int j = 0; j < LED_STRING_PIXELS; j++)
{
ledsA[j] = CRGB::Black;
}
// Serial.print("currentLED ");
// Serial.println(currentLED);
// Add gradient tail
if (currentLED - direction <= LED_STRING_PIXELS - 1 && currentLED - direction >= 0)
{
ledsA[currentLED - direction] = CHSV(160, 255, 200);
}
if (currentLED - (direction * 2) <= LED_STRING_PIXELS - 1 && currentLED - (direction * 2) >= 0)
{
ledsA[currentLED - (direction * 2)] = CHSV(160, 255, 155);
}
if (currentLED - (direction * 3) <= LED_STRING_PIXELS - 1 && currentLED - (direction * 3) >= 0)
{
ledsA[currentLED - (direction * 3)] = CHSV(160, 255, 100);
}
if (currentLED - (direction * 4) <= LED_STRING_PIXELS - 1 && currentLED - (direction * 4) >= 0)
{
ledsA[currentLED - (direction * 4)] = CHSV(160, 255, 55);
}
if (currentLED - (direction * 5) <= LED_STRING_PIXELS - 1 && currentLED - (direction * 5) >= 0)
{
ledsA[currentLED - (direction * 5)] = CHSV(160, 255, 10);
}
// Switch direction
if (currentLED + direction > LED_STRING_PIXELS - 1 || currentLED + direction < 0)
{
direction = direction * -1;
}
currentLED = currentLED + direction;
FastLED.show();
delay(60);
}