// 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 A (red button) is pressed, continuously and randomly select one LED and blink twice
// while button b (green button) is pressed, change colors at once, then
// while button c (blue button) is pressed, smooth rotate the servo back and forth between 20 and 120 degrees
while (digitalRead(butc))
{
// smooth rotation fro 20 to 120 degrees
for (pos = 20; pos <= 120; pos++)
{
myservo.write(pos);
delay(8);
}
// smooth rotation fro 20 to 120 degrees
for (pos = 120; pos >= 20; pos--)
{
myservo.write(pos);
delay(8);
}
}
}