// 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;
// integer to hold randomly generated number
int randomLED = 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);
// for Task 1: shuffles random function
randomSeed(randomLED);
}
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 (digitalRead(buta))
{
// generate random number between pins for leds (between 8 and 11)
randomLED = random(gled, aled + 1); // website says (min, max - 1)
// used to test if random generator is working
//Serial.println(randomLED);
// blink twice with 150ms between
digitalWrite(randomLED, LOW);
delay(150);
digitalWrite(randomLED, HIGH);
delay(150);
digitalWrite(randomLED, LOW);
delay(150);
digitalWrite(randomLED, HIGH);
delay(150);
// large pause between next random selection
// because sometimes the same led is picked twice in a row
// and it blinks 4 times straight
delay(1000);
}
// while button b (green button) is pressed, change colors at once
// then, colors rotate with eachother
// while button b is pressed
// 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();
}
}
// 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);
}
}
// while button d (yellow) is pressed
// servo from 15 to 165 degrees, incremented by 25 degress each time
while(digitalRead(butd))
{
// initial position of servo
pos = 15;
// only first half of the ring and servo moving clockwise
for (int index = 0; index <= 7; index++)
{
if (index < 7) // so it doesn't move past 165 degrees
{
//move servo clockwise
myservo.write(pos + ( 25 * index));
}
// color cells
// lime colored cell index
int indexL = index;
// orange color cell index
int indexO = index + 8;
// one side
NeoArray[indexL] = CRGB::Lime;
FastLED.show();
// other side
NeoArray[indexO] = CRGB::DarkOrange;
FastLED.show();
delay(150);
// cells back to black
// one side
NeoArray[indexL] = CRGB::Black;
FastLED.show();
// other side
NeoArray[indexO] = CRGB::Black;
FastLED.show();
delay(150);
}
// just get half working, and then flip the colors
// for the other half
// only first half of the ring and servo moving clockwise
for (int index = 0; index <= 7; index++)
{
if (index < 7) // so it doesn't move past 15 degrees
{
//move servo counter clockwise
myservo.write(165 - (25 * index));
}
// color cells (switch them around)
// orange colored cell index
int indexO = index;
// lime color cell index
int indexL = index + 8;
// one side
NeoArray[indexL] = CRGB::Lime;
FastLED.show();
// other side
NeoArray[indexO] = CRGB::DarkOrange;
FastLED.show();
delay(150);
// cells back to black
// one side
NeoArray[indexO] = CRGB::Black;
FastLED.show();
// other side
NeoArray[indexL] = CRGB::Black;
FastLED.show();
delay(150);
}
}
}