// 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))
{
// first quarter
for (int pixel = 0; pixel <= 3; pixel++)
{
// ith cell - red
NeoArray[pixel] = CRGB::Red;
FastLED.show();
// [i+4]th cell - green
NeoArray[pixel + 4] = CRGB::Green;
FastLED.show();
// [i+8]th cell - dark turquoise
NeoArray[pixel + 8] = CRGB::DarkTurquoise;
FastLED.show();
// [i+12]th cell - blue
NeoArray[pixel + 12] = CRGB::Blue;
FastLED.show();
delay(150);
// ith cell - red
NeoArray[pixel] = CRGB::Black;
FastLED.show();
// [i+4]th cell - green
NeoArray[pixel + 4] = CRGB::Black;
FastLED.show();
// [i+8]th cell - dark turquoise
NeoArray[pixel + 8] = CRGB::Black;
FastLED.show();
// [i+12]th cell - blue
NeoArray[pixel + 12] = CRGB::Black;
FastLED.show();
}
// second quarter
for (int pixel = 0; pixel <= 3; pixel++)
{
// ith cell
NeoArray[pixel] = CRGB::Blue;
FastLED.show();
// [i+4]th cell
NeoArray[pixel + 4] = CRGB::Red;
FastLED.show();
// [i+8]th cell
NeoArray[pixel + 8] = CRGB::Green;
FastLED.show();
// [i+12]th cell
NeoArray[pixel + 12] = CRGB::DarkTurquoise;
FastLED.show();
delay(150);
// ith cell - red
NeoArray[pixel] = CRGB::Black;
FastLED.show();
// [i+4]th cell - green
NeoArray[pixel + 4] = CRGB::Black;
FastLED.show();
// [i+8]th cell - dark turquoise
NeoArray[pixel + 8] = CRGB::Black;
FastLED.show();
// [i+12]th cell - blue
NeoArray[pixel + 12] = CRGB::Black;
FastLED.show();
}
// third quarter
for (int pixel = 0; pixel <= 3; pixel++)
{
// ith cell
NeoArray[pixel] = CRGB::DarkTurquoise;
FastLED.show();
// [i+4]th cell
NeoArray[pixel + 4] = CRGB::Blue;
FastLED.show();
// [i+8]th cell
NeoArray[pixel + 8] = CRGB::Red;
FastLED.show();
// [i+12]th cell
NeoArray[pixel + 12] = CRGB::Green;
FastLED.show();
delay(150);
// ith cell - red
NeoArray[pixel] = CRGB::Black;
FastLED.show();
// [i+4]th cell - green
NeoArray[pixel + 4] = CRGB::Black;
FastLED.show();
// [i+8]th cell - dark turquoise
NeoArray[pixel + 8] = CRGB::Black;
FastLED.show();
// [i+12]th cell - blue
NeoArray[pixel + 12] = CRGB::Black;
FastLED.show();
}
// fourth quarter
for (int pixel = 0; pixel <= 3; pixel++)
{
// ith cell
NeoArray[pixel] = CRGB::Green;
FastLED.show();
// [i+4]th cell
NeoArray[pixel + 4] = CRGB::DarkTurquoise;
FastLED.show();
// [i+8]th cell
NeoArray[pixel + 8] = CRGB::Blue;
FastLED.show();
// [i+12]th cell
NeoArray[pixel + 12] = CRGB::Red;
FastLED.show();
delay(150);
// ith cell - red
NeoArray[pixel] = CRGB::Black;
FastLED.show();
// [i+4]th cell - green
NeoArray[pixel + 4] = CRGB::Black;
FastLED.show();
// [i+8]th cell - dark turquoise
NeoArray[pixel + 8] = CRGB::Black;
FastLED.show();
// [i+12]th cell - blue
NeoArray[pixel + 12] = CRGB::Black;
FastLED.show();
}
}
}