// Exercise 3
// while button d is pressed:
// - rotate servo to 10 degrees and single blink 1st cell purple
// - rotate servo to 60 degrees and double blink 5th cell deep pink
// - rotate servo to 110 degrees and triple blink 9th cell green yellow
// - rotate servo to 160 degrees and quad blink 13th cell lime
// include servo library for the Servo motor
#include <Servo.h>
// include fastLED library for the NeoPixel ring
#include <FastLED.h>
// define pins numerically
int aled = 11; // amber led
int bled = 10; // blue led
int rled = 9; // red led
int gled = 8; // green led
// define buttons numerically
int butd = 6; // yellow/amber button
int butc = 7; // blue button
int butb = 3; // green button
int buta = 2; // red button
//define servo
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);
// serial monitor to print stuff
Serial.begin(9600);
//attach servo motor to pin 12
myservo.attach(servoPin);
}
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 d is pressed:
while (digitalRead(butd))
{
// rotate servo to 10 degrees and single blink 1st cell purple
myservo.write(10);
NeoArray[0] = CRGB::Black;
FastLED.show();
delay(1000);
NeoArray[0] = CRGB::Purple;
FastLED.show();
delay(1000); // cells start at 0
NeoArray[0] = CRGB::Black;
FastLED.show();
delay(1000);
// take a break
delay(2000);
// rotate servo to 60 degrees and double blink 5th cell deep pink
myservo.write(60);
NeoArray[6] = CRGB::DeepPink; // 5 + 1 bc cells start at 0
FastLED.show();
delay(1000);
NeoArray[6] = CRGB::Black;
FastLED.show();
delay(1000);
NeoArray[6] = CRGB::DeepPink;
FastLED.show();
delay(1000);
NeoArray[6] = CRGB::Black;
FastLED.show();
delay(1000);
// take a break
delay(1000);
// rotate servo to 110 degrees and triple blink 9th cell green yellow
myservo.write(110);
NeoArray[10] = CRGB::GreenYellow;
// take a break
delay(1000);
// rotate servo to 160 degrees and quad blink 13th cell lime
myservo.write(160);
NeoArray[14] = CRGB::Lime;
// take a break
delay(1000);
}
}