// Midterm
// Kayla Frost 300390878
// Feb 12 2025
// include servo library for the Servo motor
#include <Servo.h>
// include FastLED library for the NeoPixel ring
#include <FastLED.h>
// define pin numbers to led colors
int aled = 11; // amber led
int bled = 10; // blue led
int rled = 9; // red led
int gled = 8; // green led
// define pin numbers to button letters
int butd = 6; // yellow/amber button
int butc = 7; // blue button
int butb = 3; // green button
int buta = 2; // red button
//define servo name and pin number
int servoPin = 12;
// define servo motor called myservo
Servo myservo;
// variable to store motor's position (in degrees)
int pos = 0;
void setup() // put your setup code here, to run once:
{
// all pins connected to leds are outputs
pinMode(aled, OUTPUT);
pinMode(bled, OUTPUT);
pinMode(rled, OUTPUT);
pinMode(gled, OUTPUT);
// all pins connected to buttons are inputs
pinMode(butd, INPUT);
pinMode(butc, INPUT);
pinMode(butb, INPUT);
pinMode(buta, INPUT);
// turn all LEDs off initially
for (int pin = 8; pin <= 11; pin++)
{
digitalWrite(pin, HIGH);
}
// serial monitor to print stuff
Serial.begin(9600);
// attach servo motor to pin 12
myservo.attach(servoPin);
// move servo to initial position of 20 degrees
myservo.write(20);
}
void loop() // put your main code here, to run repeatedly:
{
// number of cells = 16 (given)
// NOTE: use #define or const int
#define neoNumCells 16
//define NeoPixel for pin it goes into
// NOTE: use #define or const int
#define neoData 13
// CRGB from library
// defines array with number of cells in ring
CRGB NeoArray[neoNumCells];
//fastLED.addLeds from library
// < NEOPIXEL , data> (array name , number of cells)
FastLED.addLeds<NEOPIXEL, neoData>(NeoArray, neoNumCells);
// while button b (green button) is pressed, change colors at once
//
// then, colors rotate with eachother
// while button b is pressed
while (digitalRead(butb))
{
for (int pixel = 0; pixel <= 16; pixel++)
{
// setup all initial colors
// ith cell - red
NeoArray[pixel] = CRGB::Red;
FastLED.show();
// next 3 cells are black
for (int cell = pixel + 1; cell <= pixel + 3; cell++)
{
NeoArray[cell] = CRGB::Black;
FastLED.show();
}
// [i+4]th cell - green
NeoArray[pixel + 4] = CRGB::Green;
FastLED.show();
// next 3 cells are black
for (int cell = pixel + 5; cell <= pixel + 7; cell++)
{
NeoArray[cell] = CRGB::Black;
FastLED.show();
}
// [i+8]th cell - dark turquoise
NeoArray[pixel + 8] = CRGB::DarkTurquoise;
FastLED.show();
// next 3 cells are black
for (int cell = pixel + 9; cell <= pixel + 11; cell++)
{
NeoArray[cell] = CRGB::Black;
FastLED.show();
}
// [i+12]th cell - blue
NeoArray[pixel + 12] = CRGB::Blue;
FastLED.show();
// next 3 cells are black
for (int cell = pixel + 13; cell <= pixel + 15; cell++)
{
NeoArray[cell] = CRGB::Black;
FastLED.show();
}
// half a second delay until colors rotate
delay(500);
for (int x = 0; x < 16; x++)
{
NeoArray[x] = CRGB::Black;
FastLED.show();
}
delay(500);
}
}
// while button b is pressed
/*while (digitalRead(butb))
{
for (int pixel = 0; pixel <= 16; pixel++)
{
// setup all initial colors
// ith cell - red
NeoArray[pixel] = CRGB::Red;
FastLED.show();
delay(150);
NeoArray[pixel] = CRGB::Black;
FastLED.show();
delay(150);
// [i+4]th cell - green
NeoArray[pixel + 4] = CRGB::Green;
FastLED.show();
delay(150);
NeoArray[pixel + 4] = CRGB::Black;
FastLED.show();
delay(150);
// [i+8]th cell - dark turquoise
NeoArray[pixel + 8] = CRGB::DarkTurquoise;
FastLED.show();
delay(150);
NeoArray[pixel + 8] = CRGB::Black;
FastLED.show();
delay(150);
// [i+12]th cell - blue
NeoArray[pixel + 12] = CRGB::Blue;
FastLED.show();
delay(150);
NeoArray[pixel + 12] = CRGB::Black;
FastLED.show();
delay(150);
for (int x = 0; x < 16; x++)
{
NeoArray[pixel + 12] = CRGB::Black;
FastLED.show();
}
delay(500);
}
}*/
}